1

I have a theoretical question I can't find an answer to -
Assuming I have a singelton instance A with inner private member a and a public
setter, now let's assume I set this member from another instance of
class type B in a private method as a consequence to some event
i.e. if something happen I will call A.setA(a) from B's private method.


My question is -
Once the use in instance of class B is over and the instance of class A still "lives" in the system,
will the instance of class B get garbage collected?
That is if B use anonymous member to init A's a.

Thanks in advance.

Edit - Code example -

public class A {
    Object a;

    public void setA(Object a) {
        this.a = a;
    }
}

public class B {

    private void foo() {
        if(...condition) {
            A.getInstance().setA(new Object());
        }
    }

}

To further explain - the instance of class A is a singleton in the system, there is no other class referencing to the instance of class B and it's done it's part after setting A's private member

crazyPixel
  • 2,301
  • 5
  • 24
  • 48

2 Answers2

1

Any object becomes eligible to be collected as garbage as soon as it is no longer considered alive.

An object is alive when there is a reference to it from another life object. In other words: as long as that B object is referenced from somewhere, it can't be collected. See here for example.

It absolutely does not matter here what code within the B class is doing. The only thing that matters is: is the B object still referenced from somewhere. In that sense you should rather study how GC works in general, see here for example.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Finally! that's the answer I was looking for, the instance of class B is not references from anywhere else, it does an API call and by the server response calls decide if to call setA or not - judging by your answer once it's no longer in use it WILL get picked up by the GC. Thanks! – crazyPixel Jan 26 '18 at 13:19
  • @crazyPixel I’m not sure whether you really understood it, as in your comment, you are again talking about the `setA` call that has no relevance to the lifetime of the `B` instance. In principle, the instance of `B` can even get garbage collected while the `foo()` method is still being executed, simply because it is not used anywhere in that method. – Holger Jan 26 '18 at 16:17
  • 2
    We really need a canonical Q&A pair that can cover any "Will this object be GCed" question. – the8472 Jan 26 '18 at 20:51
0

It's clear from the basics of the GC standards: the garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program.

If there are private class attribute istantiated by a superclass, that will live as long as any of its relationship with used objects.

You can easily keep track of this implementing your own scenario and keep it monitored via jdk instruments suck as Java Mission Control

Black.Jack
  • 1,878
  • 2
  • 22
  • 39