aspose file tools*
The moose likes Beginning Java and the fly likes Does Java Garbage Collector free the unused memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Does Java Garbage Collector free the unused memory " Watch "Does Java Garbage Collector free the unused memory " New topic
Author

Does Java Garbage Collector free the unused memory

Kishore Vallabhajosyula
Greenhorn

Joined: Mar 07, 2007
Posts: 2
Hi All,

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).

Thanks in advance,
Kishore.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not really an JDBC question...


When I place System.gc() in my code, it will invoke garbage collection

No. Garbage collection may happen, but this is the preserve of the JVM. You cannot explicitly invoke the garbage collector.


My doubt is can JVM free the memory physically?

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.
Vilas Lawande
Ranch Hand

Joined: Nov 07, 2006
Posts: 127
yes JVM can free physical memory
regards
vilas
Vilas Lawande
Ranch Hand

Joined: Nov 07, 2006
Posts: 127
No garbage collector can not free unused menory.
It will free only object's , those are not refered by any reference veriable of class
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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

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
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
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 ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted 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
Brijesh shah
Ranch Hand

Joined: Mar 16, 2007
Posts: 90
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..
 
 
subject: Does Java Garbage Collector free the unused memory