7

My application uses a lot more memory than I think it suppose to use, and I'm trying to understand which class is using a large amount of the memory and maybe not releasing it.

I'm using VisualVM and in the memory sampler I can see that most of the memory is spent on Chars, Strings and Bytes, all my classes uses Strings, but as you know the VisualVM shows ALL Chars and Strings in the system (all the Chars are identical to the Strings which makes it hard to understand who holds them), as I understand the size of the other classes that hold these strings is calculated without the strings.

How can I see in this tool who are the "Real" largest classes - the ones that holds all these strings? (preferably if I can get from these classes to their Strings and not the other way around) I tried to use the "root to the nearest GC" in the heapDump but there are about 4,000,000 Strings so the chance of me finding the "problematic" ones is very small...

Thanks!!!

aye
  • 73
  • 1
  • 4
  • I think you can not explicity say which class uses how much memory. If you are instantiating the object of another class in some different class, how would you go about it? Also to my knowledge you can not get a class based meomory utilization. – Johnydep Jan 09 '12 at 14:01

2 Answers2

3

If you do a heap dump, you can find the 20 largest objects (including all of the space it references). For anything more, I recommend using the OQL console which is very powerful.

Basically, you are searching for the following:

Lot of String's taking up 20Mb 
   --- kept alive by --->
HashSet#28839 
   --- kept alive by --->
MyOwnClass#88293

I propose you take a random string, find it's referrees and analyze this until you find out a possible suspect. Once you have this suspect, you can do an OQL query using http://visualvm.java.net/oqlhelp.html#rsizeof to find out the total size of these objects.

parasietje
  • 1,529
  • 8
  • 36
  • Thanks! I will try this now, I hope I will find something using this method. – aye Jan 09 '12 at 14:34
  • Hi, Do you know how can I find a specific large instance? I only see the 4,000,000 Strings that are the largest but cannot see the specific instances that are the largest. – aye Jan 09 '12 at 15:54
  • You have got your heap dump in which the classes view specifies String as having the most bytes[%]. You doubleclick on this, which takes you to the Instances view. Here, you can select a specific instance and follow the references in the bottom pane. – parasietje Jan 09 '12 at 17:20
  • Oh, I found it, Thanks! you can also check the size of each instance using "Retained" and find the largest instances in this way. Thanks! – aye Jan 10 '12 at 07:54
2

Make a heap dump, open it with MAT and look in histogram for largest Retained Sizes.

Nikem
  • 5,716
  • 3
  • 32
  • 59