Java内部存款和储蓄器区域与内部存款和储蓄器溢出12分,垃圾回收机制

1.连锁概念

Java虚拟机,以下简称JVM。

1.开篇

Arvin笔者又来写技术博文啦,做个细微笔记,顺便给一些人扫扫除文盲,方今对Java
的GC(垃圾回收机制)尤其感兴趣。很已经知道在C/C++
那类语言中须要程序猿自个儿去操作对内部存款和储蓄器的田管 什么 alloc,
dealloc啦之类的。当初学OC的时候也有诸如此类一章节。但是说到java很少有人会说到管理内部存款和储蓄器那块文化。java相比较C语言在内部存款和储蓄器管理这块先进了成都百货上千,因为java的内部存款和储蓄器是机动管理的,光机关那几个词就领悟很高档有木有。可是java是怎么去创制和刑满释放解除劳教内部存款和储蓄器的啊。这几个很有须求扒一扒,究竟听闻有些面试上来就问java的GC是怎么work的。还有便是这一块属于jvm的基本知识。

JVM在进行java程序的历程中会把它所管理的内部存款和储蓄器划分为多少个区域。

2.预备知识

那篇小说只即使为前面包车型客车GC分析来做基础知识扫除文盲的,主假若讲jvm的内部存款和储蓄器分配。之前提到过jvm的内存就分2块
一个栈(stack)1个堆(Heap),其实这一个是错的,没有那样简单,依旧有点小复杂的,好了来扒一扒jvm的内部存款和储蓄器

那几个区域有些的生命周期。有的正视于JVM,有的信赖于用户线程。

2.JVM内存

6块区域整合。

1.结构

必威电竞 1

这张图纸注明了,当运营java程序的时候
jvm会产生的内部存款和储蓄器结构,而笔者辈平昔所说的stack 和heap 正是呼应的jvm
stack和heap(heap中的新生代和老时代那篇文章中不介绍,前边一篇GC分析的时候
会去详细介绍,近来就把她作为jvm就好啊)

程序计数器 The pc Register

JVM能够同时扶助广大实施线程。各类JVM线程有自身的主次计数器。任曾几何时候,每一种JVM线程执行单个方法的代码,叫做这些线程的近期情势。要是不行格局不是本土的,那么程序计数器包含当前JVM正在执行的指令地址。假设被这些线程当前执行的主意是本地的,那么JVM的主次计数器的值是undefined。JVM的次序计数器充分去持有二个回去地址大概三个当地指针在钦赐的阳台。翻译自JVM规范。

简易的话

JVM执行非本地点法,它的程序计数器存指令地址。

JVM执行本位置法,它的次序计数器存值undefined。

tag:线程隔断的数据区

1)程序计数器(Program counter Register)

The Java Virtual Machine can support many threads of execution at
once. Each Java Virtual Machine thread has its own pc (program
counter) register. At any point, each Java Virtual Machine thread is
executing the code of a single method, namely the current method for
that thread. If that method is not native, the pc register contains
the address of the Java Virtual Machine instruction currently being
executed. If themethodcurrently being executed by the thread is native
, the value of the Java Virtual Machine’s pc register is undefined.
The Java Virtual Machine’s pc register is wide enough to hold a
returnAddress or a native pointer on the specific platform.

java官网给的表明,学过汇编的小伙伴应该知道cpu中的pc register
用来储存指令的地点。 其实java中的pc
register的法则和汇编中的分化等只是做的是同一件事,正是记录了当下在运行指令的地方。如若java程序运维的是native
language的吩咐则pc 里面存的是未定义。
其实pc的尺寸能够忽略不计因为里面存的数据量太小了。重点是要留意一下,pc
register是线程私有的,所谓的线程私有正是种种线程有3个相应的pc
register,所以唯有线程伊始的时候 pc reigster才会成立,线程甘休了 pc
register自然就木有了。

Java虚拟机栈 Java Virtual Machine Stacks

各样JVM线程有1个私家JVM栈,与线程一起被创制。2个JVM栈存frames。一个JVM栈是近似于古板语言C的栈:它有本土变量和有个别变量,在章程调用和重返中起着职能。因为JVM栈从不直接被操作除了放和取frames,frames依据堆放置。对于JVM栈内部存款和储蓄器不需求用到。

首先版java虚拟机规范中,JVM栈叫Java栈。

以此正式允许JVM栈是稳定大小大概是动态扩充以及依照测算的获取。

比方JVM栈大小是固定的,各样JVM栈被单独的选项当栈被创立时。

种种JVM完成能够提供程序依旧用户控制基于JVM栈的开端大小,同样的动态扩张的或许contractingJVM栈,控制在最大和纤维大小。

上面是JVM栈的可怜条件

必威电竞,如果总计后线程供给1个更大的JVM栈比起所允许的,JVM抛出StackOverflowError.

