Does Java Garbage Collector free the unused memory

I have a small doubt regarding the java garbage collection. When I place System.gc() in my code, it will invoke garbage collection. But it's up to JVM to call garbage collector or not. My doubt is can JVM free the memory physically?

Why because Virtual machine will be allocated virtual copies of resources of the system. (I read this in Virtual Machine's concepts).

Originally posted by Paul Sturrock: Physical memory management is the domain of the operating system. It might free the memory up in physical memory; it should free the memory in virtual memory.

(For the Sun JVM...)

When garbage collection happens, the JVM usually does not free any virtual memory. The Java heap is some virtual memory that the JVM has obtained from the OS, and the JVM mostly works with this constant amount of heap, regardless of how much of the heap is actually used by active Java objects.

Depending on some settings (-XXMinHeapFreeRatio), the JVM does ask for more virtual memory from the OS for the Java heap, when the Java heap nears fullness with active objects. Depending on some other settings (-XXMaxHeapFreeRatio), the JVM may occasionally release some of the Java heap, if it is getting too empty. [ May 24, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.

I don't think so. Physical memory management is the domain of the operating system.

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970

posted May 24, 2007 03:03:00

0

Originally posted by vilas lawande: yes JVM can free physical memory

Not in the Sun JVM, it can't. The JVM allocates and frees virtual memory. The OS deals with physical memory.

I suppose someone could create a JVM that had hooks into the OS VM system, so it could work with physical memory. Can't imagine why they'd bother, though.

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970

posted May 24, 2007 03:06:00

0

Originally posted by vilas lawande: No garbage collector can not free unused menory. It will free only object's , those are not refered by any reference veriable of class

I suppose this could be considered true, if you take a very narrow view of the definition of the Garbage Collector. However, the (Sun) JVM can and does free unused virtual memory eventually, if the various -X and -XX settings allow it to do so. [ May 24, 2007: Message edited by: Peter Chase ]

Depending on some settings (-XXMinHeapFreeRatio), the JVM does ask for more virtual memory from the OS for the Java heap, when the Java heap nears fullness with active objects. Depending on some other settings (-XXMaxHeapFreeRatio), the JVM may occasionally release some of the Java heap, if it is getting too empty.

The default value for MaxHeapFreeRatio is 70%, so if more than 70% of the heap are unused, the JVM is supposed to release virtual memory to the operating system.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

I think it will free the unused memory from JVM means the object or variable whose refernce variable loose from heap or if they are reference to null then they are eligible for garbage collection...You cant force to jvm to free memory from unused data, there is no gurantee it will remove unused data..Its up to the jvm to make it garbage..