TeabagD
TeabagD

Reputation: 669

Java Tomcat application Heap size a lot larger then Used Heap

I'm working on a Java Webapp that's running on a 1GB memory server with Tomcat.

Lately we've noticed that the app is eating up most of the memory on that server - around 83%.

I have profiled the App using VisualVM on my personal machine with a lot more RAM.

VisualVM is showing roughly 400MB Used Heap Space, and up to 1.4GB allocated Heap space.

Setting -Xmx512m allows me to lower the allocated heap space without any significant impact on sites performance.

enter image description here

A few questions:

  1. Is this more or less normal? Why is this happening?
  2. What can be done to reduce the memory consumption? Using -Xmx to limit the amount of memory taken up may be a problem if the app does actually end up needing more memory.
  3. If the OS needs another say 100mb for another process, will java release this memory for the OS to use, given that it's not used heap and only allocated heap, or will the OS Run into memory issue?

Upvotes: 1

Views: 1759

Answers (1)

Satish
Satish

Reputation: 1065

Answering your questions in order.

  1. Yes, it's normal. E.g. when you start the server with max heap as 2 gb, then 2gb is reserved for the java process. Application may or may not use it totally, but it cannot use more than 2 gb even though you have more free memory at OS level, because you have restricted max usage to 2 gb. Also, it's normal that the application usages much lesser memory say 100 MB only and in that case used heap size will be much lesser than the total heap size.
  2. Using -Xms limit the memory allocated, profile your application with expected load or to begin with lower load and see whether the app runs fine and also analyze the memory usage. You need to run multiple tests by varying configured memory and keeping all other parameters viz. load constant between multiple runs to find the optimum no.
  3. No, jvm will not release the memory to another process till the server is up. Once you start the server the maximum memory config for the jvm will be reserved for the jvm process.

Upvotes: 3

Related Questions