This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
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

Question about garbage collection

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two qestions about gb:
1)
/***********************************/
public class Test{
public static void main(String[] args) {
Object a = new Object();
Object b = new Object();
Object c = new Object();
Object d = new Object();
d=c=b=a;
d=null;

}
}
/***********************************/
How many objects will be eligible for garbage
collection after line d=null;?

2)
If object obj1 is accessible from object obj2
and object obj2 is accessible from object obj1,
then obj1 and obj2 are not eligible for garbage
collection.
I thought the statement was right and why is
it wrong? Why circular references do not prevent
objects from being gargage collected?
Thanks for help
Kevin
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1)
/***********************************/
public class Test{
public static void main(String[] args) {
Object a = new Object();
Object b = new Object();
Object c = new Object();
Object d = new Object();
d=c=b=a;
d=null;

}
}

d=c=b=a;
The above line makes all the four variables reference the same object - object a;
It means the other three objects which were originally referenced by the variables d,c & b become unreachable.
So they are subject to garbage collection.
"d=null" does not have any effect on the number of objects to be garbage collected because even though d is set null, the other three variables still have a reference to the object a;

/***********************************/
2)
If object obj1 is accessible from object obj2
and object obj2 is accessible from object obj1,
then obj1 and obj2 are not eligible for garbage
collection.
I thought the statement was right and why is
it wrong? Why circular references do not prevent
objects from being gargage collected?
The answer to the previous question should explain this.
 
kfu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vasansrini, Thanks for the help.
Kevin
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
d=c=b=a;
means i think here all objects are refering to d .
as all assignements are done from right to left.
I am bit confused here. Please help.
- Thanks
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got the order right. They are all referencing a, not d.
Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the sentence d=c=b=a;;
all the 3 references [d,c,b] are pointing to the same Object to which reference a is pointing.soon after this line execution the 3 objects which were earlier referenced by b,c,d will be eligible for garbage collection cuz Norefrence is referencing to them and hence their referece counter dropped to ZERO.This is TRUE even after d= null ; though it is redundant and does not have any effect on garbage Collection.
 
reply
    Bookmark Topic Watch Topic
  • New Topic