Memory structures and garbage collection algorithm

JAVA also do nearly two years, the company's leader said and done JAVA, three years is a threshold, after three years if you do not have to study the JVM, then you this can only be a programmer's tool for the tile . Coincided with the resignation of resolving a JVM these days?

 

JVM is an acronym for Java Virtual Machine (Java Virtual Machine), but also refers to the JVM virtual machine, is a fiction out of computers, were up such a thing simulate various computer functions in our actual computers.

Because the JVM exist, engage in JAVA no longer need to be concerned about when to release the memory, it will not be like that for C ++ programmers a little bit of memory and melancholy, for that is you, JVM virtual machine to help you put these things are done , then the JVM JAVA us say it!

 

Let's take a look at the JVM model of it, before the point of view documents in Baidu, say a couple of the above, the method area, heap, stack, counter. Did not, very hard to accept, then read the book in-depth understanding of the JVM, it can be considered a little experience.

 

In-depth understanding of a book that JVM, JVM run-time data area will be divided into several different areas, there are ways zone (Method Area), a virtual stack machine (VM Stack), native method stacks (Native Method Stack), heap (heap), the program counter (program counter Register), the following is the book of FIG:

 

Let one by one to explain: Let me talk about the program counter (Program Counter Register): The program counter is actually used to store the address of the place where the next instruction, when we execute an instruction when the first instruction to know the location of his store, and then the instruction register which is brought on is to get the instructions, and then stored in the address of the program counter will be incremented by one, and then like this cycle to execute, and the program counter this small memory area is "thread-private memory."

 

Why is it private? In-depth understanding of a book that the JVM is multithreaded virtual machine is switched by alternately switching threads of the way the execution time of allocation of processors to implement, that it is very hard to pronounce, in fact, that is a processor, the same a time, only one thread of execution instructions, but the time may not be balanced, may the first minute in a thread, b threads execute the second minute to go, but then, in order to ensure the switch back also needs to be consistent, then each there will be a thread independent existence of the program counter, independent to store, in order to ensure that no impact. So he is a "thread-private memory."

 

There are several features of the program counter:

• If the thread is executing a Java method, the counter records a virtual machine bytecode instruction address being executed.

• If you are performing a Native method, then the counter value is empty (Undefined).

• This memory area is the only one not provide for any situation in the region OutOfMemoryError Java Virtual Machine Specification.

 

Respectively explain these three words, this is the in-depth understanding of java virtual machine's words, the first sentence seems to have a very straightforward, not to say, it is said that the second sentence

 

Since this counter is recorded bytecode instruction address, but Native (local method); for example, (System.currentTimeMillis ()) he is achieved through C, the system will direct calls directly through the need to do not need to be compiled into execution of bytecode instructions, then the equivalent of the program counter, however, it has no record, then he would certainly value of the counter is empty.

 

The third sentence we can try to compile a short code, and then decompile out to see

That is actually like this

