JDK1.8 JVM Memory model

Source: Internet
Author: User
Overview of the JDK1.8 JVM memory model

The JDK1.8 JVM memory model is described here. The biggest difference between 1.8 and 1.7 is that the metadata area replaces the permanent generation . The nature of the meta space and the permanent generation are similar to the implementation of the method area in the JVM specification. However, the biggest difference between the meta space and the permanent generation is that the metadata space is not in the virtual machine, but the local memory is used . Ii. Introduction of the various regions 1. Program Counter

A piece of each thread that points to the line number of the bytecode code that the current thread is executing. If the current thread executes a native method, its value is null. 2. Java Virtual machine stack


Thread-Private, each thread corresponds to a Java virtual machine stack, and its lifecycle is Cheng with the line. Each Java method creates a stack frame when it is invoked and merges into the stack. Once the call is complete, the stack is out. When all the stack frames are out of the stack, the thread completes its mission. 3. Local method Stack

Features are very much the same as Java Virtual machine stacks. The difference is that the local method stack serves the native method that the virtual machine uses. Don't say much. 4. Heap


A heap is the largest and most complex area of JVM memory usage. Its only use is to hold object instances: All object instances and arrays are allocated on the right. After 1.7, the string constant pool is stripped out of the permanent generation and stored in the heap. The heap has its own further partition of memory, according to the division of GC collection angle, see above. 4.1 Heap Space memory allocation (by default) The old age: Two-thirds of the heap space of the young generation: One-third of the heap space
Eden Area: 8/10 of young generation space SURVIVOR0:1/10 young generation space SURVIVOR1:1/10

To view all default JVM parameters, execute the following command on the command line

Java-xx:+printflagsfinal-version
Output

The output has a large hundreds of rows, where only two of the associated parameters are taken

[Global flags]
    Uintx Initialsurvivorratio                      = 8                                   {product}
    uintx Newratio                                  = 2                                   {product}
... Java version "1.8.0_91"
Java (tm) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot (tm) 64-bit Server VM (b Uild 25.91-b14, Mixed mode)
parameter Explanation
Parameters function
-xx:initialsurvivorratio Initial proportions of Cenozoic Eden/survivor space
-xx:newratio Memory ratio of old and Yong areas
A calculation question

Under Default parameters, if only 40M is given in the Eden area, the total space size of the heap is calculated

According to the proportion can calculate, two survivor area each 5M, young generation 50M. The old age is twice times that of the younger generation, or 100M. Then the total size of the heap is 150M. 4.2 string constant pool

JDK1.7 began to "go to the permanent generation" of the work. 1.7 The string constant pool is stripped out of the permanent generation and stored in heap space. A. JVM parameter configuration

-xx:maxpermsize=10m
-xx:permsize=10m
-xms100m
-xmx100m
-xx:-usegcoverheadlimit
B. Test code
public class Stringoommock {public

