Issue
I have a java application that has a weird memory usage, I noticed that the memory consumption is significantly higher than the max heap size (Xmx
is 800m
, usage is 1.4g
).
One of the recent changes preceding this was a large increase in the unique strings in use, so I thought maybe the many strings I have use a lot of memory outside the heap - is this possible?
I'm running java 11.
EDIT:
for example, in this article it mentions:
When we create a String object using the new() operator, it always creates a new object in heap memory. On the other hand, if we create an object using String literal syntax e.g. “Baeldung”, it may return an existing object from the String pool, if it already exists.
Giving the impression of 2 different areas - the heap and the string pool.
Solution
In JDK, the string pool that contains interned strings, consists of two parts:
- The hash table, which is allocated off-heap and contains the references to the interned strings. This memory area is shown in Native Memory Tracking report as "String table".
- The interned strings contents. These are regular
java.lang.String
objects in Java heap.
So, the string pool has both on-heap and off-heap parts. The off-heap part is typically smaller, but it still can take a significant amount of extra memory, if the applications creates too many interned strings.
Use Native Memory Tracking to find the amount of memory consumed by the String table.
See this answer to find other reasons why the process may take much more memory than -Xmx
.
Answered By - apangin
Answer Checked By - Marilyn (JavaFixing Volunteer)