Java still uses system memory after freeing objects and garbage collection

I run JVM 1.5.0 (Mac OS X Default) and I control my Java program in Activity Monitor. I have the following:

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Date; public class MemoryTest { public static void memoryUsage() { System.out.println( Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() ); } public static void main( String[] args ) throws IOException { /* create a list */ ArrayList<Date> list = new ArrayList<Date>(); /* fill it with lots of data */ for ( int i = 0; i < 5000000; i++ ) { list.add( new Date() ); } // systems shows ~164 MB of physical being used /* clear it */ memoryUsage(); // about 154 MB list.clear(); list = null; System.gc(); memoryUsage(); // about 151 KB, garbage collector worked // system still shows 164 MB of physical being used. System.out.println("Press enter to end..."); BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) ); br.readLine(); } 

}

So why isn't physical memory freed up even though the garbage collector seems to be working fine?

+8
java garbage-collection memory-management memory-leaks
Nov 27 '08 at 19:11
source share
4 answers

Many JVMs never return memory to the operating system. Regardless of whether he does it or not, it depends on the implementation. For those who do not, the memory limits specified at startup, usually through the -Xmx flag, are the primary means of memory backup for other applications.

I find it difficult to find documentation on this, but the garbage collector for Sun Java 5 does this, assuming that under the right conditions, the heap will decrease if the correct collector is used. By default, if more than 70% of the heap is free, it will be reduced, so only 40% will be free. The command line options for controlling them are -XX:MinHeapFreeRatio and -XX:MaxHeapFreeRatio .

+19
Nov 27 '08 at 19:16
source share

There are several command line options for the JVM that help you customize the heap size used by Java. Everyone knows (or should know) about -Xms and -Xmx, which set the minimum and maximum heap sizes.

But there are also -XX: MinHeapFreeRatio and -XX: MaxHeapFreeRatio, which are the corresponding limits between which the JVM manages the free space. It does this by reducing the heap used, and this can reduce program memory consumption.

Here you can find more information:

+5
Nov 27 '08 at 19:39
source share

You need to use the JVM profiler to monitor the actual heap space used by the program, as opposed to the memory allocated for the JVM.

The JVM not only does not want to release the heap memory that it allocates, but also seeks to burn the space for various reasons, including just-in-time compilation.

+3
Nov 27 '08 at 19:27
source share

Is it possible that the OS displays the memory that is currently allocated to the program. Although 150 MB is allocated, this does not mean that 150 MB is used.

0
Nov 27 '08 at 19:16
source share



All Articles