1

I have a following piece of code:

 List<String> list = new ArrayList<String>();
  //  WeakReference<List> wr = new WeakReference<List>(list);
    System.out.println(" before tot memory... " +  Runtime.getRuntime().totalMemory());
    System.out.println(" before free memory... " +  Runtime.getRuntime().freeMemory());
    for(int i =0; i<100; i++)
    list.add(new String("hello"));
    //System.gc();
    list = null; //forcefully end its life expectancy
    System.out.println(" after tot memory... " +  Runtime.getRuntime().totalMemory());
    System.out.println(" after free memory... " +  Runtime.getRuntime().freeMemory());
    System.out.println(" after memory used ... " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
   // System.out.println(" weak reference " + wr.get());

When I run the above code, I can see the free memory is 361064(In my system, however this value may vary)

But when I run the above code with System.gc() and comment list=null , I can see my free memory is coming(160944 in this case) less than the above test case. In both the scenarios, the objects are removed from memory.But why these values are different.

2
  • 1
    new String("hello") creates two objects. This constructor should be never use. Try list.add("hello") Commented Apr 6, 2012 at 8:34
  • @ajozwik I wouldn't say 'never'. There are valid use-cases for it. That's why it exists. Commented Apr 6, 2012 at 9:45

2 Answers 2

1

list = null; nullifying any reference will automatically cause to be garbage collected. When you comment this line, the reference list is still active, then even if you call System.gc() it will not be garbage collected.

When you call gc() explicitly, the references which are already nullified or the which are out of scope are only be garbage collected.

Sign up to request clarification or add additional context in comments.

6 Comments

if that is the case, what is the use of System.gc(). in which scenarios,it can be really used.:) Also, if list = null commented and run the application, what System.gc() call does because it frees or releases something
@UNNI gc() will free all those references which are nullified or out of scope. It will never clear those references which are active.
in the above test case when you run only with System.gc(), it frees some memory or some "value".In this case , what is THAT value is released because the list is still active,it will not be gced.
@UNNI System.gc() will of course frees some memory which cannot be referenced in your code. i think you need to read some article on garbage collector, google it or see javarevisited.blogspot.in/2011/04/…
why it is freeing some memory always (or atleast in this case)? not able to find perfect reasoning for this, though:)
|
0

GC works by looking at all of the objects in memory in order to find any objects which are no longer being referenced in the program. These unused objects can be deleted in order to make room for new memory objects.

So if any object is still being referred can not be garbage collected even if you call System.gc(). How can an object be garbage collected if you can refer it in your code?

By calling list = null the object that was being referred by list variable can't be referred again, so it is eligible to get garbage collected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.