Uncover the veil of Java memory management

Source: Internet
Author: User

Objective

In contrast to the high-performance languages of C and C + +, Java has the features that such programmers envy: automatic memory management. As it seems, Java programmers do not have to care about memory, nor do they need to know the relevant knowledge. But is that really the way it turns out? Especially for our Android programmer, the memory is very hard to eat, once there is a more complex memory leaks and overflow problems, it is simply a nightmare. Therefore, a general understanding of Java memory management seems to have become a must-have skill for a qualified Android programmer, even if the new Kotlin is also based on the JVM. Let's take this opportunity to come together to uncover its veil.

Object

Java is an object-oriented programming language, the lake has been circulating a sentence: all things are objects. Therefore, Java's memory management can also be understood as the creation and release of objects. So, what exactly is the object? Boyfriend? Girlfriend? Still is? What exactly is the relationship between an object and memory? There are too many problems here, let's take a step-by-step.

Tips1:全文以常用的虚拟机HotSpot、常用的内存区域Java堆和普通Java对象为例。Tips2:如果深读过《深入理解Java虚拟机》的同学可以不用看了,请右上角,如果忘了,请继续!
Concept

Boyfriend or girlfriend you can understand the object, the object is real, such as dad, Mother, accompanied by an abstract concept, class: It is the abstract of the object, whether it is a boyfriend and girlfriend are human, belong to the human. The concept is almost introduced to this, feel like in college class ... My God (cover your face).

Object and Memory creation

What about programmers without a daughter-in-law? New one. Old simple, high, short, thin, fat, want what there is, the most regret in this life is when the programmer, although the head a bit cold.

The new one is the creation of an object, so what is the process? When the JVM encounters a new command, it first checks to see if the parameter of the directive is able to locate the symbolic reference of a class in the constant pool, and checks whether the class represented by the symbol reference has been loaded, parsed, or initialized. If not, it must first execute the corresponding class loading process, after the class loading check, you can say that an object model has come out, but Java is only a programming language, or to allocate memory is not? How else to operate?

Distribution

The allocation of object memory and the reality of many scenes are the same, such as parking, some places may only have 100 parking spaces, first to stop at the top of the vacancy, so in order to stop a car. Such allocations are called "pointer collisions." There is a kind of stop where you want to stop, as long as you plug in. Such allocations are called "idle lists." Whether it is the former or the latter, parking we see by the eye, where there is a vacancy to stop, then how the JVM "See" It? The former relies on a pointer as an indicator, and the object that allocates much memory moves back a large distance, which maintains a list of available memory (pluggable spaces).

for the concurrency-sensitive students will certainly ask questions, in the concurrency of how can be correctly assigned to the appropriate location? generally there are two solutions, one is a car stop, to ensure that the first one stopped, the next car will start to stop; the other is that we agreed to stop in which area, such as A,b,c stop in a area, then a,b,c every time to stop the A area is OK, and other areas do not matter (area refers to the thread), If they invite a friend D, then I'm sorry, just wait for the rest of the area to stop, you stop. Therefore, the creation of objects is not an atomic operation, remember, remember.

Layout

Where is the car parked, we already know, then how to stop? Some people like to stop, some like to stop sideways, some like to stop backwards. Similarly, how are objects placed in memory? It is broadly divided into 3 parts: the object header (header), the instance data (Instance), and the alignment padding (Padding).

Simply to introduce these 3 bits, after all, this concept is too strong.

The object header includes two pieces of information, the first part is used to store the object's own runtime data, such as hash code, GC generational age, lock status flag, thread-held lock, biased thread ID, biased timestamp, etc., and the other part is a type pointer, that is, the object pointer to its class metadata, The virtual machine uses this pointer to determine which class of this object is an example. Some of the above nouns do not understand, I may explain in the following article, after all, I am also in the study, if you want to be eager to know the students can consult the relevant information, let it be a concept to remember.

The instance data is well understood, it is the valid information that the object actually stores, also is the various types of field content defined in the program code.

Alignment padding does not necessarily exist, because the memory management system requires that the object start address must be an integer multiple of 8 bytes, in other words, the size of the object must be an integer multiple of 8 bytes. The object header size is an integer multiple of 8 bytes, so when the instance data size is not an integer multiple of 8 bytes, you need to align the padding to fill it.

Access

After you stop the car, do it, you gotta drive home, you gotta find your own car, right? How to find? Where do you think you're parked? You always remember your license? So how do we access our objects in memory? Let's look at a photo:

