Java Virtual machine Learning-java memory Area (i)

Source: Internet
Author: User

The Java Virtual machine memory is divided into the following areas:

1. Method Area: The method area 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. Although the Java Virtual Machine specification describes the method area as a logical part of the heap, it has an alias called Non-heap (Not a heap), which is intended to be separated from the Java heap.

For program developers who are accustomed to developing and deploying on a hotspot virtual machine, many prefer to refer to the method area as a "permanent generation", which is not equivalent in nature, simply because the design team of the hotspot virtual machine has opted to extend the GC cell phone to the method area, or to use a permanent generation to implement the method area. , so that the hotspot garbage collector can manage this part of the memory just like the Java heap, enabling provinces to write memory-management code specifically for the method area.

The Java Virtual machine specification limits the method area very loosely, with the exception of the Java heap, which does not require contiguous memory to choose a fixed size or extendable, and optionally does not implement garbage collection. According to the Java Virtual Machine specification, a OutOfMemoryError exception is thrown when the method area fails to meet the memory allocation.

2, virtual machine stack: Java Virtual machine stack is also thread-private, its life cycle is the same as the thread, the virtual machine stack describes the Java method execution of the memory model: Each method will be executed at the same time to create a stack frame for storing local variable table, operand stack, dynamic link, method exit and other information. Each method from the call to the completion of the process, corresponding to a stack frame in the virtual machine stack into the stack of the process.

The local variable table holds various basic data types and object references that are restrained during compilation. Where 64-bit long and double data types consume 2 local variable space, the rest of the data types occupy only one. The memory space required for a local variable table is allocated during compilation, and when entering a method, this method needs to allocate a large amount of local variable space in the frame, which is completely determined and does not change the size of the local variable table during the run of the method.

There are two exceptions in this region: if the thread's request has 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, it will throw outofmemoryerror if it cannot request enough memory for the extension.

3, local method stack: The role of the local method stack on the virtual machine stack is very similar, the difference between them is the virtual machine stack for the virtual machine to execute Java method Services, and the local method stack for the virtual machine used by the native method service. In the virtual machine stack specification, the methods used in the method stack are not mandatory in terms of language, usage and data structure, so the virtual machine can implement it freely. Even a virtual machine, such as a hotspot, directly joins the local method stack with the virtual machine stack, and the local method stack, like the VM stack, throws Stackoverflowerror and OutOfMemoryError.

4. Heap: The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. The purpose of this area is to hold an instance of the object, where almost all of the object instances are allocated memory. The Java heap is the primary area of garbage collector management, and is sometimes referred to as the GC heap. From the memory recovery point of view, because the collector is now basically using a generational collection algorithm, so the Java heap can also be subdivided into: the new generation and the old age, and more detailed there is Eden space, from, survivor Space, to survivor space. From the memory allocation point of view, the thread-shared Java heap may divide multiple thread-private allocation buffers. However, regardless of the partition, is not related to the content, no matter what area, the storage is still an object instance, the further mapping is to better reclaim memory, or to allocate memory more quickly.

The Java heap can be physically disconnected from memory, as long as it is logically contiguous, just like our disk space. When implemented, it can be either fixed-sized or extensible, although the current mainstream virtual machines are implemented in a scalable way. OutOfMemoryError will be thrown if there is no memory in the heap to complete the instance assignment and the heap cannot be extended.

5, Program counter: is a small memory space, can be seen as the current thread execution of the line number indicator of the byte code. The bytecode interpreter works by changing the value of this counter to select the next byte-code instruction that needs to be executed, such as branching, looping, jumping, exception handling, thread recovery, and other basic functions that rely on this counter to complete. In order to ensure that the multi-threaded switch can be restored to the correct location, each thread has a separate program counter, the program counters between the various threads do not affect each other, independent storage, for thread-private memory. If the thread executes a Java method, this counter records the executing virtual machine bytecode instruction address, or null (Undefined) If the native method is actually executing. This memory area is the only area in the Java Virtual Machine specification that does not specify any outofmemoryerror conditions.

Java Virtual machine Learning-java memory Area (i)

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.