0

I was wondering if the following code produced some sort of memory leak in Java. Anyways here's the code:

public class test {
 public static void main(String[] args){
 A a = new A();
 a.x = new A(new A(new A(new A(a))));
 a.x.x = null;
 }
}
class A{
 public A x = null;
 A() {}
 A(A a){
 x = a;
 }
}
8
  • Looks like a pretty standard linked list to me. Why would that have a memory leak? Commented Jan 16, 2016 at 5:50
  • Are you certain there's a leak? It looks fine. What's making you think where is one? Commented Jan 16, 2016 at 5:54
  • There's kind of a leak if you don't want to keep a.x. If you do want to keep it, then it's fine. Also there's no leak because a goes out of scope at the end of the method, and therefore ALL objects you created are eligible for collection, but I don't think that's what you wanted to ask about. Commented Jan 16, 2016 at 5:56
  • I made a copy paste error and its supposed to say a.x = new A(new A(new A(new A(a)))); Commented Jan 16, 2016 at 5:57
  • I was wondering if setting a.x.x to null loses the reference to the other x's within a.x's multiple x's if that makes any sense... Commented Jan 16, 2016 at 5:59

2 Answers 2

5

If you number the A's:

A a = new A();
// 1
a.x = new A(new A(new A(new A(a))));
// 2 3 4 5

Then you have a circular chain:

a → 1 → 2 → 3 → 4 → 5 → 1

When you break the circle using a.x.x = null, you get:

a → 1 → 2

Instances 3, 4, and 5 are now eligible for garbage collection.

Then, when main exits, a goes out of scope, and instances 1 and 2 are also eligible for garbage collection.

Note that program will likely end before GC has a chance to do anything.

answered Jan 16, 2016 at 6:19
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this response was very instructive and helpful.
0

There is no memory leak in that code.

When the a variable in main goes out of scope, all objects will be unreachable and eligible to be garbage collected.


I was wondering if setting a.x.x to null loses the reference to the other x's within a.x's multiple x's if that makes any sense...

Not a lot :-)

Proper memory management in Java does not depend on things not losing references to other things ... or not. The overarching rule is that if an object can no longer be reached by any executing code, it cannot influence the computation, and it is eligible to be garbage collected. The GC will deal with an unreachable object as, and when this is necessary ... but generally at the time of its choosing.

Memory leaks in Java actually occur because an object is reachable when you don't want them to be; e.g. when you "hang onto" objects by keeping links to them in a static collection.

answered Jan 16, 2016 at 6:17

1 Comment

Oh ok, that makes sense, and is very clear/informative, thank you!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.