万一JVM栈是动态扩充的,当扩大中绝非丰盛的内部存储器依旧尚未充足的内部存款和储蓄器去初步化JVM栈的二个线程,JVM抛出OutOfMemoryError

一言以蔽之的话

JVM中每一个线程有1个JVM栈,与线程一起被成立。种种JVM栈用来存frames。

frame is used to store data and partial results, as well as to
perform dynamic linking, return values for methods, and dispatch
exceptions.

 Local Variables

Operand Stacks

 Dynamic Linking

Normal Method Invocation Completion

Abrupt Method Invocation Completion

frames

2)VM stack(vm 栈)

Each Java Virtual Machine thread has a private Java Virtual Machine
stack
, created at the same time as the thread. A Java Virtual Machine
stack stores frames. A Java Virtual Machine stack is analogous to the
stack of a conventional language such as C: it holds local variables
and partial results, and plays a part in method invocation and return.
Because the Java Virtual Machine stack is never manipulated directly
except to push and pop frames, frames may be heap allocated. The
memory for a Java Virtual Machine stack does not need to be
contiguous.

stack 这一个东西呢 也是线程私有的,随线程生随线程死。其实stack
这些东西还有下级,正是stack frame。 stack frame
是针对与方法的,不难的来说,每个method被实践的时候会创制一个stack
frame 被push到vm stack 中,方法被执行到位之后会pop出vm
stack。真正存数据的地点实际上是stack frame。vm stack类似3个汇集。
stack frame中存了二种东西:

  • Local Vairable
  1. 基本项目(int,long,char,boolean…….)
  2. 指标类型的引用
  3. returnAddress的类型
  • Operand Stack
  • data frame
    假设请求vm stack 大于了
    vm可以成熟的深浅,java会抛出stackoverflowerror的不行

Native Stack 和stack的用法大约,但是是给java程序中跑native
language的时候利用的

java 堆 Heap

The Java Virtual Machine has a heap that is shared among all Java
Virtual Machine threads. The heap is the run-time data area from which
memory for all class instances and arrays is allocated.

The heap is created on virtual machine start-up. Heap storage for
objects is reclaimed by an automatic storage management system (known as
garbage collector); objects are never explicitly deallocated. The
Java Virtual Machine assumes no particular type of automatic storage
management system, and the storage management technique may be chosen
according to the implementor’s system requirements. The heap may be of a
fixed size or may be expanded as required by the computation and may be
contracted if a larger heap becomes unnecessary. The memory for the heap
does not need to be contiguous.

A Java Virtual Machine implementation may provide the programmer or the
user control over the initial size of the heap, as well as, if the heap
can be dynamically expanded or contracted, control over the maximum and
minimum heap size.

The following exceptional condition is associated with the
heap:

If a computation requires more heap than can be made available by the
automatic storage management system, the Java Virtual Machine throws
an OutOfMemoryError.

3)Heap(堆)

The Java Virtual Machine has a heap that is shared among all Java
Virtual Machine threads. The heap is the run-time data area from which
memory for all class instances and arrays is allocated.
The heap is created on virtual machine start-up. Heap storage for
objects is reclaimed by an automatic storage management system (known
as a garbage collector); objects are never explicitly deallocated.
The Java Virtual Machine assumes no particular type of automatic
storage management system, and the storage management technique may be
chosen according to the implementor’s system requirements. The heap
may be of a fixed size or may be expanded as required by the
computation and may be contracted if a larger heap becomes
unnecessary. The memory for the heap does not need to be contiguous.

堆是占内部存储器最大一块的地点,他是线程共享的也便是说在java程序运转的时候创立的直到java程序结束。主倘使存放实例对象和数组数据。也及时GC产生最多的地点。其余说一点
堆中存放的多少的大体地址不是连接的
学过汇编的伙伴应该可以通晓。假诺这么些地方须要的轻重庆大学于了vm
所承受的高低会保outofmemoryerror 也正是传说中的OOM

方法区 Method Area

The Java Virtual Machine has a method area that is shared among all
Java Virtual Machine threads. The method area is analogous to the
storage area for compiled code of a conventional language or analogous
to the “text” segment in an operating system process. It stores
per-class structures such as the run-time constant pool, field and
method data, and the code for methods and constructors, including the
special methods
(§2.9)
used in class and instance initialization and interface initialization.

The method area is created on virtual machine start-up. Although the
method area is logically part of the heap, simple implementations may
choose not to either garbage collect or compact it. This specification
does not mandate the location of the method area or the policies used to
manage compiled code. The method area may be of a fixed size or may be
expanded as required by the computation and may be contracted if a
larger method area becomes unnecessary. The memory for the method area
does not need to be contiguous.

A Java Virtual Machine implementation may provide the programmer or the
user control over the initial size of the method area, as well as, in
the case of a varying-size method area, control over the maximum and
minimum method area size.

The following exceptional condition is associated with the method
area:

If memory in the method area cannot be made available to satisfy an
allocation request, the Java Virtual Machine throws an OutOfMemoryError.

