I'm currently trying to fix a memory leak in my program and my brain starts to throw OutOfOptions Exceptions. The program creates a lot of objects and I dereference them with object = null
as soon as they aren't needed any more, but somehow the memory usage keeps still growing over time. Now I'am asking myself if I have to dereference variables inside an object as well.
For example:
public class Pojo {
private String foo;
private Integer bar;
private AnotherPojo anotherPojo;
public Pojo(String foo, Integer bar, AnotherPojo anotherPojo) {
this.foo = foo;
this.bar = bar;
this.anotherPojo = anotherPojo;
}
}
// ...
Pojo pojo = new Pojo("foo", 123, new AnotherPojo())
// ...
Is it enough to do pojo = null
or do I also have to dereference every variable separately in the Pojo object?