-1

duplicate - How to destroy java objects?

My question is very simple. I am new to app development with Java and am not sure whether I need to null objects after i am finished with them. for example i am using libGDX to create a game and I have several objects (Actors) being created. when I am finished with them do I simply call

obj.remove();

or should i -

obj.remove();
obj = null;

do I save memory by nulling objects or is there no advantage to be had?

Community
  • 1
  • 1
user2145312
  • 896
  • 3
  • 10
  • 33
  • You can check this link [How to destroy java objects?][1]. Explains everything you want to know. [1]: http://stackoverflow.com/questions/11404964/how-to-destroy-java-objects – Bibin Velayudhan Sep 18 '14 at 06:46
  • thanks, my issue is that i am creating global objects that are populated when i start the game, destroyed whilst playing and then re-populated when the user has another go. do I take it that when I call remove() and delete all referances in arrays etc that the garbage collector will then clean up after me automatically? -- **edit just read through some of the further reading suggested in the other post and I think i have my answer. – user2145312 Sep 18 '14 at 06:46
  • yes. GC will handle that and will free the memory. – Bibin Velayudhan Sep 18 '14 at 06:48
  • does anyone have an opinion/knowledge re Simulants answer re marking objects as null having a negative effect? – user2145312 Sep 18 '14 at 06:56
  • http://stackoverflow.com/questions/1848283/assigning-null-to-objects-in-every-application-after-their-use – Bibin Velayudhan Sep 18 '14 at 06:58

3 Answers3

2

No you do not need to null or manually delete objects. The java garbage collector will do this for you for any objects that have no pointers referencing them (when an object goes out of scope for example).

nmore
  • 2,474
  • 1
  • 14
  • 21
1

Generally, in java, marking the Object references as null is done to make it explicitly eligible for GC. If an object is unreachable, then it becomes eligible for GC, so, yes, you can mark it as null and let the GC do its work.

The Object will become unreachable only when there is no reference pointing to it.

example :

class MyTest {
    @Override
    protected void finalize() throws Throwable {
        System.out.println("object is unreachable..");
    }
}
// In some other class
public static void main(String[] args) {
        MyTest o1 = new MyTest();
        MyTest o2 = new MyTest();
        System.gc();
        o1 = null;
        System.gc();
        System.out.println("hello");
}
O/P:
hello
object is unreachable..

Here, you might have several thousand lines of code after "hello". You might want to make the GC's job easier by marking the object's references as null.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

Manually nulling Objects in Java is bad, because it is slowing down most garbage collection (GC) algorithims. The GC detects by itself wether an Object is reachable or not and then it gets removed. After nulling an object the space in memory is still used and only after the GC recycles the space it can be used again. So nulling objects does not free up your space immediately. Also starting the GC manually is a bad idea. It is started by the VM if it is needed.

Simulant
  • 19,190
  • 8
  • 63
  • 98
  • 1
    Why is manually nulling objects bad?. It won't slow down GC in any way. Although I agree with your second point - *starting the GC manually is a bad idea.* yes, it will affect the performance. – TheLostMind Sep 18 '14 at 06:55