简体   繁体   中英

Is the string pool size affected by setting max heap size?

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 eg “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.

In JDK, the string pool that contains interned strings, consists of two parts:

  1. 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".
  2. 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 .

Since Java 7, the string pool has been a part of the regular heap (see here ). So the regular heap size also constrains the size of the string pool.

The real thing you should be taking from that article is that new String(...) always creates a new String object that is distinct from all previously created String objects. That is not the case for a String object that corresponds to a string literal.

I noticed that the memory consumption is significantly higher than the max heap size (Xmx is 800m, usage is 1.4g).

The explanation for that is that there are various ways that a JVM uses memory that is not part of the regular heap. These include:

  • the memory for the JVM executable and its shared libraries
  • memory used for native code libraries loaded by the application
  • memory segments used for Java thread stacks
  • memory used to represent the metaspace... which holds JIT compiled native code and so on,
  • memory in the "native" heap allocated by native code; eg using malloc .
  • memory allocated for direct buffers

You can use https://visualvm.github.io/index.html to analyse and inspect for example your occupied memory and heap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM