Java memory overflow and java Overflow

Source: Internet
Author: User

Java memory overflow and java Overflow

 

  • Program counters

    A program counter is a small piece of memory, which is private to the thread and can be seen as a counter for executing bytecode in the current thread. In the virtual machine conceptual model, the bytecode interpreter uses this counter to find the next command to be executed. In java, branch statements, loops, exception handling, and thread recovery are implemented through program counters.

    Since the JVM executes each thread in turn through the CPU during thread execution, the CPU can only execute a certain instruction of one thread at a time. This requires that the correct Command Execution location be restored each time the thread is switched. Therefore, the program counter of the thread must be private to the thread. Each thread does not affect each other and is stored independently.

    If the thread is executing a java method, the value of the program counter is the instruction position of the current virtual bytecode being executed after the method is compiled. If the current thread executes a native method, the counter value is null Undefined. This region is the region where the VM specification does not specify an OutOfMemoryError.

  • Virtual Machine Stack

    This region is also private to the thread, and its lifecycle is consistent with that of the corresponding thread. The Virtual Machine stack describes the Memory Model of java method execution: each method creates a stack frame on the Virtual Machine stack during execution to save the local variable table, Operation stack, and dynamic link, method exit and other information. Each method is called to correspond to a stack frame in the Virtual Machine Stack from the stack channel out of the stack process.

    We often see that memory is divided into heap memory and stack memory. The stack memory here refers to the local variable table on the VM stack. The local variable table stores various basic data types known during the compilation period, such as int, float, boolean ...... And object reference. The 64-bit long and double types occupy two local variable storage spaces (slots ). The rest occupy one. The memory space required by the local variable table is completely determined during compilation. The size does not change during running. The Java virtual machine specification specifies two exceptions for this memory zone: If the stack depth of the thread request is greater than the maximum depth allowed by the virtual machine, StackOverflowError is thrown. if the Virtual Machine stack can be dynamically expanded and cannot be applied for with enough memory during expansion, an OutOfMemoryError will be thrown.

  • Local method Stack

    Like the preceding two regions, this region is also the private region of the thread. It is similar to the Virtual Machine stack. The difference is that the virtual machine stack serves java methods, while the local method stack serves local methods called by java. The local method Stack also throws two exceptions that are identical to the Virtual Machine stack.

  • Java heap

    The java heap memory is created when the VM is started. All object instances are saved in this region, including arrays. Therefore, heap memory is also the main work area of the garbage collector. From the perspective of memory collection, the collectors basically use generational collection policies, so java heap can be subdivided into the new generation and old generation. A little more detailed information can be divided into: Eden space, FromSurvior space, toyun or space.

  • Method Area

Stores information about classes, constants, static variables, and compiled code that have been loaded by virtual machines. Some people call this region a permanent generation. Java Virtual Machine specifications have loose requirements for this region. Therefore, the garbage collection mechanism can be implemented or not implemented in this region. The garbage collection mechanism in this area is mainly for the collection of constant pools and the uninstallation of types. This area does not require continuous memory and is scalable. When the method area cannot meet the memory allocation requirements, an OutOfMemoryError is thrown.

Runtime frequency

The runtime constant pool is a part of the Method Area. In addition to the version, field, method, interface description, and other information of the Class file, another information is the Constant Pool Table, which is used to store various literal quantities and symbolic references generated during the compilation period, this part will be placed into the constant pool area of the method area after the class is loaded.

 

 

Object Access

Object obj = new Object ();

 

Assuming that the above line of code appears in a method, the declaration of part of the "Object obj" semantics will be reflected to the local variable table on the Virtual Machine stack, as a reference type data. The semantics of "new Object ()" is reflected in the heap memory, forming a block that stores all instance data of the Object type (the data of each instance field in the Object) structured memory, the length of this memory is unchanged. In addition, the center must contain data (such as object type, parent class, implemented interface, and method) that can be queried to find the data type of this object) address information. These types of data are stored in the method area. To sum up, the heap memory needs to store two parts of data: (1) instance data of the object (2) address information of the object type data

 

The reference type does not specify which method to locate the reference type in the Java Virtual Machine specification. Therefore, the object access methods implemented by inaccessible virtual machines are different. There are two main methods: use a handle and directly use a pointer.

  • If an object is accessed by means of a handle, a block of memory needs to be divided into a handle pool in the java heap, And the handle address of the object is stored in the reference, the handle contains the instance data address information of the object and the address information of the type data.

 

      • If an object is accessed through a pointer, the address information of the object is directly stored in reference.

 

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.