Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is this a java memory leak

update: looks like it's not a memory leak, would someone create on based on an extension of this example?
Original question: Suppose I create and starts a thread that does not terminate, the thread creates an object and references as long as it's alive. See the following code. Would the JVM garbage collect x? would this be considered a memory leak?

public class MyRunnable implements Runnable{

    public void run(){
      X x = new X();
      while(true){}
   }
}

Thread t = new Thread(new MyRunnable());
t.start();
like image 815
user121196 Avatar asked Jan 24 '13 10:01

user121196


1 Answers

The thread never terminates so the garbage collector will never free x. However if you never really use x it it might be optimized out. If you do use x this can not be a memory leak - you use the memory.

like image 94
Ivaylo Strandjev Avatar answered Sep 20 '22 08:09

Ivaylo Strandjev