2

As every java developers know java Objects will be garbage collected when no longer in use. I want to know how JVM identify which object have to pick for garbage collection. (say eg..If I have 10 objects. out of 10 if 2 objects are garbage collected how jvm finds that two object).

JVM uses "mark and sweep" algorithm(If im right).

1)For example i providing string object scenarios below

  class Sample{

     public static void main(Strings args[]){
      String s1=new String("10");
      String s2=new String("20");
      String s3=new String("30");
      String s4=new String("40");
      String s5=new String("50");
      String s6=new String("60");
      String s7=new String("70"); 

        s1=null; //now s1 is eligible for gc collection
        s2=null; //now s2 is eligible for collection


 }
}

//now s1 & s2 are eligible for gc collection.If i expicitly made to null references(s1,s2) become null but what happens to the memory allocated on heap? will it be freedup?

6 Answers 6

2

Actually nothing happens. The memory used by s1 and s2 will be reused only when GC starts working and this will only happen when JVM decides and it may never happen. In your case GC will hardly ever start.

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

Comments

1

It's covered fairly succinctly here: http://www.brpreiss.com/books/opus5/html/page424.html

The mark-and-sweep algorithm is called a tracing garbage collector because is traces out the entire collection of objects that are directly or indirectly accessible by the program.

The objects that a program can access directly are those objects which are referenced by local variables on the processor stack as well as by any static variables that refer to objects. In the context of garbage collection, these variables are called the roots . An object is indirectly accessible if it is referenced by a field in some other (directly or indirectly) accessible object.

So when you do s1=null; you are disconnecting the root, and the corresponding instance becomes eligible for collection.

The actual "collection" (freeing of the heap) occurs when the GC actually executes. As to exactly when this occurs there is not a one-size-fits-all answer to that. See What the frequency of the Garbage Collection in Java?

Comments

1

Assigning null to any reference doesn't free up the memory. It only makes the reference available to remove using garbage collector. Means now this reference allocated memory can be free when garbage collector will run.

2 Comments

If i dint made the references(s1,s2) to null. how JVM identifies that some objects are available for GC.
@Manu An object becomes eligible for Garbage Collection when no live thread can access it, Java GC knows it. And GC doesn't run frequently it hardly runs.
1

If any live thread can't access the object by any means then that object becomes eligible for garbage collection. But there is no guarantee of GC to run as it depends upon JVM internal logic and algorithm. Generally it happens when JVM thinks that its time to clear up some memory usage. In your case s1 and s2 are eligible to be GCed but we can't say when it will happen.

Comments

0

An Object becomes eligible for Garbage collection or GC if its not reachable from any live threads or any static references

SEE HERE

Comments

0

Java objects are eligible for garbage collection when the reference count of that object is 0. Reference count being 0 indicates that "that particular object is not referenced by any variable, hence it can not be used anymore". Garbage collector in the first pass mark all such objects whose reference count is 0 and in the second pass it sweeps all the marked object. Hence it is mark and sweep algorithm.

will it be freedup?

It depends on the garbage collector, when the garbage collector re-run after you made the s1 and s2 null, then they will be eligible for garbage collected. But, making reference null won't immediately release the object from the memory

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.