The former is called the handle access, the advantage is obvious, the object moves as long as the pointer in the modification handle is OK, will not involve the reference, the latter is called direct pointer access, the advantages are obvious, is fast, directly less the handle of this layer. And the hotspot discussed in this article uses the latter.

Recovery

What if the car blows up? Buy a new (manual bad Laugh) of course. So how do we judge an object of crap? Before we introduce two kinds of reference algorithms: The first is the reference counting algorithm, very good understanding, to the object a counter, the initial value of 0, there is a local reference to add 1, the failure of 1, the counter is the description of 0 is excrement; the second is the Accessibility analysis algorithm, which is also well understood, starting with GC roots, referencing objects, If an object has a path from the GC roots to itself, then it means that the object is still alive, otherwise it will be a piece of crap. If the object567 is excrement:

So what can be used as GC roots?

    • Objects referenced in the virtual machine stack (local variable table in the stack frame)
    • Object referenced by class static property in method area
    • Objects referenced by constants in the method area
    • The object referenced by JNI (that is, the general native method) in the local method stack

Our hotspot uses the latter, so why not use the former? Because it is difficult to solve the problem of circular referencing between objects. For example:

ReferenceCountingGC objA = new ReferenceCountingGC();ReferenceCountingGC objB = new ReferenceCountingGC();objA.instance = objB;objB.instance = objA;objA = null;objB = null;

So the question comes, can't reach the object really excrement? Of course not, at least 2 times the mark will announce the death of an object. The first token is the Discovery object unreachable, while filtering out the Finalize () method is not overwritten or the Finalize () method has been called by the virtual machine, then these can be considered excrement, can be recycled (then this time is not only marked once?) There are no big guys to answer); the rest of the objects will be placed in the F-quenue queue and the GC will mark the objects for the second time, when the Finalize () method is executed (as long as the association with other objects in the reference chain is re-established in the method). It is best to forget the existence of this method. Its operation is expensive, uncertain, and can not guarantee the order of each object's call. Effective Java also mentions avoiding this approach.

Simple analysis of objects it's almost over here, you think it's all over? Too naïve.

What exactly is a noun, such as a virtual machine stack, a method area, or a Java heap, like the one encountered above?

Run-time Data area

International practice, No Picture,say a j8!

Seeing this picture, you must know what I'm going to do ... I do not want to ah, write this feeling is a description of the text, my days, thieves embarrassed.

Program counter

A program counter is a small amount of memory space that can be viewed as a pointer to the line number of the byte code executed by the current thread. For example, the usual branches, loops, jumps, exception handling, thread recovery and other basic functions to rely on this counter to complete. We know from the diagram that it is thread-private, and that each thread will have a separate program counter that does not affect each other. And it is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions .

Java Virtual Machine stack

The virtual machine stack describes the memory model that the Java method executes: Each method creates a stack frame to store information such as local variable tables, operand stacks, dynamic links, method exits, and so on. Each method from the call until the completion of the process, corresponding to a stack frame in the virtual machine stack into the stack of the process. Careful friend, you will find that the local variable table appears in the object's Access chapter diagram, it is important that when entering a method, this method needs to allocate how much local variable space in the frame is fully determined, in other words, the local variable table requires the memory space is allocated during compilation.

