Java 垃圾过多问题

发布于 2024-10-12 09:22:40 字数 256 浏览 7 评论 0原文

我有一个应用程序,基本上,创建一个新的字节数组(小于1K),在几秒钟(通常不到1分钟,但有些数据存储长达1小时)后存储一些数据写入磁盘,数据将变成垃圾。每秒创建大约 400 个数据包。我读过一些文章,说不要担心 GC,特别是快速创建和释放内存部分(在 Java 6 上)。 GC 运行时间太长会导致我的应用程序出现一些问题。 我设置了一些GC参数(更大的XMX和ParalelGC),这减少了Full GC时间,但还不够。我有2个想法, 我是关注GC参数还是创建字节数组内存池机制?哪一个更好?

I have an application, basically, create a new byte array (less than 1K) store some data after few seconds (generally less than 1 minute, but some data stored up to 1 hour) write to disk and data will goes to garbage. Approximatelly 400 packets per second created. I read some articles that say don't worry about GC especially quickly created and released memory parts (on Java 6).
GC runs too long cause some problem about on my application.
I set some GC parameters(Bigger XMX and ParalelGC),this decrease Full GC time decrease but not enough yet. I have 2 idea,
Am I focus GC parameters or create Byte array memory pool mechanism? Which one is better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独夜无伴 2024-10-19 09:22:40

执行 GC 的频率取决于对象大小,但成本(清理时间)更取决于对象的数量。我怀疑长期存在的数组正在空间之间复制,直到它最终进入旧空间并最终被丢弃。清洁旧一代相对昂贵。

我建议你尝试使用ByteBuffer来存储数据。它们类似于 byte[],但具有可变大小,如果您可以将直接字节缓冲区与 NIO 一起使用,效率可能会更高一些。预分配缓冲区可以更有效地预分配缓冲区。 (虽然可能会浪费虚拟内存)

顺便说一句:直接字节缓冲区使用很少的堆空间,因为它们使用“C”空间中的内存。

The frequency of performing a GC is dependant on the object size, but the cost (the clean up time) is more dependant on the number of objects. I suspect the long living arrays are being copied between the spaces until it end up in the old space and finally discarded. Cleaning the old gen is relatively expensive.

I suggest you try using ByteBuffer to store data. These are like byte[] but have a variable size and can be slightly more efficient if you can use direct byte buffers with NIO. Pre-allocating your buffers can be more efficient to preallocate your buffers. (though can waste virtual memory)

BTW: The direct byte buffers use little heap space as they use memory in the "C" space.

忘东忘西忘不掉你 2024-10-19 09:22:40

我建议您分析一下为什么 GC 对您来说工作得不够好。您可以使用 jmap 转储堆,然后使用 jhatEclipse Memory Analyser 查看其中存在哪些对象。您可能会发现您保留着不再需要的参考资料。

GC 非常聪明,如果您尝试使用自己的内存管理代码来智胜它,实际上可能会让事情变得更糟。尝试调整参数,也许您也可以尝试新的 G1 垃圾收集器。

另外,请记住,GC 喜欢短暂的、不可变的对象。

I suggest you do some analysis into why GC is not working well enough for you. You can use jmap to dump out the heap and then use jhat or Eclipse Memory Analyser to see what objects are living in it. You might find that you are holding on to references that you no longer need.

The GC is very clever and you could actually make things worse by trying to outsmart it with your own memory management code. Try tuning the parameters and maybe you can try out the new G1 Garbage Collector too.

Also, remember, that GC loves short-lived, immutable objects.

哽咽笑 2024-10-19 09:22:40
  1. 使用探查器识别代码片段
  2. 尝试使用 Wea​​kReferences。
  3. 向虚拟机建议 GC 算法
-Xgc:并行
  1. 设置一个大堆并共享内存

<前><代码>-XX:+UseISM -XX:+AggressiveHeap

  1. 下面设置垃圾收集。

<代码>-XX:SurvivorRatio 8

  1. 这可能有帮助
    http://download.oracle.com/docs /cd/E12840_01/wls/docs103/perform/JVMTuning.html#wp1130305
  1. Use profiler to identify the code snippet
  2. Try with WeakReferences.
  3. Suggest an GC algo to the VM
-Xgc: parallel
  1. Set a big Heap and shared mem
-XX:+UseISM -XX:+AggressiveHeap
  1. set below for garbage collection.

-XX:SurvivorRatio 8

  1. This may help
    http://download.oracle.com/docs/cd/E12840_01/wls/docs103/perform/JVMTuning.html#wp1130305
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文