5

I have an interesting problem with Java memory consumption. I have a native C++ application which invokes my Java application.

The Application basically does some language translations\parses a few XML's and responds to network requests. Most of the state of Application doesn't have to be retained so it is full of Methods which take in String arguments and returns string results.

This application continues to take more and more memory with time and there comes a time where it starts to take close to 2 GB 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. Comparing heap dumps over a period of time, shows the char[] and String objects take huge memory.

However when we inspect these char[], Strings we find that they do not have GC roots which means that they shouldn't be the cause of leak. Since they are a part of heap, it means they are waiting to get garbage collected. After using verious tools MAT\VisualVM\JHat and scrolling through a lot of such objects I used the trial version of yourkit. Yourkit gives the data straightaway saying that 96% of the char[] and String are unreachable. Which means that at the time of taking dump 96% of the Strings in the heap were waiting to get garbage collected.

I understand that the GC runs sparingly but when you check via VisualVM you can actually see it running :-( than how come there are so many unused objects on the heap all time.

IMO this Application should never take more than 400-500 MB memory, which is where it stays for the first 24 hours but than it continues to increase the heap :-(

I am running Java 1.6.0-25.

Please note the screenshot from yourkit

thanks for any help.

2
  • What JVM are you running? What are your JVM runtime arguments? Try running your application with -verbose:gc JVM flag and paste all extra output you get. Aug 2, 2012 at 7:09
  • Sky, I updated the JVM in the question itself. We just pass Xms while invoking the JVM and nothing else.
    – Geek
    Aug 2, 2012 at 7:16

5 Answers 5

7

Java doesn't GC when you think it does/should :-) GC is too complex a topic to understand what is going on without spending a couple of weeks really digging into the details. So if you see behavior that you can't explain, that doesn't mean its broken.

What you see can have several reasons:

  1. You are loading a huge String into memory and keep a reference to a substring. That can keep the whole string in memory (Java doesn't always allocate a new char array for substrings - since Strings are immutable, it simply reuses the original char array and remembers the offset and length).

  2. Nothing triggered the GC so far. Some C++ developers believe GC is "evil" (anything that you don't understand must be evil, right?) so they configure Java not to run it unless absolutely necessary. This means the VM will eat memory until it hits the maximum and then, it will do one huge GC run.

  3. build 25 is already pretty old. Try to update to the latest Java build (33, I think). The GC is one of the best tested parts of the VM but it does have bugs. Maybe you hit one.

  4. Unless you see OutOfMemoryException, you don't have a leak. We have an application which eats all the heap you give it. If it gets 16GB of RAM ("just to be safe"), it will use the whole 16GB because we cache what we can. You never see out of memory, because the cache will shrink as needed but system admins routinely freak out "oh god! oh god! It's running out of memory" PANIK No, it's not. Unless Java tells you so, it's not running out of memory. It's just using it efficiently.

  5. Tuning the GC with command line options is one of the best ways to break it. Hundreds of people which know a lot more about the topic than you ever will spent years making the GC efficient. You think you can do better? Good luck. -> Get rid of any "magic" command line options and calls to System.gc() and your problem might go away.

5
  • 2
    Aaron : Point 4 is exactly what is happening. This isn't a particularly important component so people are displeased it takes so much memory. I think I will allocate it a max heap space of 1 GB and configure it to dump a core if it runs Out of Memory. If there isn't a leak it shouldn't actually go Out of memory. Do you have any other suggestion ?
    – Geek
    Aug 2, 2012 at 9:15
  • No, my answer pretty much sums up 15 years of experience with Java, the VM and GC. Aug 2, 2012 at 9:17
  • I just noticed another problem, we invoke the JVM with the -Xmx1024m and it reaches up to 1.8G. Do you have any comments about this ? I have restarted the JVM with -Xmx1024m and -XX:+HeapDumpOnOutOfMemoryError options, let me test this again. Thanks.
    – Geek
    Aug 2, 2012 at 10:32
  • 1
    That's "correct" as well. One byte in Java doesn't map to 1 byte in the OS. Java needs additional memory to load the VM, shared libraries, head management structures, etc. The 64bit version will need twice as much real RAM as the 32bit version. Also don't forget that some of that memory is shared with other (java) processes. Aug 2, 2012 at 11:35
  • One key aspect here is that Java uses a virtual machine. C++ bangs the hardware while Java makes every computer appear alike (well, mostly). This makes Java so much more efficient when it comes to run your code on a new computer: Java compiles it for you as needed. With C++, you'd have to install a compiler + sources first, build, fix the build, ... Irritating when you don't need it, though :-) Aug 2, 2012 at 11:38
2

Try decreasing the heap size to 500 Megabytes and see if the software will start garbage collecting or die. Java isnt too fussy about using memory given to it. you might also research GC tuning options which will make the GC more prudent about cleaning stuff up.

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

The JVM can't collect the memory allocated for the long string in the above case, since there's a reference to part of it. If you're doing stuff with Strings and you're suffering from memory issues, this might be the cause of your grief.

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

4
  • If it actually held a reference, then you would see it as a path to the GC root. Additionally, the substring() method already copies the string. Aug 2, 2012 at 7:49
  • 2
    @StevenSchlansker: String.substring() doesn't copy anything. It reuses the existing character array and just remembers the new limits. Look into the code: grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/… Aug 2, 2012 at 7:53
  • 1
    @Steven, your first point is correct (the original Question probably isn't about a memory leak) but Buhb is correct with regards to this being a cause for memory leaks - the subString doesn't copy the String it just takes a copy of the reference, keeping the whole reallyLongString alive. This is a very well documented "bug" in the Java Library.
    – fommil
    Aug 2, 2012 at 7:54
  • Wow, I totally misread the code. There's a String(byte[], int, int) constructor and a String(int, int, byte[]) constructor and they do entirely different things! Thanks for the correction. Aug 2, 2012 at 15:54
2

There might not be a leak at all - a leak would be if the Strings were reachable. If you've allocated as much as 2GB to the application, there is no reason for the garbage collector to start freeing up memory until you are approaching that limit. If you don't want it taking any more than 500MB, then pass -Xmx 512m when starting the JVM.

You could also try tuning the garbage collector to start cleaning up much earlier.

0

First of all, stop worrying about those Strings and char[]. In almost every java application I have profiled, they are on the top of memory consumer list. And in almost no of those java application they were the real problem.

If you have not received OutOfMemoryError yet, but do worry that 2GB is too much for your java process, then try to decrease Xmx value you pass to it. If it runs well and good with 512m or 1g, then problem solved, isn't it?

If you get OOM, then one more option you can try is to use Plumbr with your java process. It is memory leak discovery tool, to it can help you if there really is a memory leak.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.