Guys, I have been working through a memory leak and need to go back to
square one for some understanding about have Garbage collection works
with java 1.4.x.
Will Garbage Collection clean up the String variable named test in the
method doSomething(String inme) ? I ask this stupid question because I
am seeing odd results. Also, Can you explain why? Thanks
Example:
inside...some servlet code...
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doSomething("whatever");
}
private boolean doSomething(String inme) {
String test = "";
test = inme;
boolean sendback = true;
return sendback;
}
Adam Maass - 26 Dec 2005 16:41 GMT
> Guys, I have been working through a memory leak and need to go back to
> square one for some understanding about have Garbage collection works
[quoted text clipped - 26 lines]
>
> }
Garbage collect collects objects that have no references to them.
(Exception: softly or weakly reachable objects might be collected -- but
that's another discussion.) Your method doSomething has two reference
variables: inme and test. inme is a reference to the actual parameter, the
string value "whatever". test initially starts as a reference to the string
value "", but is then re-assigned to the value of the variable inme. At this
point, the string "whatever" has two references to it, and the string value
"" has 0. "" might be eligible for garbage collection. When doSomething
returns, all of its references are cleared from the stack, and the string
value "" and the string value "whatever" are both eligible for garbage
collection. (That is, if they weren't string constants -- string constants
are special cases when it comes to garbage collection -- since the VM
guarantees that all string constants with the same value are actually
references to the same String object, they are held on to by the system for
far longer than other objects.)
What odd results are you seeing?
-- Adam Maass
Roedy Green - 26 Dec 2005 17:02 GMT
On 26 Dec 2005 06:34:16 -0800, "johndesp@gmail.com"
<johndesp@gmail.com> wrote, quoted or indirectly quoted someone who
said :
>doSomething("whatever");
>
[quoted text clipped - 9 lines]
>
>return sendback;
there are no strings to clean up. There are two string literals
"whatever" and "". They will be needed next time your method is run,
so they will not be GCed. Don't confuse references with String
objects.
Let us say instead you had written
doSomething( Double.toString(Math.sqrt( 50.0 )) );
private boolean doSomething(String inme) {
String test = "";
test = inme;
boolean sendback = true;
return sendback;
Now when you return, the test reference to the string will go out of
scope. Then the temporary will be no longer needed. At that point the
string will be eligible for GC.
I would also write your code in a more abbreviated form. You have two
pointless assignments:
String test = inme;
return true;

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
johndesp@gmail.com - 26 Dec 2005 17:22 GMT
Thanks Guys.. I am cleaning up my code and removing pointless
assignments. If there is a need in a method to instantiate a String,
will it eventually get cleaned up by the Garbage Collector?
thanks
ricky.clarkson@gmail.com - 26 Dec 2005 17:52 GMT
> If there is a need in a method to instantiate a String,
> will it eventually get cleaned up by the Garbage Collector?
It will get cleaned up sometime between the time when you finish using
it and the time when your program ends.
The important thing to know is that it will NOT be cleaned up before
you have finished using it, and it will NORMALLY be cleaned up before
you run out of memory.
If you are worried about memory usage, you can see it at runtime quite
well (I believe), using the Java Memory Profiler:
http://www.khelekore.org/jmp/
Otherwise, just don't worry about it. That's what garbage collection
is there for, so that you don't have to worry about freeing up memory.
IchBin - 26 Dec 2005 19:52 GMT
> If you are worried about memory usage, you can see it at runtime quite
> well (I believe), using the Java Memory Profiler:
> http://www.khelekore.org/jmp/
You can also use jconsole.exe that resides locally at JAVA_HOME\bin\
'Using JConsole to Monitor Applications'
http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Roedy Green - 26 Dec 2005 20:07 GMT
On 26 Dec 2005 09:22:48 -0800, "johndesp@gmail.com"
<johndesp@gmail.com> wrote, quoted or indirectly quoted someone who
said :
> If there is a need in a method to instantiate a String,
>will it eventually get cleaned up by the Garbage Collector?
All objects, including strings will be gced once nothing points to
them. You rarely instantiate a String with new. You normally create
one via concatenation (which is really just a StringBuilder.toString).

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 26 Dec 2005 17:06 GMT
On 26 Dec 2005 06:34:16 -0800, "johndesp@gmail.com"
<johndesp@gmail.com> wrote, quoted or indirectly quoted someone who
said :
> Also, Can you explain why?
Can I explain why your program is "doing something strange". Try
Kreskin for that. See
http://mindprod.com/jgloss/garbagecollection.html
http://mindprod.com/jgloss/packratting.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.