Android memory, performance is the eternal topic of the program

Source: Internet
Author: User

Memory, performance is the eternal topic of the program, the actual development about the stalling, Oom is often not finished two tigers, about the lag, oom positioning methods and tools more, this article is not intended to repeat, this chapter is mainly to collate the JVM's memory model and Java object Life and death.

Living space (memory area)

The Java program runs on top of the JVM, and if the Java object is a flesh and blood creature, what is its living environment? Many people divide Java memory into heap memory (heap) and stack memory (stack), which in fact is somewhat coarse and partial. The more detailed division is this:

Divided into Regulation Regulation counters, virtual machine stacks, local method stacks, method areas, and heaps.

Program counter

Before we understand the program counters, let's take a look at thread parallelism:

The sensory two threads are executed concurrently, but on a CPU is actually switched on and off, at the same time, the CPU does not execute a thread concurrently, and the thread's "parallel" is implemented by high-speed switching of the CPU.

The question now is: how do I determine the position and state of the current thread before it is switched back?
The answer is: Use the program counter. Each thread requires a separate program counter to record the state of the thread execution, and the counters between the threads do not affect each other, so the program counter is a thread-private area of memory. So the more threads there are, the greater the overhead.

Java Virtual Machine Stack

The virtual machine stack is also a thread-private memory area used to store information such as local variables, operand stacks, method exits, and so on during the execution of the method. Thread execution Each method will create a stack frame, the stack frame contains the local variable table, operand stack, method exit and other information, local variable table holds the basic type of temporary variables, including Boolean, Byte, char, short, int, float, long, Doubble and Object application type (reference, object address). For example, the following section of code:

If a thread executes method a (), then the stack memory for that thread is roughly the same:

If the method executes to 15 rows, the stack frame of Method B () is created to incorporate the stack:

After executing 15 lines to 16 lines, the stack frame of method B is out of stack:

The Java Virtual machine stack, which is what we often call stack memory, throws a Stackoverflowerror exception if the thread requests more depth than the virtual machine is running.

Local method Stack

The local method stack and the virtual machine stack are similar, the virtual machine stack is the Java code service for the virtual machine, the local method stack is to use the native method service for the virtual machine. The local method stack in the HotSpot virtual machine also throws an Stackoverflowerror exception.

-java heap of objects resting in the land

Heap memory, most people are familiar with the Java heap is the largest in the virtual machine station memory area, is the soil of all object instances. Heap memory is thread-shared, and all thread-generated objects are divided into memory in this area.

Java heap is the main management area of garbage collector, also called "GC Heap", from the point of view of garbage collector Java heap is divided into Cenozoic and Laosheng generation (and the garbage collector's generational algorithm related, see: "Java object of Death"); from a thread-sharing perspective, Java The heap may also be divided into a memory area under which each thread divides one under the cache area used by the thread (thread Local Allocation Buffer, referred to as Tlab, is further explained later). No matter how it is divided, a outofmemoryerror (OOM) exception is thrown when there is not enough space in the heap to hold the new instance.

Method area

Like the Java heap, it is shared by threads, which are used to store class information, constants, static variables, and immediately compiled code data.

Type information, which refers to the correspondence between a type and its pointer, is obtained when the object is created and accessed, and is used to find the distinguishing type. For example, there are two classes, class A and class B, if these two classes are loaded, then the method area is probably recorded like this:

A part of a constant is called a run-time-constant pool, which is not generated during compilation and loading, but is generated during runtime, for example:

String a = "abc";String b = "def";String c = a+b;

The "ABC" and "Def" generated by the above code will be stored in the run-time-constant pool. The memory usage of the method area exceeds the limit that throws Stackoverflowerror exceptions.

The "Birth" of the object

The living environment of an object is described earlier: the region of memory and the role of each region. Next talk about the "birth" of the object, a new keyword in the end contains those "not known" process?

When the program execution encounters a new keyword, it first goes to the method area to navigate to the class-compliant reference, query to what class to check if the class has not been loaded, if the class loading process is not performed, the class loading process is also a relatively complex process, where the discussion does not unfold.

After the class load is complete (or has already been loaded), the next step is to allocate memory for the new object, and there are generally two ways to allocate memory for an object: "Pointer collision" and "Idle list". If the memory that is being used in the Java heap and the free memory are continuously regular, the intermediate critical point holds a pointer as the demarcation mark, the pointer moves when allocating memory for the new object and the distance of the object is equal, so it is called "pointer collision". If the memory that is being used in the Java heap and the free memory is not contiguous, then there is no way to allocate memory with the pointer collision, the virtual machine must maintain a list to record those memory is idle, when allocating memory to flush the idle list to find a large enough space class allocated to the object, this method is called "Idle list". Whether to "crash" or "free list" depends on the regularity of the Java heap memory, and whether the Java heap memory is structured depends on whether the garbage collector uses the Recycle algorithm (refer to "Java object's death") for compression.

As mentioned earlier, Java heap memory is thread-shared, multi-threaded while allocating memory in heap memory, it is necessary to ensure that the memory partition of the atomic line. How do I ensure thread safety for memory allocation? Generally there are two scenarios, the first solution is practical synchronization control processing, the second practical Thread Local Allocation Buffer (tlab) mode. The first one is very well understood and not too much to explain. Tlab, a thread-local buffer, is a pre-assigned tlab for each thread, and is allocated on its own tlab when the thread needs to use memory.

Memory allocation play requires necessary settings for the object, such as object type information, metadata
Object hash code, GC age, and so on.

What does the object look like?

Through the above introduction we know the object of the "birth" process, the object "long what kind of"? An object can be divided into 3 parts in memory: The object header (header), the instance data (Instance), and the aligned padding (Padding).
The object header includes hash code, GC generation age, lock state, thread hold lock, bias thread ID, timestamp, and so on, plus a type pointer. The role of a type pointer is that the JVM uses this type of pointer to find the object that belongs to that class. The Instance data section is the part that actually stores the object information. The alignment supplement is not necessarily present, just for positioning, because the HotSpot VM requires that the size of the object must be an integer multiple of 8 bytes, the length of the object header is already an integer multiple of 8 bytes, the instance data size is not fixed, so use the padding section to populate.

and Shake it.

The object has already been created and knows its approximate shape and how to access it. Accessed using a direct pointer in a HotSpot. As described earlier in the virtual machine stack, the local variable table in the stack frame created during method execution stores the local variables of the method, including the base type and reference type, where the reference type is actually a pointer to the object memory address.

Recommended:

Android memory, performance is the eternal topic of the program

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.