JAVA GC(垃圾回收机制)-1

JAVA GC(垃圾回收机制)-1

1.连锁概念

1.开篇

Arvin小编又来形容技术博文啦,做个不大笔记,顺便为一部分丁扫扫盲,最近本着Java
的GC(垃圾回收机制)特别感谢兴趣。很已经懂得在C/C++
这看似语言中待程序猿自己失去操作对内存的管制 什么 alloc,
dealloc啦之类的。当初学OC的时刻吧发生如此一段节。但是说到java很少有人会说及管理内存这块文化。java相比C语言在内存管理这块先进了众多,因为java的内存是活动管理的,光机关这个词就知晓死高档有麻痹有。但是java是怎去创造同假释内存的也。这个坏有必要扒一卧,毕竟听说有些面试上来就问java的GC是怎work的。还有就是是马上无异于块属于jvm的基本知识。

2.备知识

即时首文章使是吗后的GC分析来做基础知识扫盲的,主要是讲jvm的内存分配。以前提到过jvm的内存就分割2片
一个储藏室(stack)一个堆放(Heap),其实这个是蹭的,没有这么简单,还是时有发生接触小复杂的,好了来煮一扒jvm的内存

2.JVM内存

1.结构

图片 1

就张图片表明了,当运行java程序的早晚
jvm会产生的内存结构,而我们平常所说的stack 和heap 就是相应之jvm
stack和heap(heap中的新生代与老年代就篇稿子被不介绍,后面同样首GC分析的时段
会去详细介绍,目前即使管他当做jvm就吓哪)

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自然就木有了。

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的时节利用的

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

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的百般

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对应之参数。

admin

网站地图xml地图