I’m battling out of memory issues with my application and am trying to get my head around garbage collection. If I have the following code:
public void someMethod() {
MyObject myObject = new MyObject();
myObject.doSomething(); //last use of myObject in this scope
doAnotherThing();
andEvenMoreThings();
}
So my question is, will myObject be available for garbage collection after myObject.doSomething() which is the last use of this object, or after the completion of someMethod() where it comes out of scope? I.e. is the garbage collection smart enough to see that though a local variable is still in scope, it won’t be used by the rest of the code?
“Where it comes out of scope”