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