Java Virtual machine 2:java memory Area

Source: Internet
Author: User
Tags garbage collection instance method

1. Concepts of several computers

For the future to write the article to consider, also to consolidate their knowledge and some basic concepts, here to clarify a few computer concepts.

1. Computer storage Unit

bits, bytes, kilobytes, megabits, gigabytes,terabytes, and 1024 times times between neighboring units, from small to large, 1024 is 2 of the 10 parties , namely:

    • 1Byte = 8bit
    • 1 k = 1024Byte
    • 1M = 1024K
    • 1G = 1024M
    • 1T = 1024G

2, Computer storage components

Register: Part of the CPU of the central processor, is the fastest read and write storage components in the computer, but the capacity is very small

Memory: A separate component that communicates with the CPU and is used to store the operational data in the CPU and the data exchanged with the external memory. Although today, the memory read and write speed is very fast, but because the register is on the CPU, so for the memory read and write speed and the register read and write speed There are several orders of magnitude gap. But there is no way, read/write I/O operations for memory is very difficult to eliminate, the number of registers is limited, it is not possible to complete all the operational tasks by register

3. Kernel Space and user space

Connecting memory and registers is address bus, the width of the address bus affects the index range of the physical address, because the bus width determines how many bits the processor can get from the register or memory at one time, and also determines the maximum addressable address space of the processor. such as the 32-bit CPU system, the addressable range is 0x00000000~0xffffffff, that is, 232 = 4,294,967,296 memory locations, each memory location 1 bytes, that is, 32-bit CPU system can have 4GB of memory space. However, applications are not fully able to use these address spaces, because these address spaces are partitioned into kernel space and user space, and the program can use only the memory of the user space. Kernel space mainly refers to the program logic used by the operating system for program scheduling, virtual memory use, or link hardware resources. The purpose of distinguishing between kernel space and user space is mainly to be considered from the point of view of system stability. Windows 32 operating system default kernel space and user space is 1:1, that is, 2G kernel space, 2G memory space, 32-bit Linux system in the default scale is 1:3, that is, 1G kernel space, 3G memory space.

4. Word length

CPU is one of the main technical indicators, refers to the CPU can be processed in parallel binary bits (bit). Typically, a CPU with a 8-bit cpu,32 bit of 8 bits of data is processed to handle binary data with a length of 32 bits at the same time. However, although the CPU is mostly 64-bit, it still runs at 32-bit length.

2. Preface

When it comes to Java memory areas, many people may first react as "stacks." First, the stack is not a concept, but two concepts, heap and stack are two different memory areas, simple to understand, the heap is used to store objects and stack is used to execute the program . Second, the heap memory and stack memory of this division is rough, this partitioning method can only show that most programmers are most concerned about the memory allocation with the most closely related to the memory area is these two pieces, the partition of Java memory area is actually far more complex than this. For Java programmers, with the help of the virtual machine automatic memory management mechanism, it is no longer necessary to pair the Delete/free code for each new operation, which is not prone to memory leaks and memory overflow problems. However, because Java gives memory control to a virtual machine, it is difficult to troubleshoot a memory leak and memory overflow, so a good Java programmer should be able to understand the memory areas of the virtual machine and the scenarios that can cause memory leaks and memory overflows.

3. Run-time data region

The Java Virtual machine (JVM) internally defines the memory areas that the program needs to use at run time:

The reason for dividing so many areas is because these areas have their own uses, as well as the time to create and destroy them. Some regions exist as virtual machine processes start, and some regions depend on the user thread's start and end to destroy and build.

Thread Shared Memory Area: Method area and Heap,

Thread Private Memory Area: Virtual machine stack, local method stack, program technology , basically with the thread generation and extinction, that is, the life cycle and the thread is the same, so there is no need to consider the memory recycling problem, compile time to determine the required memory size.

Look at these data areas from this classification perspective.

3.1. Thread-Exclusive Memory area

(1) Program COUNTER REGISTER, Procedure counter

This memory area is very small, it is the current thread executes the bytecode of the line number indicator , the bytecode interpreter by changing the value of this counter to select the next need to execute the bytecode instruction, branch, jump, loop and other basic functions to rely on it to achieve.

