1

I am developing a java web application and I am running it on a Tomcat 7 server. I am checking the size of the memory by calling:

System.out.println("Total memory is: " + Runtime.getRuntime().totalMemory());

What I am noticing that the size is changing, when the size of my data getting larger the Runtime.getRuntime().totalMemory() get larger as well.

1- Is the JVM trying to be larger than the size of the application data?

2- Is there any limit for the JVM size, other than the RAM memory size?

3- A third question not very related to the two above: what is the size of a running Tomcat 7 server application? Actually I have a small RAM in my server (Amazon cloud server, free tire, 600 MB RAM), And I am noticing that JAVA occupies about 52% of the memory without deploying any web application.

Excuse me for my very basic questions, I am not familiar with web servers and web applications.

Rami
  • 8,044
  • 18
  • 66
  • 108
  • 3
    The object heap dynamically grows and shrinks as [the garbage collector does work](http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html). :-) – obataku Aug 09 '12 at 21:36
  • 1
    Controlling the minimum and maximum heap sizes is [described in this post](http://stackoverflow.com/questions/1098488/jvm-heap-parameters). By default I believe the heap will continue to grow and the GC will eventually kick in to recover eligible objects. If your application has a memory leak you will eventually get an `OutOfMemory` exception based on the limits of your server set up – Brad Aug 09 '12 at 21:41

1 Answers1

3

Is there any limit for the JVM size, other than the RAM memory size?

Yes, the limit set by you.

-Xmx for maximum heap size and -Xms for initial heap size

The JVM runs virtually. If you allocate more memory than available on your machine it will be swapped out.

what is the size of a running Tomcat 7 server application?

Haven't you answered that question yourself? It's 52% of your memory. In general you can specify for your Tomcat the memory max. and min. size as well. After all it's just another JVM.

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
  • I disagree that you can set a limit for JVM memory use. You have set a limit for the JVM's *heap memory* but there is no cap on the amount of *native memory* it can use, unless you use OS-level restrictions of some kind (and there is no OS indicated on this question, so it's out of scope). – Christopher Schultz Nov 30 '18 at 17:38
  • @ChristopherSchultz You mean memory allocated through `unsafe.allocateMemory` or `malloc` with JNI? – Konrad Reiche Dec 06 '18 at 12:51
  • No, I mean that the JVM will use whatever native memory it wants/needs for non-heap-based structures. Launch a JVM with a "heap limit" of e.g. 64MiB, then ask the OS how much memory the process is actually using. It's not possible to tell the JVM "don't use more than (e.g.) 70MiB total. You can only ask it to limit the heap, or tell the OS to refuse to allocate more than X bytes of memory to the process. Of course, there are also ways for the application to allocate native memory -- usually via a native library (`malloc`), etc. – Christopher Schultz Dec 06 '18 at 17:49