How to determine the ideal size for an Metaspace for java 8

There are multiple things here that you can consider:

  1. Initial Metaspace Size: Do you see negative and measurable impact when starting up your application because the JVM has to resize the metaspace? Then you should probably set the minimum size. Still I would try to avoid this because this would be a setting that will be easily forgotten when the application grows. -XX:MetaspaceSize=<NNN>

  2. Maximum Metaspace Size: Do you want your application to fail when the metaspace grew to specific size? Or do you want to limit the resources the server takes in this regard? Then you should probably set a maximum size for the metaspace -XX:MaxMetaspaceSize=<NNN>

  3. Metaspace Free Ratio: Do you load many different classes dynamically? Then you could maybe define a free ratio on the metaspace so that always enough space for new classes is available and no resizing will be needed in critical situations. -XX:MinMetaspaceFreeRatio=<NNN> and -XX:MaxMetaspaceFreeRatio=<NNN>

My suggestion would be to stick to the defaults, test it and only react when there is a need to.


The introduction of Metaspace was an attemt to fix java's out of memory issues (IIRC) when it comes to garbage collection by the mentioned dynamic memory growth/allocation.

In a production environment, what I'm about to say isn't really all that viable but: Try it out. I've personally never had any issues with the default dynamic settings, but I guess it would depend on the size of and stress on your application. For now, assume the default settings are appropriate.