4)Method Area(方法区)

The Java Virtual Machine has a method area that is shared among all
Java Virtual Machine threads. The method area is analogous to the
storage area for compiled code of a conventional language or analogous
to the “text” segment in an operating system process. It stores
per-class structures such as the run-time constant pool, field and
method data, and the code for methods and constructors, including the
special methods used in class and instance initialization and
interface initialization.

方法区也是线程共享的首尽管用来囤积 vm
已经加载号的类音讯,静态变量,等等。同时在方块区里面有一块是
常量池,也正是大家日常用final成立出来的变量都会被放到那些里面。那几个地方时有发生GC相比较少,可是假诺当先大小也会抛出OOM的不得了

运营时常量池 Run-Time Constant Pool

run-time constant pool is a per-class or per-interface run-time
representation of the constant_pool table in a class file
(§4.4).
It contains several kinds of constants, ranging from numeric literals
known at compile-time to method and field references that must be
resolved at run-time. The run-time constant pool serves a function
similar to that of a symbol table for a conventional programming
language, although it contains a wider range of data than a typical
symbol table.

运维时常量池是各类类照旧每一个接口在类公事的常量池表的周转时表示。他带有了二种常量,从编写翻译时的数字文字到点子和引用字段,必须在运营时化解。运营时常量池服务方程式类似高璇规编制程序语言的符号表,就算它包括了3个大范围的数量而不是名列三甲的符号表。

Each run-time constant pool is allocated from the Java Virtual
Machine’s method area
(
§2.5.4).
The run-time constant pool for a class or interface is constructed when
the class or interface is created
(§5.3)
by the Java Virtual Machine.

The following exceptional condition is associated with the construction
of the run-time constant pool for a class or
interface:

When creating a class or interface, if the construction of the run-time
constant pool requires more memory than can be made available in the
method area of the Java Virtual Machine, the Java Virtual Machine throws
an OutOfMemoryError.

See §5 (Loading, Linking, and
Initializing
)
 for
information about the construction of the run-time constant pool.

2.栗子

上边介绍了一多元的内部存款和储蓄器分布每一块都有协调的效率和特征,我们接下去拿2个板栗来作为实例分析一下:

Object obj = new Object();

大概吗,但是深扒仍可以扒出很多东西的。拆分成2块来展开分析。
Object obj 在对应的stack frame中的local
variable表中以reference类型出现。
new Object()呢
在heap中开拓了一块以存款和储蓄object类型全数实例数据的内部存款和储蓄器。heap中还非得含有相应的靶子型数据类型(interface,super
class,class….)
reference
里面便是2个对准对象的引用所以未来的难点正是什么样把他们1个链接起来(=)。有2种办法能够链接,差异的vm选用不一样的情势:
措施1)指向heap中的句炳,然后由句炳指向实际的实例,什么看头啊,正是直接指向。若是是使用那种情势那么在heap中必将会开出一块存放句炳的内部存款和储蓄器

heap中2块地点 句炳池和实例池,句炳用来找到实例和对象类型数据
措施2)直接待上访问,正是撤废了句炳了。直接存款和储蓄实例的地点,直接待上访问到实例数据

两边的得失:句炳的话,假诺数额发现改变移动
reference里面包车型大巴值是不会变的。直接访问的话效用更好速度更快,sum hotspot
vm正是用的第贰手访问.

率先篇小说到此地结束啦。主要介绍了vm的内部存储器分配,假若我们要测试oom能够修改vm对应的参数。

当地点法栈 Native Method Stacks

An implementation of the Java Virtual Machine may use conventional
stacks, colloquially called “C stacks,” to support native methods
(methods written in a language other than the Java programming
language). Native method stacks may also be used by the implementation
of an interpreter for the Java Virtual Machine’s instruction set in a
language such as C. Java Virtual Machine implementations that cannot
load nativemethods and that do not themselves rely on conventional
stacks need not supply native method stacks. If supplied, native method
stacks are typically allocated per thread when each thread is created.

This specification permits native method stacks either to be of a fixed
size or to dynamically expand and contract as required by the
computation. If the native method stacks are of a fixed size, the size
of each native method stack may be chosen independently when that stack
is created.

A Java Virtual Machine implementation may provide the programmer or the
user control over the initial size of the native method stacks, as well
as, in the case of varying-size native method stacks, control over the
maximum and minimum method stack sizes.

The following exceptional conditions are associated with native method
stacks:

If the computation in a thread requires a larger native method stack
than is permitted, the Java Virtual Machine throws
a StackOverflowError.

If native method stacks can be dynamically expanded and native method
stack expansion is attempted but insufficient memory can be made
available, or if insufficient memory can be made available to create the
initial native method stack for a new thread, the Java Virtual Machine
throws an OutOfMemoryError.

参考

深刻驾驭Java虚拟机 周志明 著

JVM规范链接

相关文章

admin

网站地图xml地图