在堆转储中查找具有相同值的对象

发布于 2024-09-10 07:19:37 字数 56 浏览 4 评论 0原文

我想减少内存中对象的数量。可能有许多对象具有相同的值。有没有办法找出堆转储中具有相同值的所有对象。

I want to reduce number of objects in memory. There may be many objects having same values. Is there way to find out all the objects which has same values in heap dump.

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

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

发布评论

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

评论(3

莳間冲淡了誓言ζ 2024-09-17 07:19:37

我想减少内存中对象的数量。可能有许多对象具有相同的值。

我假设您的长期目标是通过仅保留具有相同值的对象的一份副本来让应用程序节省内存。

这个想法的问题在于,您通常需要构建/维护一个额外的数据结构,以便您可以找到具有相同值的先前对象。例如,HashMap。 (像 Guava interner 这样的现成解决方案在底层是相同的。)如果您不小心,此数据结构可能使用比您通过消除重复项节省的内存更多的内存。

此外,如果没有正确实现,“驻留”数据结构可能会通过阻止对象被垃圾收集而有效地泄漏内存。内存泄漏问题的解决方案都涉及在某种程度上使用弱引用等。这意味着更多的对象和更慢的垃圾收集。后者是因为 1)interned 对象需要被 GC 跟踪,2)interned 对象往往寿命更长并且更有可能最终出现在老年代,3)WeakReference 实例对于 GC 来说相对昂贵去处理。

总之,注意不要让你的记忆问题变得更糟!

I want to reduce number of objects in memory. There may be many objects having same values.

I assume that your long term objective is to have your application save memory by keeping only one copy of objects that have the same value.

The problem with this idea is that you typically need to build / maintain an extra data structure so that you can find a previous object with the same value. For example, a HashMap. (Off the shelf solutions like the Guava interner are the same under the hood.) If you are not careful, this data structure may use more memory than you are saving by eliminating the duplicates.

Also, it not implemented properly, an "interning" data structure may effectively leak memory by preventing objects from being garbage collected. Solutions to the memory leak issue all involve using WeakReferences, etc at some level. And that means even more objects, and slower garbage collection. The latter is because 1) the interned objects need to be traced by the GC, 2) the interned objects tend to live longer and are more likely to end up in the old generation, and 3) the WeakReference instances are relatively expensive for the GC to deal with.

In summary, take care that you don't make your memory problems worse!

静赏你的温柔 2024-09-17 07:19:37

使用分析器。我可以说 YourKit 有这个功能。

Use profiler. I can say that YourKit has this functionality.

残疾 2024-09-17 07:19:37

查看 Integer.valueOf(int i) 方法的 Sun/Oracle Java 实现。它已经具有流行值的预定缓存(从 -128 到 127),因此在为这些值调用该方法时不会创建新实例。

这表明,如果您在创建对象时小心,那么您可以减少内存占用。也许您应该调查对象是如何创建的,并尝试在创建时缓存它们。也许可以通过查看流行的用例,或者对应用程序的行为方式进行一些分析。这将取决于应用程序,并且是否有适合您的开箱即用的解决方案值得怀疑。我相信从长远来看,这会为您节省一些精力,而不是试图找出当前占用太多空间的内容。

Take a look at the Sun/Oracle Java implementation for the method Integer.valueOf(int i). It already has a pre-determined cache of popular values (from -128 to 127), so no new instances are created when calling that method for those values.

This shows that if you are careful in your creation of objects, then you can keep your memory footprint down. Perhaps you should investigate how your objects are being created and try to cache them at creation time. Perhaps by looking at popular use cases, or some profiling of the way the application behaves. This is going to be application dependent, and it is doubtful that there will be an out-of-the-box solution for you. I believe this will save you some effort in the long run, rather than trying to work out what is currently taking up too much room.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文