I have a function which runs for a long time and I declare and assign an object inside that function. Now from what I think I know this object will live in memory atleast for as long as this function is running and after that it will be available for garbage collection if no other object references it. Now, I want this object to be available for garbage collection before even function is completed running. In the coding mannger:
public void foo(){
String title;
Object goo=getObject(); //getObject is some other function
//which returns Object and can be null and I want Object to be in memory from here
if(goo!=null)title=goo.getString("title");
//after the last line of code I want Object to be available for garbage
// collection because below is a long running task which doesn't require Object.
for(int i=0;i <1000000;i++) //long running task
{
//some mischief inside with title
}
}
Now from the above task what I want Object to be available for garbage collection before the for
loop starts. Can I do something like enclose two lines of code before for loop to be inside curly braces, If no what can I do to achieve this task ?