public class Test {public int test () {int a = 10; // 0 ...... int b = 20; // 3 ....... int c = 30; // 6 ... ... return (a + b) * c; // 11 .... 13 .... 14 ...}} perform arithmetic operations

上面的0,2,3,5,6,8....就是指令的偏移地址bipush就是入栈指令, 在执行到test方法的时候,线程就会创建对应的程序计数器在计数器中放0,2,3,5,6,8....这些指令地址,所以计数器里改变的不是内存的大小,它也就没有溢出了。

 

下面我们再来说一下:JAVA虚拟机栈(VM Stack)

线程私有,生命周期和线程一样,这个虚拟机栈描述的是JAVA方法执行的内存模型,用于存局部变量,操作数栈,方法出口等信息的,上面那个bipush就是入栈指令,在这里最需要注意的就是他存放的是什么数据.局部变量里面放的就是那些我们所知道的基本的数据类型,对象引用的话那就是一个地址。

 

在虚拟机规范里面还说,他的2个异常状况:

•一个是StackOverflowError异常,栈内存溢出,这肯定很容易理解,就是栈的内存不够,你的请求线程太大。(固定长度的栈)

•如果说在动态扩展的过程中,申请的长度还是不够,那么会抛出另外一个异常OutOfMemoryError异常。

 

 

3.本地方法栈(Native Method Stack) :

它和虚拟机栈很类似,区别就在于虚拟机栈执行的是JAVA方法,但是本地方法栈则是Native方法,其他的没啥不同就连抛出异常都一样的。

 

4.JAVA堆(heap) 在JVM一书中也有提到,Heap是在JAVA虚拟机中内存占用最大的一个地方,也是所有线程共享的一个内存区域,堆内存中主要就是用于存放对象实例的。

 

几乎是所有的对象实例都在这里分配内存,JAVA堆是垃圾收集器管理的主要区域,那么现在重点来了,面试中问到最多的垃圾回收机制接下来就要仔细说说了。

 

内存回收,现在都是进行的分代算法,堆中也是,新生代,老年代,而且两种垃圾回收机制是采用的不同的回收机制的,在新生代中,每次垃圾收集时都发现有大批对象死去,只有少量存活,那就选用复制算法,只需要付出少量存活对象的复制成本就可以完成收集。

 

而老年代中因为对象存活率高、没有额外空间对它进行分配担保,就必须使用"标记-清理"或"标记-压缩"算法来进行回收,说回收机制先看看heap的分区(这个from和to 并不是绝对的,看对象处在哪个位置,GC的次数不一样之后,那from和to会有相应转变)

分区一目了然,下面研究一下算法实现吧

Minor GC:GC新生代,

Full GC:老年代GC,

因为新生代中对象的存活率比较低,所以一般采用复制算法,老年代的存活率一般比较高,一般使用”标记-清理”或者”标记-整理”算法进行回收。

 

看了有几天才明白啥意思,我说说我自己的见解吧,还是画图吧,

Minor GC:

我们每次new对象的时候都会先在新生代的Enden区放着也就是最开始 是这样子的

 

然后在Enden用完的时候里面会出现待回收的

 

 

然后就来了把存活的对象复制放到Survior1(from)中,待回收的等待给他回收掉 就是这样的

 

 

然后把Enden区清空回收掉

 

 

这样的话 第一次GC就完成了,下面再往下走

当Enden充满的时候就会再次GC

先是这个样子的

 

 

然后会把 Enden和Survoir1中的内容复制到Survior中,

 

 

然后就会把Enden和Survior进行回收

 

 

然后从Enden中过去的就相当于次数少的,而从Survior1中过去的就相当于移动了2次

 

 

这样新生代的GC就执行了2次了,

当Enden再次被使用完成的时候,就会从Survior2复制到Survior1中,

接下来是连图

 

经过回收之后Surior1就变了,1对象是从Enden直接复制过来的,2对象是Enden-->Survior2-->Survior1 ,3对象则是从Enden-->Surivior1-->Survior2-->Survior1 复制过来的,这样一步一步的执行下去的时候,就是新生代的GC。

 

既然这样,那为什么还会存在老年代呢?其实如果GC在执行的时候有些对象一直没有被回收,那么他移动次数就会无限的累计,每次从Surior(from)到Surior(to)的过程中就相当于又增加了一次移动,当他达到一定的次数的时候(默认是15),就会移动到老年代里了,所以不存在不会被回收的对象,但是这个次数可以设置的,

-XX:MaxTenuringThreshold

就类似这样子

其实上边的这只是一种情况,还有就是如果对象太大,存不下,那就直接会进入老年代。

还有那种默认就是长期活着的也会进入老年代,

而且这种复制算法的垃圾回收机制是比较浪费内存的,每次都会有一块内存区是闲着不干活的,但是优点很明显,简单高效

以上就是GC中垃圾回收中的新生代复制算法解析,新生代的Minor GC也算是知道了不少东西了,以上就是一些个人的见解,图比较清晰,容易理解,有不对的地方希望能够各位同行指点一下。

 


 

Java 极客技术公众号,是由一群热爱 Java 开发的技术人组建成立,专注分享原创、高质量的 Java 文章。如果您觉得我们的文章还不错,请帮忙赞赏、在看、转发支持,鼓励我们分享出更好的文章。

关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料。 

 

Guess you like

Origin www.cnblogs.com/justdojava/p/11198694.html