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

16,965

Solution 1

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.

Solution 2

Holger is right. It's clearly documented here, I may just quote it here.

-XX:MetaspaceSize=size Sets the size of the allocated class metadata space that will trigger a garbage collection the first time it is exceeded. This threshold for a garbage collection is increased or decreased depending on the amount of metadata used. The default size depends on the platform.

direct quote but the emphasis is mine ;)

Solution 3

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.

Share:
16,965
Rishikesh Darandale
Author by

Rishikesh Darandale

A developer, reader and learner!

Updated on June 08, 2022

Comments

  • Rishikesh Darandale
    Rishikesh Darandale almost 2 years

    We are migrating our web application from Java 7 to Java 8. We had defined the PermSize jvm param arguments.

    Since Java 8 will ignore this parameters, this are none of use. Metaspace is introduced in Java 8 and default size of it is unlimited(limited to physical memory) with dynamic growth.

    My question is do I need to figure out the best values for my web application for metaspace jvm params or default will be a good idea to use?

    Update:

    More on my web application: My web application is based on struts and spring frameworks. It usually takes 4k requests/min. This application is deployed on tomcat 8.5 and it does not host any other web application. But, there are multiple tomcat instances running on same virtual machine. The current -Xms and -Xmx values are set to 1g.

    • Wep0n
      Wep0n over 6 years
      So I can edit my answer: How big is the application and how many users are using it? Do you have multiple applications running on the same server? You definitely want to set some fixed ceiling for memory if your application is sharing the same machine with other applications, IF running out of ressources is a genuine concern as a whole.
    • Rishikesh Darandale
      Rishikesh Darandale over 6 years
      @Wep0n, I have updated about my web application in my question.
  • Holger
    Holger over 6 years
    -XX:MetaspaceSize does not set the initial size. It sets the threshold that will trigger a garbage collection the first time it is exceeded.