JVM-- zone method (Method Area)

Methods district

Run-time memory area for each thread shared. It stores the configuration information of each class, e.g. runtime constant pool (Runtime Constant Pool) bytecode content, field and method data, and the constructor of ordinary methods.

The above said is the norm, implemented in different virtual machine it is not the same, the most typical is the permanent generation of (PermGen) and meta-space (Metaspace) .

Examples of variables exist the heap, and method area irrelevant.

In the actual JVM implementations, it is not necessarily achieved by a single special area, different implementations can be placed in different places:

Permanent Generation (Permanent Generation)

HotSpot virtual machine is permanently on behalf of the realization of the method and JDK7 area in the past, but also on behalf of the heap permanent (but note that other virtual machines are not permanently generations, this is the HotSpot virtual machine-specific).

In JDK1.7, on behalf of the permanent removal of the work has already begun, originally part of the permanent generation of data transferred is stored in the Java heap or native memory, but still exists in the permanent generation of JDK1.7, it did not completely removed. Removing work includes the following three points:

  • Symbolic references (Symbolic Reference) transferred to native memory
  • Interned Strings moved to the heap
  • The end of the shift from a static variable instanceKlass objects (PermGen inside) to the end of the java.lang.Class objects (heap inside) of

Element space (metaspace)

From the beginning of JDK 8 HotSpot virtual machine completely removed PermGen, instead storing metadata in the native memory inside. The new memory used to store metadata space is called Metaspace.

Cancel permanent behalf of the following main reasons:

  • String generation permanent presence, performance problems and prone to memory overflow
  • Permanent generation will bring unnecessary complexity for the GC, and recovery efficiency is low
  • Combined with JRockit facilitate HotSpot (JRockit and no permanent generation)

Examples thrown heap overflow in Java 8:

public class StringHeapError {
    public static void main(String[] args) {
        String temp = "world";
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            String str = temp + temp;
            temp = str;
            str.intern();
        }
    }
}

Output:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

If it is in JDK 6, which will prompt PermGen Space overflow

The following are examples Metaspace overflow thrown with a large amount of a Cglib proxy class. But before that you need to modify the JVM startup parameters set as follows:

-XX:MetaspaceSize=10M -XX:MaxMetaspaceSize=10M
public class ClassInfo {
    public static void main(String[] args) {
        while (true) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(OOMObject.class);
            enhancer.setUseCache(false);
            enhancer.setCallback(new MethodInterceptor() {
                public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                    return proxy.invokeSuper(obj, args);
                }
            });
            enhancer.create();
        }
    }

    static class OOMObject { }
}

Output:

Exception in thread "main" java.lang.OutOfMemoryError: Metaspace

You can see, the class information (classes metadata) placed Metaspace rather Interned Strings, static variables placed on the heap.

Published 665 original articles · won praise 1894 · Views 240,000 +

Guess you like

Origin blog.csdn.net/cold___play/article/details/104075794