• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage collection question  RSS feed

 
Arun Bommannavar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say I have a class "TestGC", which has a single String variable, an ArrayList and has implemented PropertyChangeListener. Whats the prefered way to set this class for garbage collection? I'll be making another instance of TestGC anyway.

For example, a class called "Test" will make an instance of "TestGC". Test listens to some changes in TestGC, so I have addPropertyChangeListener in Test.

So for best garbage collection are the following steps enough?

Option 1.
Step 1. In Test remove listener on TestGC.
Step 2. TestGC=null;
Step 3. Make another instance of TestGC

Option 2.
Step 1. Set the variable in TestGC to null;
Step 2. Clear all elements from ArrayList
Step 3. In Test remove listener on TestGC.
Step 4. TestGC=null;
Step 5. Make another instance of TestGC

Option 3.
Step 1. Just make another instance of TestGC.

Thanks in advance,

Regards,
Arun Bommannavar
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say option 3. Once an object has no references for it, it is eligible for garbage collection.

"a class called "Test" will make an instance of "TestGC"."

TestGC is a reference of Test not an instance.

Test TestGC = new Test();

...

TestGC= new Test();//the previous object that TestGC pointed to is now elibilble for GC provided that no other references point to it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!