Memory occupied by Java program

Hello All,

I have just solved [one problem][1] given on CodeChef for sorting a list of integer.

Solved using two methods:

  1. Using ArrayList ([solution][2])

  2. Using Array ([solution][3])

Now time and memory occupied by above solution is:

ArrayList


Time: 3.46

Memory: 1273M

Array


Time: 3.94

Memory: 1273M

Java Arrays are faster then ArrayList and it occupies less memory than ArrayList.

Can anyone tell me why it uses same memory for both solution?

Thanks!
[1]: https://www.codechef.com/problems/TSORT
[2]: https://www.codechef.com/viewsolution/12154541
[3]: https://www.codechef.com/viewsolution/12154563

@mamatagelanee this is because of how java programs are executed. Codechef uses SPOJ’s platform to run their program, and the memory usage shown by SPOJ is the memory appears to be used by the program to the OS, not real memory. OS allocates memory to JVM and JVM allocates a fixed memory (heap memory) to program when it starts. Since JVM heap size is fixed at SPOJ and is usually more than required by the program, so often code shows same memory consumption.

For more details, you can refer to this thread.

2 Likes

I think you swapped the times taken by mistake, it’s 3.46 using array and 3.94 using ArrayList.

Do not worry about the memory,just don not declare any array of size>10^7 or a 2D array of [i][j] such that i*j >10^7 , and you will do fine.

//