Forums Register Login

Possible Memory Leak ?

+Pie Number of slices to send: Send
Hi.
Consider the following code...
public void f1()
{
StringBuffer strbuf = null;
try
{
strbuf = f2(1);
}
catch(Exception e)
{
... // Some code
}
}
public StringBuffer f2(int i)
{
StringBuffer strb = new StringBuffer();
... // Some code that operates on i and puts it into strb
return strb;
}
My Question --> Once the method f2 returns, variable strb goes out of scope. Isn't this a candidate for introducing a memory leak ??? In other words, we are trying to return a variable's reference, which has just gone out of scope?
One possible reason for it not causing a memory leak can be that although the reference is unreachable once f2() returns, it is not null, hence still not a candidate for GC. But, I am not convinced... somehow.
All comments are invited...
thx
+Pie Number of slices to send: Send
Because StringBuffer actually provides an implementation for toString, I've created a very simple example using an inner class.

From the output, I think you can see that the return keyword will return either a primitive value (not demonstrated here), or an object reference (see below). So in fact, the object created within your method f2 is still 'alive', but the variable inside f2 is the only thing that goes out of scope.

Note from the output, that the outside variable (obj) now refers to the object created on the inside. (formerly refered to by foo) This object is not eligible for GC quite yet. Something does indeed still reference the object.
[ October 15, 2002: Message edited by: Mike Curwen ]
+Pie Number of slices to send: Send
Himanshu
Please do not cross post your questions to multiple forums. Most of the visitors here read in many of the forums and it is frustrating to see the same question repeated over and over. You have a better chance of getting a correct and timely answer if you post it to the most appropriate forum instead of using the shotgun approach and hoping you'll hit something.
+Pie Number of slices to send: Send
The other thing I should clear up is in the language you used, which might cause some difficulty:

you said:
although the reference is unreachable once f2() returns, it is not null, hence still not a candidate for GC

References go out of scope, and objects are unreachable. The 'reference is unreachable' is not entirely sensical. And as pointed out... you return the reference to the object created within your method, so indeed the object is still reachable. This is why it's not GC'd

And yah, cross-posting bad!
+Pie Number of slices to send: Send
Ah ! Cannot escape the eagle eye of the moderators !
I plead guilty !
Apologize for cross posting. I was in a hurry, hence the incorrect language of the question as well as the cross posting.
I was not quite sure of where to post this question, really. I started off with posting it to the SCJP forum but then got paranoid that it might be the wrong one for this question... and then posted it to the Beginner's.
Pls. feel free to delete it from any of the forums... Indeed, we do not need copies of the same one.
You didn't tell me he was so big. Unlike this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 960 times.
Similar Threads
Does Java have buffer overflows and memory leaks?
Possible Memory Leak
Insert Unicode character
Equality of String objects
flushing in java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:13:16.