Garbage collection example in java?

class Beta { } class Alpha { static Beta b1 ; Beta b2; } public class Tester { public static void main(String[] args) { Beta b1 = new Beta(); Beta b2 = new Beta(); Alpha a1 = new Alpha(); Alpha a2 = new Alpha(); a1.b1 = b1 ; a1.b2 = b1 ; a2.b2 = b2 ; a1 = null ; b1 = null; b2 = null; System.out.println(" Line 1 " + " a1 " + a1.b1); System.out.println(" Line 2 " + " a1 " + a1.b2); System.out.println(" Line 3 " + " a2 " + a2.b2); System.out.println(" Line 4 " + " a1 " + a2.b1); System.out.println(" Line 5 " + " b1 " + b1); System.out.println(" Line 6 " + " b1 " + b2); } } 

I'm not sure why only a1 is available for garbage collection in the above program. I expect a1, b1 and b2 to be collected by the garbage collector.

As you can see, a1, b1 and b2 were made NULL, therefore, this makes objects accessible for garbage collection. If the object is NULL or unreachable by a thread or reference variable, it must be collected by the garbage collector.

Can someone help me understand the intricacies of the above program with a good example and in a more accurate way? Your help is appreciated.

+4
source share
5 answers

Since there is a reference to the objects referenced by the images b1 and b2 due to the underlying lines.

 a1.b1 = b1 ; a1.b2 = b1 ; a2.b2 = b2 ; 

Let's pretend that:

  b1--->BetaObj1 b2---BetaObj2 a1---> AlphaObj1 a2---> AlphaObj2 

a1.b1 points to b1, which means that there is a link to BetaObj1 a1.b2 points to b1, which means there is another link to BetaObj1

(There are 3 links to BetaObj1 at this moment)

a2.b2 points to b2, which means there is a link to BetaOBj2

(There are 2 links to BetaObj2 at this moment)

a1=null; makes AlphaObj1 suitable for GC

b1=null; makes the reference counter BetaObj1 equal to 2, so this object is not suitable for GC

b2=null; makes the reference counter BetaObj2 equal to 1, so this object is not suitable for GC.

+7
source
  • b2 not available for gc because it has a2.b2 link
  • b1 not available to gc because Alpha.b1 contains a link to it ( Alpha.b1 is static, do not confuse because it installs with a1.b1 )
+1
source

You should get a NullPointerException here:

 System.out.println(" Line 1 " + " a1 " + a1.b1); System.out.println(" Line 2 " + " a1 " + a1.b2); 

At this point, a1 already set to null , so access to its member b2 cannot work, since it no longer refers to the object. (Access to static b1 works, since it only requires a class, not an instance.)

As you can see, a1, b1 and b2 were made NULL, therefore, this makes objects accessible for garbage collection.

No, you set some references to these objects to null , while other references to these objects exist, as in the case of b1 and b2 , these objects cannot yet be assembled.

+1
source

There are a few misconceptions here.

  • GC only works when necessary. To execute the GC more often than necessary, work will be performed that does not need it.
  • a1.b1 is actually Alpha.b1 , since this instance does not matter and can even be null using al.b1 confused.
  • a1.b2 should throw a NullPointerException because a1 is null .
  • Alpha.b1 installed but not deleted anywhere, so the object it refers to cannot be cleaned up. Other objects can be cleared, but without explicitly calling System.gc(); , it is very unlikely that you need to run it at the point you might expect.
  • a1 is cleared, but a2 not cleared that way (until the method returns)
+1
source

Garbage Collection Example

 public class GarbageCollection { public static void main(String s[]) throws Exception { 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()); } } 

Program output:

Free memory in the JVM before garbage collection = 62767488

Free memory in the JVM after garbage collection = 62854120

0
source

All Articles