4

The following snippet from RuntimeUtil.java from jlibs guarantees GC that garbage collection is done.

Since, it also uses System.gc(), i dont understand how can they guarantee that it will happen 100% of the times.

Following is the snippet:

/**
     * This method guarantees that garbage collection is
     * done unlike <code>{@link System#gc()}</code>
     */
    public static void gc(){
        Object obj = new Object();
        WeakReference ref = new WeakReference<Object>(obj);
        obj = null;
        while(ref.get()!=null)
            System.gc();
    }
Chander Shivdasani
  • 9,878
  • 20
  • 76
  • 107

3 Answers3

5

Its related with Strong Reference & Weak Reference with respect to Garbae Collection.

A strong reference is an ordinary Java reference, the kind you use every day.

If an object is reachable via a chain of strong references (strongly reachable), it is not eligible for garbage collection. As you don't want the garbage collector destroying objects you're working on, this is normally exactly what you want.

A weak reference, simply put, is a reference that isn't strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector's ability to determine reachability

The gc() method in the pointed class works on this concept.

     /**
       * This method guarantees that garbage collection is
       * done unlike <code>{@link System#gc()}</code>
       */

       public static void gc(){
            Object obj = new Object();
            WeakReference ref = new WeakReference<Object>(obj);
            obj = null;
            while(ref.get()!=null)
                System.gc();
        }

Once a WeakReference starts returning null, the object it pointed to has become garbage and the WeakReference object is pretty much useless. This generally means that some sort of cleanup is required;

That's why they guarantee that it will happen 100% of the times.

Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
4

From what I can see it should work as they

1) create an object 2) get a "weak" reference to it 3) null the reference to it, in order to mark it for garbage collection 4) and wait with the while loop until it actually disappears

2

A single call to System.gc() does not guarantee all objects that are eligible for garbage collection are re-claimed. See How to cause soft references to be cleared in Java?

In this case, the garbage collection is run many times till a an weak object ref is returning null. I doubt efficacy of this approach. There will be many other objects that may be garbage collected. To me it (jlib) does not have any thing to learn from.

For example the code in the library written in 2009. (extracted)

/**
     * This method guarantees that garbage colleciton is
     * done after JVM shutdown is initialized
     */
    public static void gcOnExit(){
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run(){
                gc();
            }
        });
    }

Why to you need gc when shutdown is called? The library quoted in question is pure garbage.

Community
  • 1
  • 1
Jayan
  • 18,003
  • 15
  • 89
  • 143