Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

GC ... Explain Code....

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A progmmer has written the following class to prevent garbage collection of the objects of this class. Is he mistaken?

class SelfReferencingClassToPreventGC
{
private SelfReferencingClassToPreventGC a;
Object obj = new Vector();
public SelfReferencingClassToPreventGC()
{
a = this; //reference itself to make sure that this is not garbage collected.
}

public void finalize()
{
System.out.println("Object GCed");
}
}


Answer provided :

Explanation:
Yes, he is definitely mistake. Because, all he is creating is a circular reference, but such references do not prevent an object from garbage collection. Basically, if B is only refereded to by A and A is eligible for GC, then B iseligible for GC to. So, if A refers to B and B refers back to A, this arrangement does not prevent them from being garbage collected

Could anyone explain the actual concept ..
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hema..refer to island of isolation objects in KnB ..nice explanation was given ..
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GC Rule:

Collect any objects which are not reachable by any part of code. Try the below...

import java.util.*;

class SelfReferencingClassToPreventGC
{
private SelfReferencingClassToPreventGC a;
Object obj = new Vector();
public SelfReferencingClassToPreventGC()
{
a = this; //Cyclic Reference !!!. But this cant stop the object from being garbage collected.
}

public void finalize()
{
System.out.println("Object GCed");
}
}

class SimpleGCThread extends Thread
{
public void run()
{
System.out.println("I will collect the objects which are not reachable by any code");
while (true)
{
System.gc();
try
{
Thread.sleep(100);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
class GCTester {

void aMethod()
{
SelfReferencingClassToPreventGC aObj = new SelfReferencingClassToPreventGC();
}

public static void main (String args []) {
new GCTester().aMethod();
SimpleGCThread gcThread = new SimpleGCThread();
gcThread.start();

}
}
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,Lakshmanan
Look the follow code that i modified you code a litter .but can let everybody see lightly when and why the gc working.
import java.util.*;

class SelfReferencingClassToPreventGC
{
private SelfReferencingClassToPreventGC a;
Object obj = new Vector();
public SelfReferencingClassToPreventGC()
{
a = this; //Cyclic Reference !!!. But this cant stop the object from being garbage collected.
}

public void finalize()
{
System.out.println("SelfReferencingClassToPreventGC Object GCed");
}
}

class SimpleGCThread extends Thread
{
private SelfReferencingClassToPreventGC s ;
public void run()
{
System.out.println("I will collect the objects which are not reachable by any code");
while (true)
{
System.gc();
try
{
Thread.sleep(100);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public void setInstance(SelfReferencingClassToPreventGC sgc){
s = sgc;
}
public void finalize()
{
System.out.println("SimpleGCThread Object GCed");
}
}
class GCTester {

SelfReferencingClassToPreventGC aMethod()
{
SelfReferencingClassToPreventGC aObj = new SelfReferencingClassToPreventGC();
return aObj;
}

public static void main (String args []) {
SelfReferencingClassToPreventGC sgc = new GCTester().aMethod();
SimpleGCThread gcThread = new SimpleGCThread();

gcThread.setInstance(sgc);//if commented ,sgc will garbage collected.

gcThread.start();

}
}
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi enter,

Thanks for your input.

I modified the main method to have more FUN ))
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic