GeoServer Memory Usage Optimisation

Hi All,
We use GeoServer deployed in Tomcat for our applications that send only WMS and WFS requests to GeoServer.
Periodically we have issues due to high RAM usage (please see screenshot).


When I press the Free Memory button, only a small part of the memory is freed (max ~1 GB out of 4), and then in quite a short time the memory becomes full again.

In the worst case, the Tomcat service keeps running and consumes almost all of the memory, but the Tomcat Manager itself and GeoServer stop responding.



Can you please share your experience on how you deal with this? Is it better to set 50% or 75% of RAM in Max Memory Pool in the Tomcat configuration? We have servers with 4GB and 8GB of RAM.

1 Like

Hi @9Val welcome you the community.

GeoServer as a JVM based application, have (via tomcat configurations, environment variables or command line) a configuration to set the initial and maximun heap size, usually set on run command like:

Xms512m -Xmx4g

GeoServer consumpsion can increase during high concurrent requests load and then the garbage collector will free the used space to the JVM, but (and here the trick) it will not return the free memory to the operating system. This means once GeoServer reached a max memory usage of 4 GB the requested memory will keep being 4GB (plus other memry spaces used by the JVM) until GeoServer goes down and get restarted.

If the problem is on JVM not returning the free space to OS: On GeoServer deployments usually a max memory size previously separated for GeoServer is usually asigned to the max heap size parameter (-Xmx). Setting this parameter with a high value that can make the operating system enter on memory pressure is not recommended, due to the fact I wrote previously: JVM will not return the memory already taken.
Take into account the extra memory spaces JVM will also request, so a 4 GB max heap is not exactly the JVM conduming 4GB but probably a bit more.

I would recommend to set the max heap value to a value that in the case of be reached don’t put the system on pressure. Sometimes making this memory presure back to JVM makes the Garbage collector works harder to free the heap memory and we tradeoff CPU cycles for less max heap usage during the runtime.

If the problem is also on requests consuming too much memory, check how much WMS GetMap request you’re getting concurrently, you can tweek the memory configurations per request on WMS configuration page and set a maximun count of concurrent WMS requests limit with control flow module:
https://docs.geoserver.org/main/en/user/services/wms/configuration.html
https://docs.geoserver.org/main/en/user/extensions/controlflow/index.html

1 Like

I second what Fernando said, that’s how the JVM works, it’s not specifically related to GeoServer.
If your GeoServer is used in anger, with high load most of the time, I’d leave it chew as much memory
as it needs, that’s how you get the best performance.

However, if your usage is spotty, high peaks followed by long stretches of idleness, and you need that
memory to be freed for other tasks, then you can have a look at these blog posts:
https://thomas.preissler.me/blog/2021/05/02/release-memory-back-to-the-os-with-java-11
https://accumulo.apache.org/blog/2024/04/09/does-a-compactor-return-memory-to-OS.html

1 Like