JVM

16-方法区

2021-04-30  本文已影响0人  紫荆秋雪_文

一、栈、堆、方法区

image.png

二、设置方法区大小与OOM

1、JDK7及以前

-XX:PermSize
-XX:MaxPermSize

2、JDK8及以后

-XX:MetaspaceSize
-XX:MaxMetaspaceSize

三、方法区内部结构

image.png

3.1、方法区中存储什么

《深入理解 Java 虚拟机》一书中对方法区(Method Area)存储内容描述如下:方法区用于存储已被虚拟机加载的 类型信息、常量、静态变量、即时编译器编译后的代码缓存等 方法区中存储内容.png

3.1.1、类型信息

3.1.2、域(Field)信息

3.1.3、方法(Method)信息

JVM必须保存所有方法的一下信息,同域信息一样包括声明顺序

3.1.4、non-final的类变量

3.1.5、全局常量(static final)

被声明为 final 的类变量的处理方法则不同,每个全局常量在编译的时候就会被分配了

方法区的内部结构

/**
 * 方法区的内部结构
 */
public class MethodAreaTest extends Object
        implements IMethodArea1, IMethodArea2  {

    // 属性
    public int num = 10;
    private static String str = "方法区的内部结构";

    public void test1() {
        int count = 20;
        System.out.println("count=" + count);
    }

    public static int test2(int cal) {
        int result = 0;
        try {
            int value = 30;
            result = value / cal;
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public static void main(String[] args) {

    }
    @Override
    public void getMethodArea1() {
        System.out.println("MethodArea1");
    }

    @Override
    public void getMethodArea2() {
        System.out.println("MethodArea2");
    }
}
javap -v -p MethodAreaTest
javap -v -p MethodAreaTest > MethodAreaTest.txt
Classfile /Users/zijingqiuxue/Desktop/Code/Code_2021/JVM/07_MethodAreaTest/out/production/07_MethodAreaTest/com/lkty/method/MethodAreaTest.class
  Last modified 2021-5-1; size 1607 bytes
  MD5 checksum 72c10a1ecdbdf862a2bd852ca8d52dca
  Compiled from "MethodAreaTest.java"

// TODO: 全限定类名
public class com.lkty.method.MethodAreaTest 
      implements com.lkty.method.IMethodArea1,com.lkty.method.IMethodArea2
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER

// TODO: 常量池
Constant pool:
   #1 = Methodref          #18.#52        // java/lang/Object."<init>":()V
   #2 = Fieldref           #17.#53        // com/lkty/method/MethodAreaTest.num:I
   #3 = Fieldref           #54.#55        // java/lang/System.out:Ljava/io/PrintStream;
   #4 = Class              #56            // java/lang/StringBuilder
   #5 = Methodref          #4.#52         // java/lang/StringBuilder."<init>":()V
   #6 = String             #57            // count=
   #7 = Methodref          #4.#58         // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   #8 = Methodref          #4.#59         // java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
   #9 = Methodref          #4.#60         // java/lang/StringBuilder.toString:()Ljava/lang/String;
  #10 = Methodref          #61.#62        // java/io/PrintStream.println:(Ljava/lang/String;)V
  #11 = Class              #63            // java/lang/Exception
  #12 = Methodref          #11.#64        // java/lang/Exception.printStackTrace:()V
  #13 = String             #65            // MethodArea1
  #14 = String             #66            // MethodArea2
  #15 = String             #67            // 方法区的内部结构
  #16 = Fieldref           #17.#68        // com/lkty/method/MethodAreaTest.str:Ljava/lang/String;
  #17 = Class              #69            // com/lkty/method/MethodAreaTest
  #18 = Class              #70            // java/lang/Object
  #19 = Class              #71            // com/lkty/method/IMethodArea1
  #20 = Class              #72            // com/lkty/method/IMethodArea2
  #21 = Utf8               num
  #22 = Utf8               I
  #23 = Utf8               str
  #24 = Utf8               Ljava/lang/String;
  #25 = Utf8               <init>
  #26 = Utf8               ()V
  #27 = Utf8               Code
  #28 = Utf8               LineNumberTable
  #29 = Utf8               LocalVariableTable
  #30 = Utf8               this
  #31 = Utf8               Lcom/lkty/method/MethodAreaTest;
  #32 = Utf8               test1
  #33 = Utf8               count
  #34 = Utf8               test2
  #35 = Utf8               (I)I
  #36 = Utf8               value
  #37 = Utf8               e
  #38 = Utf8               Ljava/lang/Exception;
  #39 = Utf8               cal
  #40 = Utf8               result
  #41 = Utf8               StackMapTable
  #42 = Class              #63            // java/lang/Exception
  #43 = Utf8               main
  #44 = Utf8               ([Ljava/lang/String;)V
  #45 = Utf8               args
  #46 = Utf8               [Ljava/lang/String;
  #47 = Utf8               getMethodArea1
  #48 = Utf8               getMethodArea2
  #49 = Utf8               <clinit>
  #50 = Utf8               SourceFile
  #51 = Utf8               MethodAreaTest.java
  #52 = NameAndType        #25:#26        // "<init>":()V
  #53 = NameAndType        #21:#22        // num:I
  #54 = Class              #73            // java/lang/System
  #55 = NameAndType        #74:#75        // out:Ljava/io/PrintStream;
  #56 = Utf8               java/lang/StringBuilder
  #57 = Utf8               count=
  #58 = NameAndType        #76:#77        // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #59 = NameAndType        #76:#78        // append:(I)Ljava/lang/StringBuilder;
  #60 = NameAndType        #79:#80        // toString:()Ljava/lang/String;
  #61 = Class              #81            // java/io/PrintStream
  #62 = NameAndType        #82:#83        // println:(Ljava/lang/String;)V
  #63 = Utf8               java/lang/Exception
  #64 = NameAndType        #84:#26        // printStackTrace:()V
  #65 = Utf8               MethodArea1
  #66 = Utf8               MethodArea2
  #67 = Utf8               方法区的内部结构
  #68 = NameAndType        #23:#24        // str:Ljava/lang/String;
  #69 = Utf8               com/lkty/method/MethodAreaTest
  #70 = Utf8               java/lang/Object
  #71 = Utf8               com/lkty/method/IMethodArea1
  #72 = Utf8               com/lkty/method/IMethodArea2
  #73 = Utf8               java/lang/System
  #74 = Utf8               out
  #75 = Utf8               Ljava/io/PrintStream;
  #76 = Utf8               append
  #77 = Utf8               (Ljava/lang/String;)Ljava/lang/StringBuilder;
  #78 = Utf8               (I)Ljava/lang/StringBuilder;
  #79 = Utf8               toString
  #80 = Utf8               ()Ljava/lang/String;
  #81 = Utf8               java/io/PrintStream
  #82 = Utf8               println
  #83 = Utf8               (Ljava/lang/String;)V
  #84 = Utf8               printStackTrace
{
  // TODO: 属性
  public int num;
    descriptor: I // int类型
    flags: ACC_PUBLIC // 修饰符

  // TODO: 属性
  private static java.lang.String str;
    descriptor: Ljava/lang/String;  // String类型
    flags: ACC_PRIVATE, ACC_STATIC  // 修饰符

  // TODO: 构造器
  public com.lkty.method.MethodAreaTest();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: aload_0
         5: bipush        10
         7: putfield      #2                  // Field num:I
        10: return
      LineNumberTable:
        line 6: 0
        line 10: 4
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      11     0  this   Lcom/lkty/method/MethodAreaTest;


  // TODO: 方法 test1
  public void test1();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=3, locals=2, args_size=1
         0: bipush        20
         2: istore_1
         3: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
         6: new           #4                  // class java/lang/StringBuilder
         9: dup
        10: invokespecial #5                  // Method java/lang/StringBuilder."<init>":()V
        13: ldc           #6                  // String count=
        15: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        18: iload_1
        19: invokevirtual #8                  // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
        22: invokevirtual #9                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        25: invokevirtual #10                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        28: return
      LineNumberTable:
        line 14: 0
        line 15: 3
        line 16: 28
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      29     0  this   Lcom/lkty/method/MethodAreaTest;
            3      26     1 count   I

  // TODO: 方法 test2
  public static int test2(int);
    descriptor: (I)I
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=3, args_size=1
         0: iconst_0
         1: istore_1
         2: bipush        30
         4: istore_2
         5: iload_2
         6: iload_0
         7: idiv
         8: istore_1
         9: goto          17
        12: astore_2
        13: aload_2
        14: invokevirtual #12                 // Method java/lang/Exception.printStackTrace:()V
        17: iload_1
        18: ireturn
      
      // TODO: 方法 异常
      Exception table:
         from    to  target type
             2     9    12   Class java/lang/Exception
      LineNumberTable:
        line 19: 0
        line 21: 2
        line 22: 5
        line 26: 9
        line 24: 12
        line 25: 13
        line 28: 17
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            5       4     2 value   I
           13       4     2     e   Ljava/lang/Exception;
            0      19     0   cal   I
            2      17     1 result   I
      StackMapTable: number_of_entries = 2
        frame_type = 255 /* full_frame */
          offset_delta = 12
          locals = [ int, int ]
          stack = [ class java/lang/Exception ]
        frame_type = 4 /* same */

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=0, locals=1, args_size=1
         0: return
      LineNumberTable:
        line 33: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       1     0  args   [Ljava/lang/String;

  public void getMethodArea1();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #13                 // String MethodArea1
         5: invokevirtual #10                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return
      LineNumberTable:
        line 36: 0
        line 37: 8
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       9     0  this   Lcom/lkty/method/MethodAreaTest;

  public void getMethodArea2();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #14                 // String MethodArea2
         5: invokevirtual #10                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return
      LineNumberTable:
        line 41: 0
        line 42: 8
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       9     0  this   Lcom/lkty/method/MethodAreaTest;

  static {};
    descriptor: ()V
    flags: ACC_STATIC
    Code:
      stack=1, locals=0, args_size=0
         0: ldc           #15                 // String 方法区的内部结构
         2: putstatic     #16                 // Field str:Ljava/lang/String;
         5: return
      LineNumberTable:
        line 11: 0
}
SourceFile: "MethodAreaTest.java"

3.2、运行时常量池&常量池

3.3、为什么需要常量池

一个Java源文件中的类、接口,编译后产生一个字节码文件。而Java中的字节码需要数据支持,通常这种数据会很大以至于不能直接存到字节码里,换一种方式,可以存到常量池,这个字节码包含了指向常量池的引用。在动态链接的时候会用到运行时常量池。

3.3.1、常量池中存储

3.3.2、小结

常量池,可以看做是一张表,虚拟机指令根据这张常量表找到要执行的类名、方法名、参数类型、字面量等类型

3.3.3、运行时常量池

3.3.4、方法区演示

package com.lkty.method;


public class MethodAreaDemo {

    public static void main(String[] args) {
        int x = 500;
        int y = 100;
        int a = x / y;
        int b = 50;
        System.out.println(a + b);
    }
}
 0 sipush 500
 3 istore_1
 4 bipush 100
 6 istore_2
 7 iload_1
 8 iload_2
 9 idiv
10 istore_3
11 bipush 50
13 istore 4
15 getstatic #2 <java/lang/System.out>
18 iload_3
19 iload 4
21 iadd
22 invokevirtual #3 <java/io/PrintStream.println>
25 return

四、方法区的演进过程

4.1、JDK6 JVM运行时数据区

JDK6 JVM运行时数据区.png

4.2、JDK7 JVM运行时数据区

JDK7 JVM运行时数据区.png

4.3、JDK8 JVM运行时数据区

JDK8 JVM运行时数据区.png

4.4、元空间为什么要替换永久代

4.5、StringTable为什么要调整

JDK7中将StringTable放到了堆空间中。因为永久代的回收效率很低,在Full GC的时候才会触发。而Full GC是老年代的空间不足、永久代不足时才会触发。这就导致StringTable回收效率不高。而我们开发中会有大量的字符串被创建,回收效率低,导致永久代内存不足。放到堆中,能即时回收内存

五、方法区的垃圾回收

上一篇下一篇

猜你喜欢

热点阅读