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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With