Java - Runtime.freeMemory ()

I tried to see the use of Runtime.freeMemory ().

The documentation says "Returns the amount of free memory in a Java virtual machine"

I performed a simple test program. The program below:

public class Test { public static void main(String a[]) throws Exception { System.out.println("Total memory: " +Runtime.getRuntime().totalMemory()); System.out.println("Free memory: " +Runtime.getRuntime().freeMemory()); Integer intArr[]= new Integer[10000]; for(int i =0; i<10000;i++){ intArr[i] = new Integer(i+500); } System.out.println("Free memory: " +Runtime.getRuntime().freeMemory()); System.out.println("sample print :"+ intArr[0]); System.out.println("sample print :"+ intArr[5000]); System.out.println("sample print :"+ intArr[9999]); } } 

Output:

Total memory: 67108864

Free memory: 61822408 <Before allocating 10,000 objects>

Free memory: 61822408 <Size remains the same even after 10,000 objects are selected. why? >

print sample: 500

print sample: 5500

print sample: 10499

Since objects are created on the heap, the value "Free memory" printed a second time must be less than the first output, right?

But it prints the same value. Can someone explain why it prints the same value?

+4
source share
3 answers

I know that this is not the answer you need, but according to JavaDoc - freeMemory returns:

a approximation to the total amount of memory currently available for future allocated objects, measured in bytes.

Just to check it out - I took your code and ran twice. Once with an array size of 10,000, and once with 100. I also added another snapshot right after:

 Integer intArr[]= new Integer[10000]; 

When starting from 10,000 - I got the expected result, a decrease of 40,0016 bytes in free memory immediately after creating the array.

When working with 100 I got the same amount of free memory before and after creating the array - not the desired effect.

As already mentioned, most of the answers, since this is a native method, depends on the JVM and therefore can act differently on any platform. I am running Windows 7 using the Eclipse built-in JVM (v3.6) .

But I think the key word here is approximation .

+3
source

Memory is allocated from the surrounding operating system in large chunks. The combination of 10,000 objects is not large enough to cause additional selection.

+1
source

Just a proven bytecode .. the snippet is the following and it is in the middle of two printf :

 SIPUSH 10000 ANEWARRAY java/lang/Integer ASTORE 1 

Therefore, it actually allocates the array dynamically, and freeMemory should return a different value. Since this is not the case, I believe that it really depends on the platform / version, as already indicated.

For example, on my machine, this really changes:

 Free memory: 81915960 Free memory: 81353824 

One more assumption: it may depend on the initial JVM heap size parameter, so if the JVM starts with a sufficient heap, ready to use, it does not need to allocate it to a certain threshold (this is -Xms , you can try to increase or decrease it, to see something has changed).

+1
source

Source: https://habr.com/ru/post/1234169/


All Articles