Here are three things to note:

    • Each thread has a separate program counter, and the counters between the threads do not affect each other, so the zone is thread -private .
    • When a thread executes a Java method, the counter records the address of the executing virtual machine bytecode instruction, that is, the value of the counter is empty when the thread executes the native method (called the local operating system method).
    • In addition, the memory area is the only area in the Java Virtual Machine specification that does not specify any oom (memory overflow: outofmemoryerror) condition , meaning that the block area does not throw a memory overflow exception.

(2) JAVA stack, virtual machine stack

    • The zone is also thread-private, and its life cycle is the same as the thread.
    • The virtual machine stack, which is what we call the stack memory, is a Java method that describes the memory model that the Java method executes .
    • Each method from the call until the completion of the process, it corresponds to a stack frame in the virtual machine into the stack to the process of the stack.
    • The memory model executed by the Java method: Each method is executed with a stack frame that stores the local variable table, the operand stack, the dynamic link, the method return address, and some additional additional information. stack It is used to support the continuation of the virtual machine for method invocation and method execution of the data structure. For the execution engine, in the active thread, only the stack frame at the top of the stack is valid, called the current stack frame, and the method associated with the stack frame is called the current method, and all bytecode instructions run by the execution engine operate on the current stack frame only. When compiling program code, the number of local variable tables required in the stack frame, the number of deep operand stacks are fully determined, and the code attribute of the method table is written. Therefore, how much memory a stack frame needs to allocate is not affected by the program run-time variable data, but only by the specific virtual machine implementation.
    • The size of the stack is usually between 256k~756k, specifically related to the implementation of the JVM.
    • In the Java Virtual Machine specification, Two exceptions are specified for this area:

1. A Stackoverflowerror exception is thrown if the thread requests a stack depth that is greater than the depth allowed by the virtual machine.

2. If the virtual machine cannot request enough memory space in the dynamic expansion stack, the OutOfMemoryError exception is thrown.

There are some overlapping places: when the stack space cannot continue to be allocated, whether the memory is too small or the stack space used is too large, it is essentially just two descriptions of the same thing. In a single-threaded operation, either because the stack frame is too large or the virtual machine stack space is too small, when the stack space cannot be allocated, the virtual machine throws a Stackoverflowerror exception, and does not get the OutOfMemoryError exception. In a multithreaded environment, OutOfMemoryError exceptions are thrown.

The functions and data structures of each part of the information stored in the stack frame are described in detail below.

1. Local variable table

A local variable table is a set of variable value storage spaces used to store method parameters and local variables defined inside the method , where the type of data stored is the various basic data types , object references (reference) that are known at compile time ( is not equivalent to the object itself, it may be a reference pointer to the start address of an object, or it may point to a handle that represents an object or other location associated with the object) and the Returnaddress type (which points to the address of a bytecode directive).

The memory space required for a local variable table is allocated during compilation, that is, when the Java program is compiled into a class file, the capacity of the maximum local variable table to be allocated is determined. When entering a method, this method needs to allocate how much local variable space in the stack is fully determined, and the local variable table size is not changed during the operation of the method.

The capacity of the local variable table is the smallest unit in the variable slot (slot) . The virtual machine specification does not explicitly indicate the amount of memory space a slot should occupy (allowing it to change depending on the processor, operating system, or virtual machine), and a slot can hold a data type that is less than 32 bits: Boolean, Byte, char, short, int, float, reference and returnaddresss. Reference is the reference type of the object, and ReturnAddress is the service of the byte instruction, which executes the address of a bytecode instruction. For a 64-bit data type (long and double), the virtual opportunity assigns two contiguous slot spaces to it in the previous way.

The virtual machine uses the local variable table through index positioning , the range of index values is from 0 to the maximum number of slots in the local variable table, for 32-bit data type variables, index n for the nth slot, and for 64 bits, index n for Nth and n+1 two slots.

