开发者

Finding objects having same values in heap dump

I want to reduce 开发者_JAVA技巧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.


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!


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