Stupid Question - losing my mind - Garbage Collection and methods..

J

johndesp

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;

}
 
A

Adam Maass

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;

}

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
 
R

Roedy Green

doSomething("whatever");


}


private boolean doSomething(String inme) {

String test = "";

test = inme;

boolean sendback = true;


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;
 
J

johndesp

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
 
R

ricky.clarkson

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.
 
I

IchBin

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-)
 
R

Roedy Green

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top