Author | Topic: GC question.......... |
eram ranch hand |
posted March 18, 2000 08:41 AM
Q1. gc mechanism cannot be forced, Then what am I doing with the code System.gc() ? Is it that, I am forcing the mechanism explicitly , but cannot gaurantee the results that gc will ever run, bcos it runs in a low priority thread and may never get a chance to run. Q2. Is it that referencing an object by null will always make it ready to be garbage collected(atleast at that point or line) even if it gets another reference down in another line. For e.g : Answers: I think line 6. any other answers & explanations, please welcome.
|
maha anna bartender |
posted March 18, 2000 09:53 AM
eram, From your qstn, I understand that you are using a loose language for the object ref and the actual object itself. The point you have to clear first is , 5. Important point is Only objects are garbage collected NOT object references But the refernce var 'str' still exists. It can hold another object's memory address as it's content. This 'str' ref is NOT GC'd. When (at what line) is Object reference s2 gced 1. String s1 = new String("java"); There are some other mechanism through which the before object is garbage collected, it can be made to have another ref to it.By overriding,protected finalize() throws Throwable method. But this concept doesn't come into picture here, because String class is final class . You can't subclass and override the finalize() method . So once a String object is set to 'null' means it for GC. There is no way to bring it back again. (unless it is a String literal in the String pool and this discussion is not needed for SCJP2 cert ) regds
|
Jim Yingst sheriff |
posted March 18, 2000 11:55 AM
Good answer M.A. (as usual). Regarding the last paragraph, I just wanted to note that for those of you interested only in certification, you don't need to worry about that. So anyone who's confused by the last paragraph, don't worry about it for now. If you're confused by the previous paragraphs though, study them carefully, because they're important to understand. As for the original question Q1: you can force part of the garbage collection mechanism to run (namely the gc() method itself), however it may abort early without going through the extensive process of finding all referenced objects in memory and then marking the unreferenced objects for deletion. And even if it does go through this process, it may not find any available memory. So basically, you can "force" the system to try to do garbage collection, but you cannot force it to get results. The low-priority gc thread is something else, unrelated to what happens when you run System.gc(). That refers to the asynchronous gc - the fact that even if there is no System.gc() call and there is no problem currently with low memory, the garbage collection will be attempted periodically by the system because there's a low-priority thread that makes it happen. If you invoke gc() directly (or if the system invoked it because it was otherwise about to throw an OutOfMemoryError) then it's not necessarily low-priority. (In the last case, it better be highest priority, since otherwise the system's about to crash.)
|
eram ranch hand |
posted March 18, 2000 04:56 PM
Thanks Maha & Jim. Nice explanations.
|
Steve Butcher greenhorn |
posted March 21, 2000 01:15 PM
Maha anna, With regard to the statement: only objects are gc'd not object references. What about: ... when v is finally gc'd aren't the object references that were the Vector instance gc'd as well? When the object that owns this code is gc'd aren't o1, o2 and v gc'd, too? Or is this a stack/heap thing that always confuses me? I'm not trying to pick nits, it just seems that on the first read the statement appears correct but the more I thought about it the more it seemed incorrect although a good explanation within the context of eram's question...the question is, how do you express that context so that the statement is correct (if it is indeed incorrect). Steve [This message has been edited by Steve Butcher (edited March 21, 2000).]
|
Jim Yingst sheriff |
posted March 21, 2000 02:06 PM
Steve- you are correct. I suppose to be correct we should say that variables - references and primitives - are only collected when the object or class that contains them is collected. Good point.
|
| | |