This is a java memory leak

update: it looks like this is not a memory leak, will anyone create based on the extension of this example?
The original subject: Suppose I create and start a thread that does not interrupt, the thread creates an object and links as long as it is alive. See the following code. Does JVM collect garbage x? Will 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();
+5
source share
4 answers

The thread never ends, so the garbage collector will never free x. However, if you never use x, this can be optimized. If you use x, this cannot be a memory leak - you are using memory.

+5

x , x = null. .

0

Will jvm x be garbage collected?

No, the cause of the cause still supports refrence.

Will this be considered a memory leak?

Actually not a leak, but a memory loss.

0
source

From the wiki

A memory leak can occur when an object is stored in memory, but cannot be accessed. So

would this be considered a memory leak?

NO this is not a memory leak.

Would jvm garbage collect x ?

NO, because the thread will never end.

0
source

All Articles