简体   繁体   中英

What happens to the Java Thread during a memory leak in the heap?

I understand that a memory leak in the heap is due to an object that is still reachable/referenced in the stack so the GC can't clean it. So my questions are:

  1. Does this means that there's still a Java Thread running that owns that Stack memory or not?
  2. If yes, running the same Java thread again would increase the memory leak or it re-uses the same objects in the heap since they are visible to all threads?
  3. If no, does the leak happens in both heap and stack memory as the objects in the heap have to be reachable by the stack to survive a GC?

I'm sorry for these simple questions but I'm having issues to find a clear answer to them. Thanks!

I understand that a memory leak in the heap is due to an object that is still reachable/referenced in the stack so the GC can't clean it.

That is only one possible cause of memory leaks. Another is that there is are unwanted references in a static variables. And possibly others as well if you dig deep enough. (For instance, you can create a Java heap memory leak in native code, or by mismanaging classloaders or direct memory buffers, or by running too many threads.)

1) Does this means that there's still a Java Thread running that owns that Stack memory or not.

Well a Java thread's stack is removed when it terminates. So if a stack exists, that means that its thread has not terminated yet. But it may be running, waiting on a lock or blocked in an I/O operation.

When a thread terminates, any remaining references held on its stack would immediately become unreachable. But technically they became unreachable when the run() method call terminated.

2) If yes, running the same Java thread again would increase the memory leak or it re-uses the same objects in the heap since they are visible to all threads?

If a specific memory leak is caused by a thread, then running the thread again 1 would naturally leak more memory. On the other hand, since thread termination of a thread releases all of its references, if you rerun a thread after it has terminated, you don't compound the leak.

Java does not "reuse" objects. Each time you new a type (class or array) a brand new object is created. Always.

Objects in the heap are not visible to all threads. They are only visible to the threads that they are reachable from.

3) If no, does the leak happens in both heap and stack memory as the objects in the heap have to be reachable by the stack to survive a GC?

As I said at the start, there are ways to create a memory leak that don't depend on the stack. Objects don't need to be referenced via the stack to be reachable.


1 - I assume we are talking about creating and starting a new Thread with the same or equivalent Runnable as before. Technically, a Thread cannot be run (started) twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM