简体   繁体   中英

Java Long Running Thread cause memory leak?

I have been reading about this article:

Since I myself have issues with threads:

Does this mean that Long running threads in Java will create memory leaks? What if I need to have a long running thread in the first place. Isn't most application long running threads also?

Long running thread doesn't create memory leak. It is what you do inside it. Technically memory leaks happens when garbage collector could not collect free space, as the space is marked as being used. ie if references are held.

Now in a long running thread, you could have an object reference present for the lifetime of the thread. This object itself could be expensive. This is the case in first link you shared(threadlocal holding transitively all the references)

On your second link , the problem seems to lie somewhere. Here what I generally do if I suspect memory leak

  1. Use jmap to get count of each class instances
  2. Force full GC
  3. Again count the instances of each class, these are the objects GC was not able to clean

Repeat multiple times, you will notice some objects, which should have been cleared. This will give you some idea. Following those references in code you can get some idea.

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