• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does Java Garbage Collector free the unused memory

 
Kishore Vallabhajosyula
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vilas Lawande
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes JVM can free physical memory
regards
vilas
 
Vilas Lawande
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brijesh shah
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic