java - Delete object's variable after instantiation -


so have object needs variables instantiated. these variables passed object through array of objects. then, each element in array gets assigned internal variable.

does array garbage collected after internal variables assigned , array never referenced again, or should manually done?

class myobject () {     public static object [] values;     public void setvalues(inputarray) {         values = inputarray;     } } 

memory kind of important because need create few hundred of these objects plus rest of code.

whether array eligible gc depends on condition:

  • is there still referencing array?

if, have this:

public class foo {     private int[] myarray = {1, 2, 3, 4};     yourobject obj;      public void somemethod() {         obj = new yourobject(myarray);     } } 

then myarray not eligible garbage collection because variable myarray in foo object still referencing it. can set myarray null make eligible gc.

if myarray local variable, however:

public class foo {     yourobject obj;     public void somemethod() {         int[] myarray = {1, 2, 3, 4};         obj = new yourobject(myarray);     } } 

then eligible gc after somemethod returns because myarray have gone out of scope then.

also, note "eligible gc" doesn't mean "will collected immediately". mean gc has possibility of collecting in future. when exactly? don't know.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -