3

As I know when I run garbage collection using System.gc() method it will collect all the unused and undestroyed objects from the heap and clean it. So when I run the System.gc(); the memory of the JVM should be increased. but in the below code when I run it, the result is confusing me.

Runtime rs = Runtime.getRuntime();
System.out.println("Free memory in JVM before Garbage Collection = " + 
                   rs.freeMemory());
rs.gc();
System.out.println("Free memory in JVM after Garbage Collection = " +
                   rs.freeMemory());

the output is

Free memory in JVM before Garbage Collection = 12184624
Free memory in JVM after Garbage Collection = 12184360    

see the before value is greater than after value.

Please correct me if I in a wrong concept or explain me why this happen.

Thank you.

maXfenda
  • 214
  • 3
  • 10
  • 1
    For reference, many [profilers](http://stackoverflow.com/q/2064427/230513) can [`ForceGarbageCollection`](http://stackoverflow.com/a/14492574/230513). – trashgod Aug 08 '13 at 09:02

5 Answers5

6

Calling for garbage collection in Java is merely a suggestion to the JVM. The way that garbage is collected is technically 100% out of your control. Calling System.gc() politely suggests that the trash should be taken out, but Java is within its rights to ignore you completely like a disobedient child.

Although it will USUALLY do as you ask, it won't always and you certainly can't rely on it.

Kon
  • 10,702
  • 6
  • 41
  • 58
  • 2
    For reference, http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#gc() – Daniel Williams Aug 08 '13 at 04:20
  • 2
    It's too bad System.gc() doesn't return a boolean so you can whip the child into shape by threatening to System.exit() if it doesn't do its chores. The JVM is so passive-aggressive with its silent protests sometimes. – Jason C Aug 08 '13 at 04:22
  • 1
    Some VMs are such slobs that they don't even have trash cans; they just leave smelly objects lying all over the place until they die! – chrylis -cautiouslyoptimistic- Aug 08 '13 at 05:23
  • 1
    For the current implementations of garbage collectors I doubt that the call to `Runtime.gc()` is usefull anymore. I think that the call is ignored by most current collectors. – Uwe Plonus Aug 08 '13 at 05:59
1

Garbage collection is done automatically as the system sees fit, in addition to any System.gc() requests. There is no guarantee that there is any memory that can be freed at the time of a request.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
1

Runtime.gc() is more of a hint to the GC to expend some extra effort towards freeing memory and may not actually free any in practice, but your surprising decreasing value of free memory may very well instead be because of Runtime.freeMemory not giving an accurate result;

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

In other words, it's not an exact number, and the approximation may very well have improved by the GC analyzing the memory contents even if none was freed.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

From the Javadocs,

public void gc()

Runs the garbage collector. Calling this method suggests that the Java virtual machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects. The name gc stands for "garbage collector". The virtual machine performs this recycling process automatically as needed, in a separate thread, even if the gc method is not invoked explicitly.

The method System.gc() is the conventional and convenient means of invoking this method.

Says that the gc won't clean when you call it but it adds it the JVM's queue and the JVM will do that if it seems needed otherwise ignored.

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
0

You as Java programmer can not force Garbage collection in Java. By calling System.gc(), it will only trigger if JVM thinks it needs a garbage collection based on Java heap size. There are methods like System.gc() and Runtime.gc() which is used to send request of Garbage collection to JVM and it internally uses mark-and-sweep algorithm to determine which objects are no longer in use and need to be collected as garbage but it’s not guaranteed that garbage collection will happen just at the time when you call System.gc() or Runtime.gc(). Moreover it also depends whether there are some objects eligible for garbage collection at the time when you call System.gc() or Runtime.gc(), if not so there will be no effect of calling System.gc() or Runtime.gc() at that moment.

Source:(You can read more here)

http://javarevisited.blogspot.in/2011/04/garbage-collection-in-java.html

http://javabook.compuware.com/content/memory/how-garbage-collection-works.aspx

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

Shailesh Saxena
  • 3,472
  • 2
  • 18
  • 28