java virtual machine - memory management

 
 
 
native method
Java methods implemented by non-java code, using the native keyword, can be used in conjunction with the java keyword except abstract
program counter
Can be thought of as a line number indicator of the bytecode being executed by the current thread. When the bytecode interpreter works, it selects the next bytecode instruction to be executed by changing the value of this counter, branching, looping, jumping, exception handling, thread recovery and other basic functions. This area is an area where the outofmeroryError condition is not specified in the java virtual machine specification
virtual machine stack
 
1. The life cycle is consistent with the thread
2. The stack memory area that programmers usually refer to, more precisely refers to the local variable table part in the virtual machine stack
3. The local variable table stores various basic data types of the compiler, object references
4. When the stack depth requested by the thread is greater than the depth allowed by the virtual machine, a stackOverFlowError is thrown
5. When the virtual machine cannot apply for enough memory during expansion, it will throw OutOfMemoryError
6. The virtual machine stack describes the memory model of java method execution: each method will create a stack frame when it is executed, which is used to store the local variable table, operand stack, dynamic link, method exit and other information . Each method from invocation to completion of execution corresponds to the process of a stack frame being pushed to the stack in the virtual machine stack.
native method stack
1. The function is the same as the virtual machine stack, except that the native method is executed in this area.
2. Like the virtual machine stack, stackOverFlowError and outOfMemoryError will be thrown
3. Some virtual machines combine the local method stack and the virtual machine stack into one
Java heap
1. The largest piece of memory managed by the Java virtual machine
2. Store almost all object instances
3. The main management area of ​​the GC manager
4、根据GC的分代算法将堆分为新生代和老年代,或者更细的分为Eden空间,From Suvivor空间,To Suvivor空间
5、堆为线程共享区
6、从内存分配的角度来看,堆中可以划分出多个线程私有的分配缓冲区(TLAB)
7、对内存的划分是为了更好的回收内存和分配内存
8、Java堆在逻辑上是连续的,在物理上可以不连续
9、大小可调,通过-Xmx和-Xms参数调节大小
10、当堆中没有内存分配给新创建的对象实例,并且堆无法再扩展的时候就会抛出outOfMemoryError
方法区
1、线程共享区
2、存储虚拟机加载的类信息,常量,静态变量,即时编译器编译后的代码
3、俗称永久代
4、通过参数-XX:MaxPermSize来限制方法区的大小
5、该区域的内存回收主要针对常量池和对类型的卸载
6、当该区无法满足内存分配的需求的时候会抛出oytOfMemoryError
运行时常量池
1、方法区的一部分
2、class文件中存放编译期生成的各种字面量和符号的引用的常量池信息,在加载完后放入运行时常量池
3、抛出outofMemoryError的情况同方法区
直接内存
1、不是虚拟机运行时数据区的一部分
2、不受堆内存大小的限制
3、NIO使用Native函数库直接操作堆外内存提高性能,但是如果堆内存分配不合理,就会导致动态扩展的时候抛出outOfMemoryError
 
内存分配与回收策略
  • 对象优先在新生代Eden区分配
          When most objects are created, they will allocate space in the Eden space of the new generation. When there is not enough space in the Eden area, the JVM will initiate a Minor GC
  • Large objects go directly to the old age
          Large objects refer to Java objects that require a large amount of contiguous memory space. Typical large objects are long strings and long arrays. Large objects often lead to memory space remaining, but GC is triggered in advance to make room for them. The virtual machine provides the parameter -XX:PretenureSizeThreshold. Objects that require more memory than this parameter value are directly allocated in the old age.
  • Long-lived objects will enter the old age
          The virtual machine defines an age counter for each object. When the object is born in the Eden area and still survives after a Minor GC and can be accommodated by the Survivor, the object will be moved to the Survivor area and the age of the object will increase by one. When the age reaches a certain value (the default is 15), it will be moved to the old age. If the old age cannot be stored, a Full GC will be initiated. For the age threshold of the object promoted to the old age, it can be set by the parameter -XX:MaxTenuringThresHold
  • Dynamic object age judgment
           Under normal circumstances, objects in the new generation must reach the age of the object before they are put into the old generation. However, if the total memory occupied by objects of the same age in the Survivor area is greater than half of the Survivor space, then the objects of Age >= this age are directly placed in the old generation.
  • space allocation guarantee
          Before Minor GC occurs, the virtual machine first checks whether the largest contiguous space in the old generation is larger than the total space of all objects in the new generation. If it is larger than the total space of all objects in the new generation, the Minor GC can be guaranteed to be safe. If not, the virtual machine will check -XX:HandlePromotionFailure The value of whether the guarantee is allowed to fail, if allowed, a Minor GC will be initiated, although this GC is not safe, if not, a Full GC will be initiated
 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326989576&siteId=291194637