Java String objects do not collect garbage on time

I have an interesting issue with Java memory consumption. I have my own C ++ application that calls my Java application.

The application mainly performs some language translations \ parses several XML and responds to network requests. Most of the state of the application should not be preserved, so it is full of methods that take String arguments and return string results.

This application continues to receive more and more memory over time, and there comes a time when it starts to take about 2 GB of memory, which made us suspect that there is a leak somewhere in some hashtable or static variables. On closer inspection, we did not find any leaks. A comparison of heap dumps over a given period of time shows that the char [] and String objects occupy a huge amount of memory.

However, when we check these char [] lines, we find that they do not have GC roots, which means that they should not be the cause of the leak. Since they are part of the heap, this means that they are waiting for garbage collection. After using the proven MAT \ VisualVM \ JHat tools and scrolling through many such objects, I used the trial version of yourkit. Immediately after this, Yourkit reports that 96% char [] and String are not available. This means that while taking the dump, 96% of the rows in the heap were expecting garbage collection.

I understand that GC works sparingly, but when you check with VisualVM, you can see how it works :-( than there are always so many unused objects in the heap.

IMO this application should never occupy more than 400-500 MB of memory, where it remains for the first 24 hours, but it continues to increase the heap: - (

Java 1.6.0-25.

Please note the screenshot from yourkit

.

+5
5

Java GC, , /:-) GC , , , , . , , , , .

, , :

  • . (Java char - , char ).

  • GC. ++ , GC "" (, , , ?), Java , . , , , GC.

  • build 25 . Java (, 33). GC , . , .

  • OutOfMemoryException, . , , . 16 ( " " ), 16 , , . , , " ! ! " PANIK , . Java , . .

  • GC . , , - , GC . , ? . → - "" System.gc(), .

+7

500 , . Java . GC, GC .

+2

, - , String . 2 , , , . , 500 , -Xmx 512m JVM.

, .

+2
String reallyLongString = "this is a really long String";
String tinyString = reallyLongString.substring(2, 3);
reallyLongString = null

JVM , , . , , .

tinyString = new String(reallyLongString.substring(2, 3); .

+1

, char []. Java, , . Java .

If you haven't received OutOfMemoryError yet, but worry that 2GB is too much for your java process, then try decreasing the Xmx value you pass to it. If it works well and well with 512m or 1g, then the problem is resolved, right?

If you get OOM, then another option you can try is to use Plumbr using your Java process. This is a memory leak detection tool, it can help you if there really is a memory leak.

0
source

All Articles