链表内存泄漏 [英] LinkedList Memory Leak

查看:291
本文介绍了链表内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试挂起的应用程序.该程序使用通过LinkedList实现的队列,在压力测试期间,我发现该程序由于堆内存不足而停止响应.我分析了堆转储,发现内存似乎从LinkedList泄漏.

I am attempting to debug an application that is hanging. The program uses a queue implemented with a LinkedList, and during stress testing I've discovered that the program stops responding due to running out of heap memory. I analyzed the heap dump and found that memory seems to be leaking from the LinkedList.

堆转储的相关部分:

▶java.net.Vectior @ 0xff5eacd0
▶▶java.util.Vector @ 0xff629f30
▶▶▶java.lang.Object[1280] @ 0xff629f50
▶▶▶▶class com.itnade.vsm.staticobject.TrapQueue @ 0xff6b23e8
▶▶▶▶▶java.util.LinkedList @ 0xff6b2460
▶▶▶▶▶▶java.util.LinkedList$Node @ 0xfb954560
▶▶▶▶▶▶java.util.LinkedList$Node @ 0xfb959968
▶▶▶▶▶▶java.util.LinkedList$Node @ 0xfb95ede8
▶▶▶▶▶▶java.util.LinkedList$Node @ 0xfb964230
▶▶▶▶▶▶java.util.LinkedList$Node @ 0xfb969638
 ...
 ...

从转储中可以看到,LinkedList$Node不会被删除并累积.

As you can see from the dump, the LinkedList$Nodes are not deleted, and accumulate.

程序的一般流程是:

      Queue.offer()→ Queue.poll→ Queue.remove(object)

      Queue.offer() → Queue.poll → Queue.remove(object)

为什么LinkedList似乎正在泄漏内存,如何防止这种情况发生?

Why does the LinkedList appear to be leaking memory, and how can I prevent this from happening?

推荐答案

根据显式删除对象将无法解决您的问题……基本上是因为在Java中无法做到这一点.

Your problem won't be solved by explicitly deleting objects ... basically because there is no way to do that in Java.

'如果您确实为CMS收集器创建了太多垃圾,那么唯一的解决方案是使用对象池来回收缓冲区对象,而不是将它们放在地板上让GC处理.但是,您需要注意不要将当前的问题替换为其他问题:

'If you are really creating too much garbage for the CMS collector to cope with, then the only solution is to use an object pool to recycle the buffer objects instead of dropping them on the floor for the GC to deal with. However, you need to be careful that you don't replace your current problem with others:

The recycled objects may need to be "cleaned" (zeroed).
A poorly designed object pool can be a memory leak.
A poorly designed object pool can be a concurrency bottleneck.
A poorly designed object pool can increase GC overheads, especially if you are running with a heap that is too small.

另一方面,您的真正问题可能是您的堆对于尝试运行的应用程序而言太小了.如果您运行的次数接近极限,则GC不会每次都回收大量垃圾.由于运行GC的成本与非垃圾的数量成正比,因此很容易看出,随着堆越来越接近满满,GC的效率是非线性的.'

On the other hand, your real problem may be that your heap is too small for the application you are trying to run. If you run too close to the limit, the GC won't reclaim much garbage each time. Since the cost of running the GC is proportional to the amount of NON-garbage, it is easy to see that the GC's efficiency is non-linear as the heap gets closer to full.'

您可以通过以下代码在代码点中调用垃圾收集器: 'system.GC();'

you can call garbage collector in a point of code by following code: 'system.GC();'

这篇关于链表内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