0

My java heap is allocating at around 123 MB. I need this to be less. I have a 1 GB limit and both programs running are servers. One runs at 953 MB. The server JAR I am trying to run should only take up 10 MB, or less. How can I make ubuntu respond the same as other OS's I have tested the JAR on? My code can be found at GitHub.

Java Version: JDK/JRE-7

Tyler
  • 35
  • 1
  • 5
  • 1
    `man java`. Then search for `memory` – njzk2 Dec 09 '13 at 16:14
  • Which JVM are you using? – David Dec 09 '13 at 16:14
  • 7
    Generally when the JVM seems to take up a lot of memory (e.g. looking at `top`) for a small application, it has actually only *reserved* that memory, but not *locked* it. This means that most of the physical RAM on the server is still available to other applications. If your Java application does end up needing as much memory as the JVM had initially reserved, the OS may need to start swapping memory. However, if you code your Java application carefully, the Java application will only ever need a small portion of the memory that the JVM reserved. – Mike Clark Dec 09 '13 at 16:18
  • An additional point to @MikeClark 's excellent description is to consider what do you want to happen when memory usage of both programs exceeds 1 gig? Should they start swapping or should one of them die? You can set a hard limit with jvm memory options. Lots of good reading here: http://docs.oracle.com/cd/E13222_01/wls/docs81/perform/JVMTuning.html – Taylor Dec 09 '13 at 17:00
  • @MikeClark you could post this as a answer, really. Please do, because this is quite helpful and will be much more visible as the answer. – Eel Lee Dec 10 '13 at 10:05

1 Answers1

0

Out-of-the-box Java on *nix can look a little scary when you just look at it via top. The java executable often puts up huge numbers under the VIRT column, like 900m. Why is my small Java program using 900m of RAM?

Actually, it's probably not using 900m of RAM. The JVM has told the OS "I might use this much memory... be prepared". But it's probably not actually using anywhere near that much physical RAM -- and if it's a small program, it'll never come anywhere near that. Any physical RAM that java is not actually using is still freely available to other processes on the system.

For a more accurate picture of how much physical RAM the java process is using, look under top's RES column. Though, a full discussion of *nix memory management and profiling Java is probably outside the scope of this answer. I'd encourage you to try Googling the topic and developing specific questions based on the material you find.

Most of the time your Java programs (and other programs running along side them) are going to do just fine using Java's default memory settings. Sometimes you need to limit (or increase) the maximum amount of heap memory that JVM is allowed to allocate. This is the most commonly tuned Java memory setting, and it is usually set with the -Xmx command-line argument. You can read more about it here and here.

Sometimes it can be a little bit tricky figuring out where to modify java's command-line options if your Java program is being magically started for you, e.g., as a system service, or part of some larger script. Googling Xmx will probably get you started on the conventional way of modifying java arguments for that product.

For example Google search: ubuntu tomcat Xmx

Gives links that point us in the direction of /etc/default/tomcat6.

Community
  • 1
  • 1
Mike Clark
  • 10,027
  • 3
  • 40
  • 54