Why my Java heap is with so many char[]

Viewed 15294

I have a web application in Tomcat where I do many String operations (substring, indexof, trimming, etc.). I made a heap dump with jmap and I loaded it using VisualVM and I realized that near 50% of my heap memory usage is with char[], Why the char[] are taking that memory usage?, should I be concerned?, is it something related to String pools?enter image description here

2 Answers

Strings are internally just a char[] and some extra data. A char[] means a character array in other words it's an array that saves your string character by character. If you do a lot of string proccessing it's entirely possible that your system is fileld with char arrays.

So in short nothing to be concerned unless you system is actually using more memory then it should be. In that case you can look if there are some file structures left uncleared (hash maps or related).

811k of char[] correspond to 800k of Strings, so yes, you have too many Strings.

If you're having a memory leak (judging by the tag), then it's most probably are strings in a HashMap. You have 800k instances of strings, 200k instances of hash entries, 30k of maps. This probably means that your strings are kept in this cache and are not removed. Global map is a frequent cause of memory leaks, one needs to make sure that they are evicted from this cache.

Since all these values are not being GC-ed, you could try analyzing an object graph to see what's holding them with a tool like JProfiler.

Related