what is the trick
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
member variable can be garbage collected and when a automatic variable is garbage collected.
It would be great help to every body as one question on it is always there.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
rest can you explain to me with example, reference to it is null
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I will try to explain the garbage collection thing:
First of all, garbage collection is vendor specific & is run as the lowest priority thread by the JVM (which is again vendor specific).
Since its run as the lowest priority thread, one can't predict when gc will be run. It is normally run when there are no more threads ready for execution.
The JVM keeps track of all the objects that have references & all that don't. So, whenever the gc is run (can't say when it is run), it will check out all those objects having no references & clear the memory used up by these objects.
Secondly, one can explicitly set the reference of an object to null, as follows...
Assume s1, s2 to be member variables :
1. String s1 = "Java Forum";
2. String s2 = s1;
3. s1 = null;
4. s2 = null
In step 1, a String s1 is created & allocated some memory to store the literal "Java Forum".
In step 2, another String s2 is created & it points to the same location where s1 points to. Hence, we say s2 contains the value "Java Forum".
In step 3, the reference s1 is set to null, that means s1 now DOES NOT point to the string "Java Forum". However, the reference s2 is still pointing to "Java Forum" or any other string.
In step 4, the reference s2 is set to null, that means s2 now DOES NOT point to the string "Java Forum" or any other string.
Hence, the strings s1 & s2 can be gc'd. In fact, the memory location where "Java Forum" is stored does not contain any references, hence that memory is freed.
Second point to remember is automatic variables are gc'd when they go out of scope.
One more important thing to note is that if a thread is created in a function or say in the constructor of an object & the start method is called, the JVM notes that it is a live thread & the thread is not gc'd until its run method is over. (When a thread's run method is over, the thread becomes dead & can be gc'd).
I hope its clear now.
Anybody would like to add something to this ?
Kavita
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Good notes Kavitha. !!
Sorry for being picky, but I would just like to comment on two of your statements -
First of all, garbage collection is vendor specific & is run as the lowest priority thread by the JVM (which is again vendor specific). Since its run as the lowest priority thread, one can't predict when gc will be run. It is normally run when there are no more threads ready for execution.
This is usually true, however because of the vendor dependency, it is better not to make any assumptions. There may be implementations of VM where GC is made a priority thread - VMs running on hardware where memory is very expensive, hand-held devices and PDAs for example. So let's just say GC is vendor specific and you cannot assume anything about the priority of the GC thread.
Second point to remember is automatic variables are gc'd when they go out of scope.
I would like to correct this statement and say, automatic variables become eligible for garbage collection when they go out of scope.

You can be assured you will not get very tricky questions about GC in the exam. The typical questions will be identifying when the object becomes eligible for garbage collection
Just my two cents worth ....

Ajith
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Your comments have helped me a lot in clearing my doubts about the garbage collectioin mechanism of java.
Thanks again.
with regards
raghav..
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I thought that when a threads run method is over, the thread object becomes dead, but cannot be gc'd. you can still access the Member variables of the thread through the Reference to the thread. The thread becomes eligible for gc when there is no active reference held for the thread, say by setting the thread to null. Correct me if I am wrong.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You are right. After the run method completes, the thread object is like any other object. Hence it too becomes eligible for GC only when there are no references pointing to it.
Ajith
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
In the explanation given by kavita, I want to know that whether the string references s1 & s2 get garbage collected or the string object referenced by s1 & s2.
Please clear my doubt.
thanks,
mana
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
That certainly helps. Because so far in all the books I have read, its mentioned that GC is run as the lowest priority thread. But, I never thought that there could be scenarios where it may differ.
Yes, the other thing about being "eligible" for garbage collection is also true. An object need not be gc'd, but it could be ready for being gc'd.
Thanks for sharing your thoughts about this one.
Originally posted by Ajith Kallambella:
Good notes Kavitha. !!
Sorry for being picky, but I would just like to comment on two of your statements -
[b]First of all, garbage collection is vendor specific & is run as the lowest priority thread by the JVM (which is again vendor specific). Since its run as the lowest priority thread, one can't predict when gc will be run. It is normally run when there are no more threads ready for execution.
This is usually true, however because of the vendor dependency, it is better not to make any assumptions. There may be implementations of VM where GC is made a priority thread - VMs running on hardware where memory is very expensive, hand-held devices and PDAs for example. So let's just say GC is vendor specific and you cannot assume anything about the priority of the GC thread.
Second point to remember is automatic variables are gc'd when they go out of scope.
I would like to correct this statement and say, automatic variables become eligible for garbage collection when they go out of scope.
You can be assured you will not get very tricky questions about GC in the exam. The typical questions will be identifying when the object becomes eligible for garbage collection
Just my two cents worth ....
Ajith[/B]
Kavita
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
A simple question, string literals are GC'ed ?
I'd read in some place i don't remember now that string literals are not GC'ed
Am I missing something?
Anyone comments are welcome! Thanks!
Regds.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
NOW ARE'T THESE TWO POINTS CLASHING , PLEASE EXPLAIN
AMIT
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Matt
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
angel custodio:
I read in somebody�s post that String literals may be GC'ed when the whole class is unloaded. If you are interested, you may try to find this message. But for the exam you do not need to know such subtle things. I remember Bill Brodgen said that regarding GC you should threat String objects like any other objects and to pay no attention whether they are literals or not. By the way, on new exam I had a question on GC, but objects were not of String class.
Amit Pasricha:
this article about GC and finalization is very good: http://www.artima.com/designtechniques/cleanup.html
- it helped me a lot�
Matt DeLacey:
Would they then be eligible for gc BEFORE they went out of scope?
- Interesting question� I wrote a small research program to investigate�
The output on my computer is:
Apparently the answer for your question is �yes�.
[This message has been edited by Mapraputa Is (edited October 19, 2000).]
Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
See if this makes things clear :
1. A local variable becomes eligible for gc when it goes out of scope.
A local variable is alive only in the method in which it is declared. Hence, once the method is over, the local variable sor of "dies", means it goes out of scope. Once it goes out of scope, it is eligible for gc.
2. When a local variable is set to null, it means that it no longer has any references to it. So, it is eligible for being gc'd. If the gc routine runs at the time when the local variable has no reference, it is gc'd.
In short, a local variable is ready for gc whenever
a. it goes out of scope
or
b. all references to it are set to null
whichever is earlier.
Got it ???

Originally posted by Matt DeLacey:
It has been said that local variables become eligible for garbage collection when they go out of scope. This sort of implies that there aren't eligible before then, but what happens if they are set to null before they go out of scope. Would they then be eligible for gc BEFORE they went out of scope?
Matt
Kavita
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-------------------
hi ,
In the explanation given by kavita, I want to know that whether the string references s1 & s2 get garbage collected or the string object referenced by s1 & s2.
Please clear my doubt.
thanks,
mana
-------------------
Om
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
> mamta's question is still unaswered?
Om, only objects get garbage collected. References are never gc'd.
| Well don't expect me to do the dishes! This ad has been cleaned for your convenience: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |







