JVM-- memory structure

 

A program counter / PC register ( Program Counter Registe )

  To save the currently executing program memory address (execution address of the next instruction jvm) , due to the Java support multiple threads of execution, it is impossible to track the program execution is always linear actuator. When there are multiple execution threads cross, the thread of the currently executing program is interrupted address where memory is bound to be saved for use again to continue execution in accordance with the instruction address was interrupted when the interrupted thread resumes execution. To post a thread switch can be restored to the correct location of execution, each thread needs to have a separate program counter , counter independently of each other between threads, independent store, we call this type of memory area for the "thread private" memory, It is thread-safe.

  Features: 1. 2. thread private memory overflow does not exist

Second, the virtual machine stack (Java Virtual Machine Stack)

  VM stack is always associated with the threads together, whenever a thread is created, JVM will create a corresponding virtual machine stack for the thread , in this virtual machine stack will contain multiple stack frames (Stack Frame) , these stack frames are associated with each method together, each running a method which creates a stack frame , each stack frame will contain some local variables, stack, and the operation information methods return values. Whenever a method execution is completed, it will pop up the stack frame element stack frame as the return value, and clears the stack frame, stack virtual machine stack stack frame stack activity is currently executing, that is, the method is currently being executed, PC register will point to this address. (Only one stack is active) only local variable stack frame of the stack operation activity may be used when calling a method in the further stack frame, corresponding to a new stack frame is created, the newly created stack Java stack frames are placed in the stack, the stack becomes the current activities. Now only the same stack of local variables can be used when the stack frame all the instructions are complete, the stack frame is removed virtual machine stack, the stack frame just becomes active stack frame, the return value of the previous stack frame this becomes an operand stack operation of the stack frame 

 2.1 Features:

  1. Each thread contains a stack area, stack saves only method (not including members of the variable object) basic data types and self-referential (not an object) definition of an object, the object are stored in the stack area
  2. Data (primitive types and object references) are private, others can not access stacks each stack.
  3. Stack is divided into three parts: basic types of variables, and the execution environment context, operation instruction area (storage operation instruction).

 2.2  Issue Analysis:

  1. Garbage collection involves the stack memory?  Does not involve, stored in a stack memory stack frames, a stack frame when executed, the virtual machine stack frame will pop the stack, and then emptied

  2. Stack memory allocation bigger the better it? No, total memory is certain, when the big stack memory allocation, will reduce the maximum number of threads

  Local variables 3. whether the method thread safe?

    (1) . If a local variable within the method did not flee the role of access methods, it is thread safe

    (2) If it is a local variable refers to an object, scope and method of escape, need to consider thread safety

    

 

  2.3 stack memory overflow

    the reason:

    1. The stack frame due to excessive stack memory overflow

    2. The stack frame is too large to cause a stack overflow memory

    Above all belong to the stack is greater than the depth of the thread requested virtual machine allowable depth , it will be reported StackOverflowError exception if the virtual machine can dynamically expand, not enough memory to apply if the extension will throw OutOfMemoryError exception.

Third, native method stacks (Native Method Stacks)

  Native method stacks and Java (VM) stack is very similar to the role, but the difference is the Java stack for the JVM implementation of Java service method, and the local Native method stacks for the JVM execution method service. Native method stacks StackOverflowError and an OutOfMemoryError will be thrown.

  Things on the concepts defined are the JVM specification maybe, not to say that really give concrete realization of JVM for each Java thread to open two separate stacks. To Oracle JDK / OpenJDK's HotSpot VM, for example, which uses so-called "mixed stack" - in the same stack frame stack frame and native methods of the stack to store Java method calls, so each Java thread is only one call stack combines the JVM specification JVM stack and native method stacks and maybe concept .
Fourth, the heap (Heap)
   China heap memory is the biggest piece of the JVM management is shared by all Java thread locks, created when starting the JVM is not thread safe . Java objects are heap memory of the place, which is described in the Java Virtual Machine Specification is: all object instances and arrays to be allocated on the heap. Java heap is the main area of the GC management , from the perspective of memory recovery point of view, because the GC is now basically using generational collection algorithm, so the Java heap can also be broken down into: the old and the new generation's; the new generation has again more carefully Eden space, From Survivor space, To Survivor space
   4.1 Features:

  1. stored all object instances, each object contains information about a class corresponding to the (class information stored in the method area).

  2.jvm only a heap (heap) is shared by all threads, the heap does not store the basic types and object references, only store the object itself, almost all object instances and arrays are allocated on the heap.

  3. No consecutive memory can choose a fixed size, scalability

  4.2 heap overflow

    When there is no available memory heap allocation completion example, and can no longer expand

      

  4.3 Heap Memory Diagnostic

    1. jps tool

       View the current system in which the process of java

    2. jmap Tools

        View heap memory usage jmap - heap process id

    3. jconsole tool

        Graphical interface, versatile monitoring tool that can continuously monitor

Fifth, the method area

  Also known as a static area, with the same heap, shared by all threads . It has for storing class information of the virtual machine is loaded, a constant, static variables, the compiler code for the instant when the other data (such as spring or AOP IOC created using the bean, or use CGLIB, dynamically generated class information is reflected in the form of the like ).

  5.1 Composition

  

  由上图可知,jdk1.6的时候,方法区是通过永久代实现的。而对于jdk1.8,方法区是通过元空间实现的。存储位置不同,永久代物理是是堆的一部分,和新生代,老年代地址是连续的,而元空间属于本地内存;

  注:JDK 6 时,String等字符串常量的信息是置于方法区中的,但是到了JDK 7 时,已经移动到了Java堆。

  5.2 方法区内存溢出

  永久代内存溢出  :java.lang.OutOfMemoryError: PermGen space 

       -XX   :  MaxPermSize=8m

  

  元空间内存溢出  : java.lang.OutOfMemoryError: Metaspace 

        -XX   :   MaxMetaspaceSize=8m

  5.3 常量池

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

  5.4 运行时常量池

  运行时常量池是方法区的一部分,常量池是 *.class 文件中的,当该类被加载,它的常量池信息就会放入运行时常量 池,并把里面的符号地址变为真实地址,一个类加载到 JVM 中后对应一个运行时常量池 

  5.5 字符串常量池(StringTable)

   5.5.1 注意

    1.  在JDK1.7之前运行时常量池逻辑包含字符串常量池存放在方法区, 此时hotspot虚拟机对方法区的实现为永久代
    2. 在JDK1.7 字符串常量池被从方法区拿到了堆中, 这里没有提到运行时常量池,也就是说字符串常量池被单独拿到堆,运行时常量池剩下的东西还在方法区, 也就是hotspot中的永久代
    3. Removed JDK1.8 hotspot permanent substitute element space (Metaspace) Instead, this time the string constant pool is still heap , runtime constant pool is still the method area , but the implementation on behalf of the district from the permanent space becomes yuan (Metaspace) 

   5.5.2 Features:

        1. String constant pool is only a symbol, when used for the first time become the object

      2. The use of the mechanisms of cell strings, string objects to avoid recreating

      3. string variable stitching principle is StringBuilder (1.8)

      4. The  principle is to compile a string constant splice optimization

      5. Using the intern method, the pool has not actively string string string objects in the pool

        1.8 try this string object into the string pool, if you do not put in, if not then put the string pool, the object will return the string pool

        1.6 try this string object into the string pool, if you do not put in, if not put a copy of this object, into the string pool, the object will return the string pool

      6 may be garbage collected

      7 .hashtable implementation

    5.5.3 tuning
      adjustment -XX: StringTableSize = number of barrels 
  
     5.5.4 face questions
String s1 = "a"; 
String s2 = "b"; 
String s3 = "a" + "b";
 String s4 = s1 + s2;
 String s5 = "ab"; 
String s6 = s4.intern();
//
System.out.println(s3 == s4);
 System.out.println(s3 == s5); 
System.out.println(s3 == s6);
String x2
= new String("c") + new String("d"); String x1 = "cd";
x2.intern();
// 问,如果调换了【最后两行代码】的位置呢,如果是jdk1.6呢 System.out.println(x1 == x2);

  共6个答案:false true true false  true false

六、直接内存(Direct Memory)

  NIO(New Input/Output)类,引入了一种基于通道(Channel)与缓冲区(Buffer)的I/O方式,它可使用Native函数库直接分配堆外内存。不受Java堆大小(-Xmx)限制,从而可能造成各个内存区域总和大于物理内存限制而造成动态扩展时出现OutOfMemoryError。

  6.1 特点:

  1.常见于 NIO 操作时,用于数据缓冲区

  2.分配回收成本较高,但读写性能高

  3.不受 JVM 内存回收管理

  6.2 分配和回收原理

  1.使用了 Unsafe 对象完成直接内存的分配回收,并且回收需要主动调用 freeMemory 方法

  2. ByteBuffer 的实现类内部,使用了 Cleaner (虚引用)来监测 ByteBuffer 对象,一旦 ByteBuffer 对象被垃圾回收,那么就会由 ReferenceHandler 线程通过 Cleaner 的 clean 方法调 用 freeMemory 来释放直接内存

  

 

 

 

 



Guess you like

Origin www.cnblogs.com/xiao-ran/p/11888509.html