2

Suppose I am running tomcat with -Xms256m -Xmx1024m, but would like to increase my heap usage.

What is the largest safe heap size I can allocate?

For the purpose of this question, please assume, the system has 5GB RAM and is a z/Linux system. However, if possible I would like a broader rule of thumb answer.

This is dedicated system, so I am mostly worried about the OS memory usage. I am not worried about other programs.

Also, if this is a duplicate, sorry. I was surprised I did not find more information on this kind of thing.

GC

PS: I understand there may be some more system information needed. I don't mind if the answer details how to find an answer, rather than giving me a number.

trincot
  • 317,000
  • 35
  • 244
  • 286
GC_
  • 448
  • 4
  • 23
  • It doesn't matter if you specify to much memory it will only use what it can – brso05 May 22 '15 at 19:53
  • Unless you have a very large number of network connections or something else unusual, it's generally acceptable to almost ignore OS memory usage and just add a bit when you figure out how much RAM needs to remain available for caching (to keep the I/O working set in RAM). You should also keep 1% or so for page tables. – David Schwartz May 22 '15 at 19:54
  • 1
    Are you on x64 or x32 bit machine? – brso05 May 22 '15 at 19:56
  • http://stackoverflow.com/a/3648468/4028085 – brso05 May 22 '15 at 19:59
  • @brso05 My understanding is using too much heap memory will cause the OS to page too much. – GC_ May 22 '15 at 20:00
  • @brso05 x64 bit machine – GC_ May 22 '15 at 20:01
  • @GC_ yes it can cause paging and this can be a performance problem but as far as the jvm is concerned there is no problem...You should tune your jvm to get the best performance based upon what system and applications you are running. Trial and error... x64 means the jvm will allow you to specify a higher amount for heap space. On a x32 bit machine if you specify more than 4G the jvm will not start and throw an error. – brso05 May 22 '15 at 20:04
  • @GC_ it depends on how much memory your applications actually need if they don't need that much than it is safer to go lower but if they need to push the limits of physical memory then you have to keep tuning your jvm until you can get as close as you can without causing performance issues. Heap space is not the only jvm setting you can change to help with memory performance. – brso05 May 22 '15 at 20:07
  • @David Schwartz How would you determine how much need to remain available to caching. – GC_ May 22 '15 at 20:17
  • @GC_ There are two ways. One way is to reason about what your application is doing and estimate how much data it will need cached. Another is to give it a particular amount of memory and measure whether you see indications that it doesn't have enough (such as excessive disk I/O). If your app doesn't have a lot of network connections and doesn't use the disk a lot, almost all your system memory can be used by the application itself. – David Schwartz May 22 '15 at 20:19
  • @brso05 How would you measure or quantify "performance problems" in this context? Is there a specific metric to look at? – GC_ May 22 '15 at 20:20
  • @GC_ stress test your application and see how it handles it if you don't experience problems (freezing) (requests taking a long time) etc...then your fine. – brso05 May 22 '15 at 20:21

1 Answers1

0

You will want to avoid swapping of the heap all costs if you at all care about performance. This means that you will have to make sure that the total reserved memory on the system is less than the available RAM.

A rule of thumb in setting the heap size is very hard to give since we cannot know the native memory consumption of your system or your application.

For example, your Java application will consume native memory for things like the metaspace and JNI data.

I think the best you can do is to run the application with a not too agressive heap size (think 3 GB on a 5 GB RAM system) to find our how much native memory it typically consumes.

When you know this you can size your heap accordingly and make sure you leave some memory to spare (think 10%) just in case and for OS level caching

Thought process should be something like this:

  • Memory consumed when I run nothing on the system is 200 MB

  • Native memory typically consumed by my java application is 300 MB

  • I want 500 MB for OS level caching and "just in case"

  • Then I should be able to set my max heap size to 5000 - 500 - 300 - 200 = 4000 MB

K Erlandsson
  • 13,408
  • 6
  • 51
  • 67
  • You are forgetting that the memory size of the Java Virtual Machine is not limited to just that used by the Java heap. There is the native heap, and the Java and native stacks for each thread to worry about. The overhead (on top of the heap) can sometimes be quite surprising. I've seen JVMs where the total used memory is twice the size of the maximum heap size. – Christopher Schultz May 24 '15 at 15:54
  • @ChristopherSchultz That was one of the points I was trying to make by writing "For example, your Java application will consume native memory for things like the metaspace and JNI data." And with "Native memory typically consumed by my java application ..." I mean exactly that. It seems I could be a lot more clear in my answer though. – K Erlandsson May 24 '15 at 17:43