When the method executes, the virtual machine uses the local variable table to complete the pass of the parameter value to the parameter list, and if it is an instance method (not static), the slot of the No. 0-bit index in the local variable table defaults to the reference that is used to pass the object instance to which the method belongs, in which the keyword " This "to access this implied parameter . The remaining parameters are arranged in the order of the parameter tables, occupying a local variable slot starting at 1, and assigning the rest of the slots to the variable order and scope defined in the method body, after the parameter table has been allocated.

Slots in a local variable table are reusable, variables defined in the method body, scopes do not necessarily overwrite the entire method body, and if the value of the current bytecode PC counter exceeds the scope of a variable, the slot corresponding to that variable can be used by other variables. This design is not only to save space, in some cases, the reuse of the slot will directly affect the system and garbage collection behavior.

2, the operation of the stack

    • The operand stack is often called the Operation Stack, which is used to store the result of the operation and the operand of the operation.
    • The maximum depth of the operand stack is also determined at compile time . The 32-bit data type occupies a stack capacity of 1,64 of 2 for the data type.
    • It is different from the local variable table through the index to access, but through the stack and the way out of the stack: When a method starts execution, its operation stack is empty, during the execution of the method, there will be a variety of bytecode instructions (such as: add operation, assignment, etc.) to the Operation Stack to write and extract content.
    • The Java Virtual machine's interpretation execution engine is called the "stack-based execution engine", where the "stack" is the operand stack. So we also call Java virtual machines stack-based, unlike Android virtual machines, which are register-based. The main advantage of the stack-based instruction set is the portability, the main disadvantage is that the execution speed is relatively slow, and because the register is provided directly by the hardware, the main advantage of the register instruction set is the fast execution speed, the main disadvantage is the poor portability.

3. Dynamic Connection

each stack frame contains a reference to the method in which the stack frame belongs to the running constant pool (described in the method area, later), and holds this reference to support dynamic connections in the method invocation process . A large number of symbolic references exist in the constant pool of the class file, and the method invocation directives in the bytecode are referenced as parameters to the symbol in the constant pool that points to the method. These symbolic references, some of which are converted to direct references (such as final, static, and so on) during the class loading phase or when they are first used, are called statically resolved, and the other part is converted to direct references during each run, which is called dynamic connections .

4. Method return address

When a method is executed, there are two ways to exit the method: The execution engine encounters a bytecode instruction returned by either method or encounters an exception, and the exception is not processed in the method body . Regardless of the exit mode, after the method exits, it is necessary to return to the location where the method was called before the program can continue execution. When the method returns, you may need to save some information in the stack frame to help restore the execution state of its upper-level method. In general, the method normally exits, the caller's PC counter value can be used as the return address, the stack frame is likely to save the counter value, and the method exits unexpectedly, the return address is to be determined by the exception handler, the stack frame is generally not save this part of the information.

The process of exiting the method is actually the same as putting the current stack frame out of the way, so the actions you might take when exiting are: Restoring the local variable table of the upper method and the operand stack, and, if there is a return value, pressing it into the operand stack of the caller's stack frame, adjusting the value of the PC counter to a directive following the method

(3) NATIVE method Stack, local methods stack

The same as the virtual machine stack, except that the method stack serves the native method used by the virtual machine. The virtual machine specification does not impose any enforcement on this area, so we are using a hotspot virtual machine that simply does not have this area, and it is together with the virtual machine stack.

3.2. Memory areas shared between threads

(1) heap, heap

    • The Java heap is the largest piece of memory managed by a virtual machine, which is a chunk of memory shared by all threads.
    • The only purpose of this memory area is to hold object instances where almost all object instances are allocated memory.
    • Java Heap is the main area of garbage collector management, so it is often referred to as the "gc heap ". Because the garbage collector now uses the basic is the Generation collection algorithm, so the heap can also be subdivided into the new generation and the old age, and then the Eden area, from Survivior area, to Survivor area. (this is said later)
    • According to the Java Virtual Machine specification, the heap can be in a physically discontinuous memory space, as long as it is logically contiguous. A OutOfMemoryError exception is thrown when there is no memory to allocate in the heap and the heap cannot be expanded.

(2) Method area