In the Java Virtual Machine specification, there are two exceptions to this area: if the thread requests a stack depth greater than the virtual machine allows, the STACKOVERFLOWERROR exception will be thrown, and if the virtual machine stack can be dynamically extended (most of the current Java virtual machines can be dynamically extended, Only the Java Virtual Machine specification also allows a fixed-length virtual machine stack, which throws a OutOfMemoryError exception if it cannot be extended without sufficient memory to be requested.

Local method Stack

The role of the local method stack and the virtual machine stack is very similar, but the difference between them is that the virtual machine performs Java (also said bytecode) service for the virtual machine, and the local method stack is the native method service used by the virtual machine. So the exception that is thrown with the Java Virtual machine stack is the same.

Java heap

You can assume that almost all object instances are allocated on the heap. Isn't that all? This is an optimization technique, just imagine if an object cannot be accessed by any other method or thread in any way, why not allocate it directly on the stack?

According to the Java Virtual Machine specification, the Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous, which also means that if there is not enough memory on the logic to complete the allocation and the heap cannot be extended, then the OutOfMemoryError exception will be thrown.

Method area

The method area, like the Java heap, is an area of memory shared by each thread that stores data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine. But it can also choose not to implement garbage collection, except that it does not require contiguous memory and cannot select a fixed size or extensible memory like the Java heap. OutOfMemoryError exception is thrown.

Run-time data area introduced almost, here to add a concept called direct memory, in jdk1.4 added NiO useful to, interested can see. We must note that each region (in addition to the program counter) has thrown memory overflow condition, later someone asked, when will produce oom, do not say memory is not enough time, very sad feeling.

Garbage collection algorithm

The Java heap mentioned above can be said to be the largest piece of memory managed by a virtual machine, a frequent flyer frequented by GC, and therefore also called a "gc heap". GC as its name implies is garbage collection, which is a big advantage of Java, unused memory can be automatically recycled. Since it is garbage collection can have garbage collection device Ah, the sweep also use a broom.

The figure is our hotspot garbage collector, above is the new generation, the bottom is the old age, the specific garbage collector of the history of the role I do not introduce, no need, this article hope that the reader has a general understanding. So, there is a garbage collector, there must be a way, drink drink also with a straw, straw what principle do we not point 13 number? So here is a general introduction to the idea of several algorithms.

Tag-Purge algorithm

See the name. Mark the objects that need to be recycled, and then clear the marked object once. It can be said to be the most basic collection algorithm, even if the algorithm described in the following is based on it to improve. Since the improvement, then there must be unbearable shortcomings, it is not only inefficient, there is a serious problem, even if there will be a large number of discontinuous memory fragments, from the Java we have just mentioned the reason for Oom, it is very easy to allocate the second time to perform garbage collection, or directly oom. Execution process:

Replication Algorithms

This algorithm is very good understanding, will be available to two pieces of memory, each time only with one piece, when to be recycled, the available objects copied to another piece, and then the original piece of clean out, can be said in the efficiency of greatly improved, but there is a fatal weakness is half of memory.

Replication algorithm Execution Process:

Tagging-sorting algorithms

The copy algorithm is highly efficient in theory, but you think that if there are 100 objects, 98 of which are available, then you have to copy 98 objects, and in extreme cases 100 survive, you have to copy them all over again, which is unacceptable. The algorithm improves the memory fragmentation of the tag-clear algorithm by moving the available objects to one end and then directly clearing out the memory outside the end boundary. Execution process:

Generational collection Algorithms

From what we have just analyzed, the replication algorithm seems to be more suitable for the object of the raw, and the remaining two algorithms are more suitable for the "hundred-year-old" object. The area of the former objects is called the Cenozoic, and the latter is called the old age. Our generational algorithm is based on the new generation and the old age using different algorithms.

So, here's the question, what is the object of the old age? In other words, how can we enter the old age? First, the analysis of a special case: large objects directly into the old age; then a normal step: object A assigns priority to the new generation of Eden space when allocated, and when the Eden space is not enough to allocate memory, a minor GC is performed, and then object A is still alive and can be accommodated by survivor space. Then move to the survivor space and set its age counter to 1, and after that, object a minor GC and survive each time, the age increases by 1, and when the maximum age (Maxtenuringthreshold) is reached, it will be promoted to the old generation (applause). Of course this is not absolute, if the sum of all objects of the same age in the survivor space is greater than half the size of survivor space, objects older than or equal to that age can be directly promoted.

About the main content of this article is almost here, and finally left a very critical question, when the garbage collector in the end garbage collection, and how to do it? Here is a very good noun called "Stop the World".

Zatan

First of all, I would like to say in-depth understanding of Java Virtual Machine (2nd edition) is really a good book, I this kind of chicken is not a chance to know this great God, but also not to advertise, read the students should know. Secondly, all the content of this article comes from the book, even one word is not bad. This article can be said that I read the second part of the book: Automatic memory management mechanism notes. Many of this article is conceptual knowledge, such as why Earth is called Earth? This is a conventional thing, but for our Android programmer, it is best to have a general understanding of it, but not all of the students have read the book (bought, not necessarily see), so I shared the article, which is part of their own understanding, if there is a problem I correct in time, It's better for everyone to buy the original book and read it carefully.

It's great to learn a little bit every day. Since it is learning, the object must have been summed up by predecessors, you should do is to understand it, and turn to their own things (with their own ideas to translate it, the essence of the same), or it is called exploration. There is a word is better memory than rotten pen, the teacher must have said this sentence, then a sentence did not enter my ears.

If you are in the process of learning Java problems or want to get some learning resources, welcome to join my Java Learning QQ Group:495273252

Uncover the veil of Java memory management

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.