    static void Main (string[] args) {
        try {
            list<string> List = new Arra Ylist<string> ();
            for (int i = 0;; i++) {
                System.out.println (i);
                List.add (string.valueof ("String" + i++). Intern ());
            }
        catch (Java.lang.Exception e) {
            e.printstacktrace ();}}}
c. Results of operation under jdk1.6

jdk1.6 Environment is a permanent generation of oom

153658
153660
Exception in thread "main" java.lang.OutOfMemoryError:PermGen spaces
    at Java.lang.String.intern (Native method) at
    Com.jd.im.StringOomMock.main (stringoommock.java:17)
D. Results of operation under jdk1.7

jdk1.7 under the heap oom, and accompanied by frequent FULLGC, the CPU has been running high

2252792
2252794
2252796
2252798 java.lang.instrument assertion
: "FAILED" With the message can ' t create name string at.. /.. /.. /src/share/instrument/jplisagent.c line:807
Exception in thread "main" Java.lang.OutOfMemoryError:Java heap spaces At
    Java.nio.CharBuffer.wrap (charbuffer.java:369) at
    Sun.nio.cs.StreamEncoder.implWrite ( streamencoder.java:265) at
    sun.nio.cs.StreamEncoder.write (streamencoder.java:125)
    at Java.io.OutputStreamWriter.write (outputstreamwriter.java:207) at
    Java.io.BufferedWriter.flushBuffer ( bufferedwriter.java:129) at
    java.io.PrintStream.write (printstream.java:526)
    at Java.io.PrintStream.print (printstream.java:597) at
    java.io.PrintStream.println (printstream.java:736)
    At Com.jd.im.StringOomMock.main (stringoommock.java:16)
E. Results of operation under jdk1.8

The result of the jdk1.8 is the same as 1.7, the heap space oom.

2236898
2236900
2236902
Exception in thread "main" Java.lang.OutOfMemoryError:Java heap spaces
    at Java.lang.Integer.toString (integer.java:403) at
    java.lang.String.valueOf (string.java:3099)
    at Java.io.PrintStream.print (printstream.java:597) at
    java.io.PrintStream.println (printstream.java:736)
    at Com.jd.im.StringOomMock.main (STRINGOOMMOCK.JAVA:16)
5. Metadata area

The metadata area replaces the 1.7 version and the previous permanent generation. The metadata area and the permanent generation are essentially implementations of the method area. The method area holds the class information loaded by the virtual machine, static variables, constants and other data.
Metadata Area Oom test: A. JVM parameter configuration

-xx:metaspacesize=8m 
-xx:maxmetaspacesize=50m
B. Test code

Generate a new class with the Cglib framework.

public class Metaspaceoommock {public static void main (string[] args) {Classloadingmxbean Loadingbean = Mans
        Agementfactory.getclassloadingmxbean ();
            while (true) {Enhancer enhancer = new enhancer ();
            Enhancer.setsuperclass (Metaspaceoommock.class);
            Enhancer.setcallbacktypes (New Class[]{dispatcher.class, methodinterceptor.class}); Enhancer.setcallbackfilter (New Callbackfilter () {@Override public int accept (method Metho
                D) {return 1; @Override public boolean equals (Object obj) {return super.equals (o)
                BJ);

            }
            });
            Class clazz = Enhancer.createclass ();
            System.out.println (Clazz.getname ()); Displays quantity information (the number of types that have been loaded, the number of types that are currently valid, the number of types that have been unloaded) System.out.println ("total:" + loadingbean.gettotalloadedclasscount (
            )); System.out.println ("active:" + loadingbean.getloadedclasscount ());
        System.out.println ("unloaded:" + loadingbean.getunloadedclasscount ()); }
    }
}
c. Run output:
Jvm. metaspaceoommock$ $EnhancerByCGLIB $$567f7ec0 total:6265 active:6265 unloaded:0 JVM. metaspaceoommock$ $EnhancerByCGLIB $$3501581b total:6266 active:6266 unloaded:0 Exception in thread "main" net.sf.cglib. Core. Codegenerationexception:java.lang.reflect.invocationtargetexception-->null at Net.sf.cglib.core.AbstractClassGenerator.generate (abstractclassgenerator.java:345) at Net.sf.cglib.proxy.Enhancer.generate (enhancer.java:492) at net.sf.cglib.core.abstractclassgenerator$ Classloaderdata$3.apply (abstractclassgenerator.java:93) at net.sf.cglib.core.abstractclassgenerator$ Classloaderdata$3.apply (abstractclassgenerator.java:91) at Net.sf.cglib.core.internal.loadingcache$2.call ( loadingcache.java:54) at Java.util.concurrent.FutureTask.run (futuretask.java:266) at Net.sf.cglib.core.internal.Lo Adingcache.createentry (loadingcache.java:61) at Net.sf.cglib.core.internal.LoadingCache.get (loadingcache.java:34 ) at Net.sf.cglib.core.abstractclassgenerator$claSsloaderdata.get (abstractclassgenerator.java:116) at Net.sf.cglib.core.AbstractClassGenerator.create ( abstractclassgenerator.java:291) at Net.sf.cglib.proxy.Enhancer.createHelper (enhancer.java:480) at NET.SF.CGLIB.PR Oxy. Enhancer.createclass (enhancer.java:337) at JVM. Metaspaceoommock.main (metaspaceoommock.java:38) caused by:java.lang.reflect.InvocationTargetException at Sun.reflect.GeneratedMethodAccessor1.invoke (Unknown Source) at Sun.reflect.DelegatingMethodAccessorImpl.invoke ( delegatingmethodaccessorimpl.java:43) at Java.lang.reflect.Method.invoke (method.java:498) at Net.sf.cglib.core.Ref Lectutils.defineclass (reflectutils.java:413) at Net.sf.cglib.core.AbstractClassGenerator.generate ( abstractclassgenerator.java:336) ... caused by:java.lang.OutOfMemoryError:Metaspace at Java.lang.ClassLoader.defineClass1 (Native method) at J Ava.lang.ClassLoader.defineClass (classloader.java:763) ... More

If the JDK is 1.7, then the oom will be the PermGen area. 6. Direct Memory

jdk1.4 introduced NIO, which can be used to directly allocate the outer heap of memory using the native function library.

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.