JAVA GC(垃圾回收机制)-1Java内存区域和内存溢出好。

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

  • 骨干类型(int,long,char,boolean…….)

  • 目标类型的援
  • 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规范链接

相关文章