File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Possible Memory Leak ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Possible Memory Leak ?" Watch "Possible Memory Leak ?" New topic
Author

Possible Memory Leak ?

Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
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


Himanshu Jhamb<br />SCJP2 v1.2 (April 2002)<br />SCJP2 v1.4 (May 2002)
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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 ]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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.


Dave
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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!
Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Possible Memory Leak ?