Results 1 to 17 of 17
Thread: Weird Garbage Collection...
- 12-19-2011, 10:57 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Weird Garbage Collection...
Hi everyone, here is the code:
Java Code:package myPackage; public class Book { boolean checkedOut = false; Book(boolean checkOut){ checkedOut = checkOut; } void checkIn() { checkedOut = false; } void checkedOut() { checkedOut = true; } protected void finalize() { if(checkedOut) System.out.println("Error: Checked out"); } }When I run this, I get different amount of "Error: Checked out" 's in the console every time I run it.Java Code:package myPackage; public class TerminationCondition { public static void main(String[] args) { for(int i = 0;i<100;i++) { Book novel = new Book(true); novel.checkIn(); new Book(true); System.gc(); } } }
Sometimes I get 100 lines, sometimes only around 20, sometimes around 70 or 80..
Can anyone explain step by step what this code is really doing and what causes this?
Thank you..
- 12-20-2011, 12:12 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: Weird Garbage Collection...
Think threading. The Garbage Collector runs in its own thread. When main exits the program shuts down stopping both threads - wouldn't be surprised to see variable output when you are outputting the results of a race between two threads, and stopping the race prematurely (when main exits).
To illustrate this, sleep the main thread after the for loop for a period of time (using Thread.sleep()) - say for a second, then check the consistency of the output.Last edited by doWhile; 12-20-2011 at 12:15 AM.
- 12-20-2011, 09:24 AM #3
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Weird Garbage Collection...
gc() is only a request to the VM to run the garbage collection. The system will determine if and how much it does. It will vary according to memory usage.
- 12-20-2011, 09:34 AM #4
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Weird Garbage Collection...
is gc() calling finalize?
- 12-20-2011, 09:51 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Weird Garbage Collection...
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-20-2011, 10:08 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
Re: Weird Garbage Collection...
I'd have to reread up on this, but I thought stuff with a finalise() was not necessarily gc'd on a collection run, but stuck off to one side so the finalise() could be executed. This means, though they are marked for gc, they are also marked as having a finalise so are not gc'd straight away (when they would have been without a finalise()).
In other words, finalise() will delay gc for eligible objects.
- 12-20-2011, 10:16 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Weird Garbage Collection...
Yep, finalize() postpones the final garbage collection, but before an object is ripped to pieces, its intestines removed and its eyeballs poked out, its finalize() method has definitely run once. The gc keeps a flag, indicating that a finalize() method has been run before; if so, it doesn't run again (to protect against resurrection). There is no guarantee that an object will ever be collected though (System.exit() is an example).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-20-2011, 10:59 AM #8
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
- 12-20-2011, 11:08 AM #9
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Weird Garbage Collection...
So what exactly is gc doing then ?
- 12-20-2011, 11:22 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Weird Garbage Collection...
Stop spreading vague rumours; you're not helping anyone with it; straight from the JLS:
Finalizers aren't called in any particular order, nor are they called if their memory need not to be reused (as for the System.exit() call). Read paragraph 12.6. and further for all the details.
Originally Posted by JLS
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-20-2011, 12:03 PM #11
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Weird Garbage Collection...
As I said, you cannot rely on finalize being called. The garbage collector does not necessarily collect everything, even when you call gc(). There is also a very complex model with complex categorization of references that the garbage collector uses in determining what to collect.
I don't know why you have called something a rumour and then posted confirmation!
- 12-20-2011, 12:06 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Weird Garbage Collection...
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-20-2011, 12:49 PM #13
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Weird Garbage Collection...
You learn to read. Here, I've highlighted it, just for you! Do you know the significance of that IF?
Read this. It may help you..
Cleanup: finalization and garbage collection
Now, if you have a useful comment, I am all ears.Your objects might not get garbage collected.
You might find that the storage for an object never gets released because your program never nears the point of running out of storage. If your program completes and the garbage collector never gets around to releasing the storage for any of your objects, that storage will be returned to the operating system en masse as the program exits. This is a good thing, because garbage collection has some overhead, and if you never do it you never incur that expense.
- 12-20-2011, 01:03 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 12-20-2011, 01:12 PM #15
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Weird Garbage Collection...
Reading between the lines, I am glad you have realised your misconception.
If, in future, you learn to cast you comments more thoughtfully, you will afford yourself the dignity of a graceful exit from your dilemma instead of skidding out on your nose.
My last word on this to you.
Cheers.
- 12-20-2011, 05:31 PM #16
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: Weird Garbage Collection...
Argument aside...
fatabass, if you have not yet, please read my advice in post 2. I ran your code with a delay after the for loop, and finalize() gets called exactly 100 times - every time. This result should answer your question, and if you have different results please do share. One might expect it to be called 200 times - I recommend the following reading (in particular, the Invisible section) :
http://java.sun.com/docs/books/perfo...PAppGC.fm.html
There are a lot of intricate details to garbage collection, and evidently it causes heated debate, but I would not recommend relying on the finalize method to accomplish important tasks...my .02
- 12-20-2011, 09:59 PM #17
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Similar Threads
-
Garbage Collection Pauses
By jdpl28 in forum Advanced JavaReplies: 0Last Post: 07-16-2011, 02:28 PM -
i m so confuse in garbage collection
By vivek6569 in forum New To JavaReplies: 3Last Post: 05-03-2011, 07:39 AM -
JVM Garbage Collection
By daromnet in forum Advanced JavaReplies: 4Last Post: 03-09-2011, 10:18 AM -
Garbage Collection Question
By DrMath in forum New To JavaReplies: 5Last Post: 10-03-2009, 10:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks