• Post Reply Bookmark Topic Watch Topic
  • New Topic

garbage collector  RSS feed

 
yaswanth yash
Greenhorn
Posts: 12
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what makes an Object eligible for Garbage Collection?
And after how much time the garbage collector comes again for Garbage Collection?
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, everyone's favorite topic.

I predict you'll be advised to avoid letting yourself become obsessed with this, and that will be good advice. However, as a Java programmer who had to go into rehab before I stopped thinking what I knew about C++ was relevant to my Java experiences, I sympathise with any new coder's concerns about the mysterious Garbage Collector.
yaswanth yash wrote:what makes an Object eligible for Garbage Collection?

The short answer is that an object is eligible when there are no references to it anywhere. If it's a local variable that you haven't assigned to anything at a higher scoping level, that's when the method that created it exits, or when all variables holding a reference to it are set to something else.

And after how much time the garbage collector comes again for Garbage Collection?

This question does perplex a lot of people, partly because the best answer appears to be, "You have no way of knowing, you will never have any way of knowing, you cannot make this happen yourself at any particular time, and now you had better just start getting used to it." Those of us who are rightfully proud of our abilities to pair of every malloc() call with a matching free() somewhere find this very hard to accept. However, you really just have to accept it. The Garbage Collector is like The Force: you trust it, and just let go.
 
yaswanth yash
Greenhorn
Posts: 12
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test {
Test t1 = new Test(); // line 1
public static void main(String[] ar) {
new Test(); // line 2
Test t2 = new Test(); // line 3
}
}


hi stevens,
can you tell me,
After how much time the objects created at lines 1,2,3
are made eligible for garbage collection??

And do you mean to say that making an object eligible for garbage collection means a dereferencing that object ?!
 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
None of them are ever made available for garbage collection.

Line (3) is never executed because line (2) goes into an loop in which Test objects are partially created until the program crashes with a stack overflow exception.

So as Stevens implied, I would suggest that garbage collection is a topic best learned after you learn basic Java programming. Don't try to learn it first.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yaswanth yash wrote:public class Test {
Test t1 = new Test(); // line 1
public static void main(String[] ar) {
new Test(); // line 2
Test t2 = new Test(); // line 3
}
}


hi stevens,
can you tell me,
At what time the objects created at lines 1,2,3 in this program
are made eligible for garbage collection??


Good Gracious, yaswanth, what are you trying to do here??? As Paul said, no object created by this code will ever be eligible since none of them ever lacks a reference, because that "new Test()" call results in an infinite sequence of Test objects creating more Test objects. (To be a bit of a nit-picker about it, I wouldn't say it "goes into a loop," but that it results in infinite recursion, which Paul clearly knew when he warned that the program will crash with a stack overflow.)

And do you mean to say that making an object means a dereferencing that object ?!


If I understand your question, the answer is "yes." When there are no outstanding references to an object, it is eligible for garbage collection.

Paul's right. Learning about the GC can wait. Mostly, the only thing to learn about it is that there really isn't anything to learn about it. As a new Java programmer, I found myself very concerned about it, thinking that maybe I needed to call it explicitly here and there, to be sure my program wasn't using up memory for no reason. Well, it turns out that the people who wrote the GC knew what they were doing. The GC will always be called if your application tries to get more memory than it can, so don't worry that you will ever run out of resources for lack of calling it yourself. I'm no GC expert, but I believe it also tries to hold off running while your application isn't blocked. That is, it runs when it really should run, and not when it really shouldn't. And, like I said before, you simply cannot force it to run; best you can do is ask it to run. Hard as it may seem, the bad news is that you just have to trust it. The good news is that it is worthy of your trust.
 
yaswanth yash
Greenhorn
Posts: 12
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:
yaswanth yash wrote:public class Test {
Test t1 = new Test(); // line 1
public static void main(String[] ar) {
new Test(); // line 2
Test t2 = new Test(); // line 3
}
}


hi stevens,
can you tell me,
After how much time the objects created at lines 1,2,3 in this program
are made eligible for garbage collection??


Good Gracious, yaswanth, what are you trying to do here??? As Paul said, no object created by this code will ever be eligible since none of them ever lacks a reference, because that "new Test()" call results in an infinite sequence of Test objects creating more Test objects. (To be a bit of a nit-picker about it, I wouldn't say it "goes into a loop," but that it results in infinite recursion, which Paul clearly knew when he warned that the program will crash with a stack overflow.)

And do you mean to say that making an object eligible for garbage collection means dereferencing that object ?!


If I understand your question, the answer is "yes." When there are no outstanding references to an object, it is eligible for garbage collection.

Paul's right. Learning about the GC can wait. Mostly, the only thing to learn about it is that there really isn't anything to learn about it. As a new Java programmer, I found myself very concerned about it, thinking that maybe I needed to call it explicitly here and there, to be sure my program wasn't using up memory for no reason. Well, it turns out that the people who wrote the GC knew what they were doing. The GC will always be called if your application tries to get more memory than it can, so don't worry that you will ever run out of resources for lack of calling it yourself. I'm no GC expert, but I believe it also tries to hold off running while your application isn't blocked. That is, it runs when it really should run, and not when it really shouldn't. And, like I said before, you simply cannot force it to run; best you can do is ask it to run. Hard as it may seem, the bad news is that you just have to trust it. The good news is that it is worthy of your trust.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yaswanth yash wrote:(lots of quoted text and nothing else)

Was there a question there?

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!