Can using too many static variables cause a memory leak in Java?

前端 未结 5 1766

If my application has too many static variables or methods, then as per definition they will be stored in heap. Please correct me if I am wrong

1) Will these variab

5条回答
  •  不知归路
    2020-12-08 01:01

    Objects directly or indirectly referenced by statics will remain on the heap until the appropriate class loader can be collected. There are cases (ThreadLocal, for instance) where other objects indirectly reference the class loader causing it to remain uncollected.

    If you have a static List, say, and add references to it dynamically, then you can very easily end up with "object lifetime contention issues". Avoid mutable statics for many reasons.

提交回复
热议问题