stack 那几个事物呢 也是线程私有的,有的正视于JVM

其实java中的pc,有的依赖于JVM

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)一个堆(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是线程私有的,所谓的线程私有就是逐个线程有一个遥相呼应的pc
register,所以只有线程开首的时候 pc reigster才会创设,线程截止了 pc
register自然就木有了。

Java虚拟机栈 Java Virtual Machine Stacks

各类JVM线程有一个个体JVM栈,与线程一起被创设。一个JVM栈存frames。一个JVM栈是看似于古板语言C的栈:它有地面变量和一些变量,在点子调用和重回中起着职能。因为JVM栈从不直接被操作除了放和取frames,frames依据堆放置。对于JVM栈内存不要求用到。

第一版java虚拟机规范中,JVM栈叫Java栈。

其一正式允许JVM栈是原则性大小或许是动态增添以及依照总计的拿走。

假使JVM栈大小是一定的,每一种JVM栈被单独的取舍当栈被制造时。

各种JVM达成可以提供程序照旧用户控制基于JVM栈的始发大小,同样的动态扩大的要么contractingJVM栈,控制在最大和纤维大小。

下边是JVM栈的很是条件

假使统计后线程需要一个更大的JVM栈比起所允许的,JVM抛出StackOverflowError.

假定JVM栈是动态伸张的,当伸张中没有丰富的内存如故尚未丰硕的内存去开始化JVM栈的一个线程,JVM抛出OutOfMemoryError

简单的讲的话

JVM中各种线程有一个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类似一个集结。
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.

运作时常量池是每一种类依旧各种接口在类公事的常量池表的周转时表示。他饱含了两种常量,从编译时的数字文字到点子和引用字段,必须在运转时搞定。运行时常量池服务方程式类似刘恒规编程语言的符号表,即便它涵盖了一个大范围的数码而不是出类拔萃的符号表。

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.栗子

地方介绍了一多级的内存分布每一块都有友好的作用和特色,我们接下去拿一个板栗来作为实例分析一下:

Object obj = new Object();

简易吗,不过深扒还能够扒出很多东西的。拆分成2块来展开剖析。
Object obj 在相应的stack frame中的local
variable表中以reference类型出现。
new Object()呢
在heap中开发了一块以存储object类型所有实例数据的内存。heap中还必须带有相应的目的型数据类型(interface,super
class,class….)
reference
里面就是一个针对对象的引用所以以往的标题就是何许把她们2个链接起来(=)。有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规范链接