Java Virtual machine-java memory Area

Source: Internet
Author: User

Run-time data regionJava Virtual Machine in the process of executing a Java program, it manages the memory divided into a number of different sizes of data fields;
These zones have their own uses, as well as creation and destruction times, and some regions exist as virtual machine processes start, and some are built and destroyed depending on the start and end of the user thread.

Program counter:The program counter is a small amount of memory space that can be viewed as the byte code executed by the current thread .signal indicator;because the multithreading of a Java Virtual machine is implemented in a way that threads rotate and allocate processing execution time, at any given moment, a processor executes only the instructions in one thread. Therefore, in order for the thread to switch back to the correct execution location,A separate program counter is required for each thread. The program counters between each thread do not affect each other, independent storage, such memory area is "thread-private" memory;Java Virtual machine stack:The Java Virtual machine stack is alsoThread Private, whose life cycle is the same as the thread. virtual Machine Stack executes Java method services for virtual machinesThe virtual machine stack describes the memory model that the Java method executes: Each method creates a stack frame tostore local variable table, operand stack, dynamic link, method out of stack portand other information. each method from the call until the completion of the process, corresponding to a stack frame in the virtual machine stack from the stack to the process of the stack.
The local variable table holds the various basic data types (Boolean, Byte, char, short, int, float, long, double), object references (reference types), and returnaddress types that are known at compile time; which -bit length ofLongand theDoubletype will occupy2local variable space, the rest of the data types only occupy1A. The memory space required by the local variable table is allocated during compilation.
In the Java Virtual Machine specification, 2 exceptions are specified for this area:1. If the threadthe requested stack depth is greater than the allowed depth of the virtual machine, it will throwStackoverflowerror(Stack Overflow) anomalies;2, if the virtual machine stack can be dynamically extended, ifUnable to request enough memory for expansion, it will throwOutOfMemoryError(memory overflow) exception;
Local method Stack:The local method stack is the native method service used by the virtual machine;
The local method stack, like a virtual machine stack, also has 2 exceptions:
1, if the thread request the stack depth is greater than the virtual machine allowed depth, will throw Stackoverflowerror exception;
2, if the virtual machine can be dynamically extended, if the extension is unable to request enough memory, will throw OutOfMemoryError exception;java heap:
The largest piece of memory managed by a virtual machine is alsoAllthreads are shared, which is created at virtual machine startup, the only purpose of this memory area is to hold object instances, almost allobject instances and arrays are allocated memory here. JavaHeap is the main area of garbage collector management, the objects are automatically managed, which is commonly known asGC(Garbage Collector) is managed. Javathe capacity of a heap can be a fixed size, or it can be dynamically scaled with demand (-xmsand the-xmx) and automatically shrink when too much space is not required. Javathe memory used by the heap does not need to be guaranteed to be physically contiguous, as long as it is logically contiguous. JAVAclassification of Heaps: From the perspective of memory recycling, JavaThe heap can be subdivided into:New Generation , old age, the finer point can also be divided intoEdenSpace,From SurvivorSpace,To SurvivorSpaceand so on. from the memory allocation point of view, the thread-shared Java heap may be divided into multiple thread-privateallocation buffer (Tlab);JVMimplementations should be provided to the programmer to adjustJavathe initial capacity of the heap, for the dynamic expansion and contraction of the heap, you should provide the means to adjust its maximum and minimum capacity. If there is no memory in the heap to complete the instance allocation and the heap cannot be expanded, it will be thrownOutOfMemoryError.
Method Area:in the Java virtual machine specification, the method area is treated as a logical part of the heap, but in fact, the method area is not a heap, in addition, many people will be the Java GC Collection mechanism divided into 3 generations: The green age, the old age, the permanent generation, these authors defined the method area as "permanent generation", This is because in the implementation of the previous HotSpot virtual machine, the idea of generational collection is extended to the method area, and the method area is designed as a permanent generation. However, most virtual machines other than hotspot do not treat the method area as a permanent generation, and the hotspot itself plans to cancel the permanent generation.
the same as the heap is by eachmemory areas shared by Threads, which is used to store the virtual machineloaded class information, constants, static variables, immediate compiler-compiled codeand other data. Although this area is described by the virtual machine specification as a logical part of the heap, its name is non-heap and is used to make a difference with the heap.
The method area is created when the virtual machine is started.
the capacity of a method area can be a fixed size, or it can be scaled dynamically as the requirements of the program execute, and shrink automatically when too much space is not required.
The method area can be discontinuous in the actual memory space.
The Java Virtual Machine Implementation should provide a means for the programmer or end user to adjust the initial capacity of the method area, and for the dynamic expansion and contraction of the method area, it should provide the means to adjust its maximum and minimum capacity.
garbage Collection performed on the general method area is rare, and this is one of the reasons that the method area is known as a permanent generation (HotSpot), but this does not mean that there is no garbage collection on the method area at all, and that the memory-recycling target on it is primarily for the collection of constant pools and unloading of types.
The OutOfMemoryError is thrown when the method area does not meet the memory allocation requirements. To run a constant-rate pool:
It is part of the method area.
In addition to the class file, there isthe version, field, method, interface of the classand other information such as description, there is also a messageChang (Constant Pool Table), used to hold various literal and symbolic references generated during compilation, direct references to translations(the symbolic reference is that the encoding is to use a string to represent a variable, the location of the interface, the direct reference is the translated address according to the symbol reference, will be completed in the class Link stage translation); This section will be stored in the run-time pool of the method area after the class is loaded.
Java Virtual machines have strict specifications for each part of a class file (which naturally includes Chang), and each byte is used to store which data must conform to the requirements of the specification, so that it is recognized, loaded, and executed by the virtual machine. However, for running a constant pool, the Java Virtual Machine specification does not require any details, and the virtual machines implemented by different providers can implement this memory area as they want. However, in general, in addition to saving the symbolic references described in the class file, the translated direct references are also stored in the run-time-constant pool.
Another important feature of running a constant pool with respect to a regular pool of class files iswith Dynamic nature, the Java language does not require constants to be generated at compile time only, that is, the contents of the constant pool in the class file are not pre-placed to enter the method area to run the frequent pool, and the new constants may be put into the pool during the run. This feature is used by developers more than the Intern () method of the String class.
since the run-time constant pool is part of the method area, it is naturally limited by the memory of the method area .A OutOfMemoryError exception is thrown when a constant pool cannot be applied to memory again;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. But this part of the memory is also used frequently, but also can cause outofmemoryerror abnormal;
in theJDK1.4added the newNIOclass, which introduces a method based on theI/O to the channel and bufferway, it can be usedNative Function libraryDirect DistributionOut-of- heap memory, which is then manipulated by a Directbytebuffer object stored in the Java heap as a reference to this memory.
this can significantly improve performance in some scenarios, becauseavoids copying data back in the Java heap and the native heap. hotspot Virtual Machine Object Quest
virtual opportunity to a new command
1. The first step is to check whether the parameter of this directive is able to locate the symbolic reference of a class in a constant pool, and to check whether the class represented by the symbol reference has been loaded, parsed, and initialized. If not, you must first perform the appropriate class loading process
2. After the class load check passes, the next virtual machine willallocating memory for new objects. The amount of memory required by an object can be fully determined after the class is loaded, and the task of allocating space to an object is equivalent to dividing a size of memory from the Java heap.
If the Java heapmemory is absolutely regular, you can use thePointer Collision"Allocation method (used memory aside, free memory on one side, and a pointer in the middle as an indicator of the demarcation point, allocating memory is simply moving the middle pointer to the free memory side of the object to the same size as the distance). ;
If the Java heapmemory is not regular, you can use theFree list"The allocation method (used memory and free memory interleaved, the virtual machine must maintain a list, record which memory blocks are available, at the time of allocation from the list to find a large enough space to partition the object example, and update the records on the list);
choosing that allocation method is determined by whether the memory is structured in the Java heap. The regularity of memory in the Java heap is determined by whether the garbage collector in use has a compression collation function.
In addition to dividing the available space, consider whether object creation is frequent in the virtual machine,is not thread-safe in a concurrency scenario.
solve this problem hasTwo Solutions: One is to allocate the memory space of the actionSynchronous Processing-in fact, the virtual machine using CAS with the failure to retry the way to ensure the atomicity of the update operation, the other is the memory allocation of the action according tothread partitioning in different spaces, that is, each thread pre-allocates a small chunk of memory in the Java heap, called the local thread allocation buffer (TLAB).
3. After the memory allocation is complete, the virtual machine needs to initialize the allocated memory space to 0 (excluding object headers).
If you use Tlab, this work process can also be done in advance to Tlab allocation.
This operation ensures that instance fields of an object can be used directly in Java code without assigning an initial value, and the program can access the 0 value corresponding to the data type of these fields.
4. The virtual agent makes the necessary settings for the object, such as which class the object is an instance of, if the metadata information of the class can be found, the object's hash code, the object's GC generational age, and so on, the information is stored in the object's object header. memory layout of the object
in a hotspot virtual machine, the layout of objects stored in memory can be divided into: Object headers, instance data, and aligned fills;
Object HeaderIt is divided into 2 parts: some of which are used to store the running data of the object itself, such as hash code, GC generational age, lock status flag, and so on, the other part is the type pointer, which is the pointer to its class metadata, which the virtual machine uses to determine which class instance the object is.
instance Datapart is the valid information that the object is actually stored, as well as the various types of field content defined in the program code. The storage order of this section is affected by the virtual machine allocation policy parameters and the order in which the fields are defined in the Java source code .
Object FillsDoes not necessarily exist, nor does it have a special meaning. Because the automatic memory management system of the Hotspot virtual machine requires that the object start address must be an integer multiple of 8 bytes, the size of the object must be an integer multiple of 8 bytes.
when the object instance data part is not aligned, it needs to be filled by object padding. Access positioning of objects
The way an object is accessed depends on the virtual machine implementation. The current mainstream access methods are Handleand theDirect PointerTwo kinds
Handle :with a handle, a chunk of memory is separated into the Java heap as a handle pool.
The handle address of the object is stored in reference, and the handle contains the specific address information of the object instance data and the object type data respectively;
Advantages: A stable handle address is stored in the reference, and only the instance data pointers in the handle are changed when the object is moved, and the reference itself does not need to be modified;
Direct pointer:If you use a direct pointer approach, you must consider how to place information that accesses type data in the layout of the Java heap object. The address of the object is directly stored in the reference;Advantages: Avoids the time overhead of one pointer positioning and is fast.

Java Virtual machine-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.