OutOfMemoryError Series (4): Metaspace

Source: Internet
Author: User
Tags gc overhead limit exceeded

This is the fourth article in this series, related articles list: OutOfMemoryError series (1): Java Heap Space OutOfMemoryError series (2): GC overhead limit exceeded outofmemory Error series (3): PermGen Space OutOfMemoryError Series (4): Metaspace

The JVM limits the maximum memory of the Java program, and modifying/specifying startup parameters can change this limitation. Java divides heap memory into sections, as shown in the following illustration:

The maximum values for these memory pools "Java8 and above" are specified by JVM startup parameters such as-xmx and-xx:maxmetaspacesize. If not explicitly specified, the size of the platform type (OS version +JVM version) and physical memory is determined.

The message expressed by the Java.lang.OutOfMemoryError:Metaspace error is that the metadata area (metaspace) has been used for full reason analysis

If you are an old Java driver, you should be familiar with PermGen. But starting with Java 8, the memory structure has changed a lot, no longer using PermGen, but introducing a new space: Metaspace. This change is based on a wide range of considerations, in part because it is listed as follows:

It's hard to predict how big a permgen space is. Specifies that the small will cause Java.lang.OutOfMemoryError:Permgen size error, set more and cause waste.

In order to improve GC performance, the concurrency phase of garbage collection is not halted, and the metadata is specifically traversed (specific iterators).

Depth optimization of concurrent class unloading for G1 garbage collector.

In Java8, all content in the previous PermGen is moved to metaspace space. For example: Class name, field, method, byte code, constant pool, JIT optimization code, and so on.

The amount of metaspace used is related to the number/size of classes that the JVM loads into memory. It can be said that the main reason for java.lang.OutOfMemoryError:Metaspace errors is that the number of classes loaded into memory is too large or too bulky . Sample

Similar to the PermGen in the previous chapter, the amount of metaspace space used has much to do with the number of classes loaded by the JVM. The following is a simple example:

public class Metaspace {
  static javassist. Classpool CP = Javassist. Classpool.getdefault ();

  public static void Main (string[] args) throws exception{for
    (int i = 0;; i++) { 
      Class c = Cp.makeclass ("Eu.plumb r.demo.generated "+ i). Toclass ();

You can see that using the Javassist tool library to generate class is very simple. In the For loop, many classes are generated dynamically, which eventually loads the class into the metaspace.

Execute this code, as the generated class more and more, will eventually occupy Metaspace space, throw java.lang.OutOfMemoryError:Metaspace. On Mac OS X, in the Java 1.8.0_05 environment, if you set the boot parameter-xx:maxmetaspacesize=64m, the JVM hangs after approximately 70,000 classes are loaded. Solution

If you throw a metaspace-related outofmemoryerror, the first solution is to increase the metaspace size. Use the startup parameters such as the following:

-xx:maxmetaspacesize=512m

This sets the maximum value of the metaspace to 512MB and does not throw outofmemoryerror if it is not exhausted.

There is a seemingly simple scheme that directly removes the Metaspace size limit. However, it is necessary to note that without limiting the size of the metaspace memory, if there is not enough physical memory, it may cause memory exchange (swapping), which can severely slow down system performance. In addition, problems such as native memory allocation failures can also be caused.

In a modern application cluster, it is better to suspend the application node than to slow down the response.

If you don't want to get the alarm, you can hide the Java.lang.OutOfMemoryError:Metaspace error message like an ostrich. But this does not really solve the problem, it will only delay the outbreak of time. If there is a memory leak, please refer to the previous article and look for the solution carefully.

Original link: https://plumbr.eu/outofmemoryerror/permgen-space

Translation Date: September 19, 2017

Translator: Anchor: Http://blog.csdn.net/renfufei

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.