Java对象内存使用情况 - ibm jvm 1.4.2 [英] Java Object memory usage - ibm jvm 1.4.2

查看:174
本文介绍了Java对象内存使用情况 - ibm jvm 1.4.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在应用程序中找到java中对象的内存使用情况?

Is it possible to find memory usage of object in java within application?

我希望在应用程序运行时将对象内存使用量作为调试输出的一部分。
我不想使用外部应用程序连接到VM。

I want to have object memory usage to be part of debug output when application runs. I don't want to connect using external application to VM.

我有一个问题,即很少有类占用大量内存并导致内存
问题,我的应用程序崩溃了。我需要找到内存使用情况(我正在使用有限的内存资源)。

I have a problem that few classes eats up huge amount of memory and causes memory problems, my app gets crash. I need to find that memory usage (I am working with limited memory resources).

编辑:我使用的是java 1.4:/

I am using java 1.4:/

推荐答案

查看我的宠物项目, MemoryMeasurer 。一个很小的例子:

See my pet project, MemoryMeasurer. A tiny example:

long memory = MemoryMeasurer.measureBytes(new HashMap());

您还可以获得更多定性内存细分:

You may also derive more qualitative memory breakdown:

Footprint footprint = ObjectGraphMeasurer.measure(new HashMap());

例如,我使用后者来推导 每个条目各种数据结构的成本,其中开销是以创建的对象数量来衡量的,引用和原语,而不仅仅是字节(也是可行的)。因此,下次使用(默认) HashSet 时,可以通知您其中的每个元素都需要1个新对象(不是您的元素),5个引用和一个int ,这是 HashMap 中条目的完全相同的成本(不出意外,因为任何 HashSet 元素最终都会出现在 HashMap ),依此类推。

For example, I used the latter to derive the per entry cost of various data structures, where the overhead is measured in number of objects created, references, and primitives, instead of just bytes (which is also doable). So, next time you use a (default) HashSet, you can be informed that each element in it costs 1 new object (not your element), 5 references, and an int, which is the exact same cost for an entry in HashMap (not unexpectedly, since any HashSet element ends up in a HashMap), and so on.

您可以在任何对象图上使用它。如果您的对象图包含您希望忽略的其他结构的链接,则应使用谓词来避免探索它们。

You can use it on any object graph. If your object graph contains links other structures you do wish to ignore, you should use a predicate to avoid exploring them.

编辑 <$ c $ 1.4> Instrumentation 不适用于Java 1.4(哇,人们仍然使用它?!),所以上面的 memoryBytes 调用不起作用为了你。但第二个会。然后你可以写这样的东西(如果你在32位机器上):

Edit Instrumentation is not available to Java 1.4 (wow, people still use that?!), so the memoryBytes call above wouldn't work for you. But the second would. Then you can write something like this (if you are on a 32bit machine):

long memory = footprint.getObjects() * 8 + footprint.getReferences() * 4 +
              footprint.getPrimitives().count(int.class) * 4 + 
              footprint.getPrimitives().count(long.class) * 8 + ...;

这给你一个近似值。一个更好的答案是将其调整为16的最接近倍数:

That gives you an approximation. A better answer would be to ceil this to the closest multiple of 16:

long alignedMemory = (x + 15) & (~0xF); //the last part zeros the lowest 4 bits

但答案可能仍然是关闭的,因为如果你例如,找到16个布尔值,如果它们存在于同一个对象中则是一回事,如果它们分布在多个对象中则是另一回事(并且由于对齐而导致过多的空间使用)。这个逻辑可以作为另一个访客实现(类似于 MemoryMeasurer ObjectGraphMeasurer 已实施 - 相当就像你可能看到的那样),但我没有打扰,因为那是 Instrumentation 所做的,所以它只能理解1.5以下的Java版本。

But the answer might still be off, since if you find, say, 16 booleans, it's one thing if they are found in the same object, and quite another if they are spread in multiple objects (and cause excessive space usage due to aligning). This logic could be implemented as another visitor (similar to how MemoryMeasurer and ObjectGraphMeasurer are implemented - quite simply as you may see), but I didn't bother, since that's what Instrumentation does, so it would only make sense of Java versions below 1.5.

这篇关于Java对象内存使用情况 - ibm jvm 1.4.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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