This area is used to store data such as class information, constants, static variables, and immediately compiled code of the virtual machine , which is described as a logical part of the heap, but actually it should be separate from the heap. From the perspective of the collection algorithm mentioned above, in the hotspot, the method area ≈ permanent generation. However, after JDK 7, the hotspot we use should not have a permanent generation of this concept, will use native memory to achieve the planning of the method area.

    • The method area is also 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 .
    • The method area is also known as the " Permanent Generation ", but JDK7, JRockit, and IBM J9 have no concept of a permanent generation.
    • The Java Virtual machine specification describes the method area as a logical part of the Java heap, and it does not require contiguous memory as in the Java heap, and the allocation of the method area is implemented using Native memory .
    • Garbage collection behavior is less present in this area, and the recovery target for the area's memory is to reclaim the discarded constant pool and useless classes .
    • Running a constant pool is part of the method area that holds the various literal and symbolic references generated during the compilation period, which is dynamic.
    • According to the Java Virtual Machine specification, a OutOfMemoryError exception (OOM) is thrown when the method area does not meet the memory allocation requirements.

(3) Runtime CONSTANT pool, running constant-volume

In addition to descriptive information about the class's version information, fields, methods, interfaces, and so on, there is also a constant pool that holds the various literal and symbolic references generated during compilation, which will be loaded into the run-time pool of the approach area when the class loads. In addition, direct references to translations are also stored in this area . Another feature of this area is the dynamic nature , Java does not require constants must be generated during the compilation, the constants generated during the run will also exist in this constant pool, the String.intern () method is the application of this feature.

About literals, symbolic references, and direct references:

The literal is equivalent to the concept of a Java language-level constant, such as a literal string, a constant value declared final, and so on.

Symbolic references are concepts of compilation principles, including the following three types of constants: 1, class and interface fully qualified name 2, field name and Descriptor 3, method name and descriptor.

A direct reference can be a pointer to a reference target directly, a relative offset, or a handle that can be indirectly anchored to the target. Direct references are related to the memory layout of a virtual machine, and a direct reference to the translation of the same symbol reference on a different virtual machine is generally different. If a direct reference is present, then the target of the reference must be in memory.

4. Direct Memory

Direct Memory is not part of the data area when the virtual machine is running, nor is it an area of memory defined in the Java VM Specification . It is allocated directly from the operating system, so it is not limited by the size of the Java heap, but it is still subject to the size of the native total memory (including RAM, swap area) and processor addressing space, so it can also cause outofmemoryerror anomalies to occur.

New NiO has been added to the JDK1.4, introducing a channel-to-buffer I/O approach that can be used to allocate out-of-heap memory directly using the native library, and then operate through a Directbytebuffer object stored in the Java heap as a reference to that memory. This can significantly improve performance in some scenarios because it avoids copying data back and forth in the Java heap and native heap.

5. Summary

A simple summary:

    • Program Counter (PC): Java thread Private, similar to the operating system of the PC counter, to specify the next code to execute the address of the bytecode;
    • Java Virtual machine stack: Java thread Private, Virtual machine exhibition describes the memory model that the Java method executes: Each method executes, creates a stack frame for storing local variables, operands, dynamic links, method exits, and so on Each method call means a stack frame in the virtual machine stack into the stack of the process;
    • Local method Stack: similar to the Java Virtual machine stack, the difference is the local method service that this region calls to the JVM;
    • Heap: All threads share a region of the primary area that the garbage collector manages. At present, the main garbage collection algorithms are collected on a generational, so the region can also be subdivided into the following areas: – Young generation –eden space –from survivor space 1,from survivor Space 2, for storing the objects that survived the youth GC; – the old age
    • Method Area: A region shared by each thread to store information such as class information, constants, static variables, etc. loaded by the virtual machine;
    • Run a constant pool: part of the method area that holds the various literal and symbolic references generated by the compiler;

Reprint Address: http://www.cnblogs.com/xrq730/p/4827590.html

http://blog.csdn.net/qq_31957747/article/details/73662504

http://blog.csdn.net/ns_code/article/details/17565503

Java Virtual machine 2:java memory Area

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.