Java Stack Memory Analysis detailed

Source: Internet
Author: User

Computer terminology the heap and stack represent different storage structures: stack-stack; heap-heap

So the heap and stack in the Java Virtual Machine (JVM) are two kinds of memory

Heap, stack contrast
Contrast point Heap Stack
Features in the JVM Memory Data area Memory Instruction Area
Static and dynamic state Run-time data area, dynamically allocating memory size
Storing data An object instance (which holds an object instance, is actually a property value that holds an object instance, the type of the property and the type tag of the object itself, etc.) and does not save the object's method (by instruction, saved in the stack). After the object instance is allocated in the heap, you need to save a 4-byte heap memory address in the stack to locate the object instance in the heap so that it is easy to locate the object instance) Basic data types, instruction codes, constants, reference addresses for objects (basic data types include byte, int, char, long, float, double, Boolean, and short, function methods belong to directives)
Recycling method To be responsible for the garbage collection After the scope of the variable is exceeded, the automatic release
Advantages and Disadvantages The advantage is that the memory size can be allocated dynamically, and the lifetime does not have to tell the compiler beforehand because it allocates memory dynamically at runtime, and the Java garbage collector automatically collects the data that is no longer used. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time. The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible.

stack: equivalent to the memory instruction area, which stores the basic data types, instruction codes, constants, reference addresses of the objects (basic data types include byte, int, char, long, float, double, Boolean, and short, the function method belongs to the directive).

Heap: is a runtime data area, memory data area, storage object instance, save object instance, actually Save object instance property value, property type and object itself type tag, etc., do not save Object method (method is instruction, save in stack). After the object instance is allocated in the heap, you need to save a 4-byte heap memory address in the stack to locate the object instance in the heap so that it is easy to locate the object instance.

Description of some stacks circulating on the Internet:

some basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function. When a variable is defined in a block of code, Java allocates a memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable, which can be used immediately by another.

heap memory is used to hold objects and arrays created by new . The memory allocated in the heap is managed by the Java Virtual Machine automatic garbage collector. After creating an array or an object in the heap, you can also define a special variable in the stack that is equal to the array or the first address of the object in the heap memory, and this particular variable in the stack becomes the reference variable of the array or object. You can then use the reference variable in the stack memory in your program to access the array or object in the heap, which is the equivalent of an alias, or codename, of an array or object.

A reference variable is a normal variable that is defined when memory is allocated in the stack, and the reference variable is released in the program run to the extraterritorial scope. The array and object itself is allocated in the heap, and even if the program runs beyond the block of code that uses the new generation of arrays and objects, the heap memory that the array and the object itself occupies will not be freed, and arrays and objects become garbage, no longer used, but still occupy memory when no reference variable points to it. is released by the garbage collector at a later indeterminate time. This is also the main reason for the memory of Java comparison, in fact, the variables in the stack point to the heap memory variables, this is the pointer in Java!

The JVM is a stack-based virtual machine, and the JVM allocates a stack for each newly created thread. In other words, for a Java program, it runs through the operation of the stack. The stack holds the state of the thread in frames. The JVM operates on the stack in only two ways: stack and stack operations in frames.

Each Java application uniquely corresponds to a single JVM instance, and each instance uniquely corresponds to one heap. All instances or arrays of classes created by the application in the run are placed in this heap and shared by all threads of the application.

The Java heap is a run-time data area in which objects allocate space. These objects are established through directives such as new, NewArray, Anewarray, and Multianewarray, and they do not require program code to be explicitly released. Heap is responsible for garbage collection, the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer use data. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.

The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. The stack mainly contains some basic types of variables (, int, short, long, byte, float, double, Boolean, char) and object handle.

Stack has a very important particularity, is that there is data in the stack can be shared. Let's say we define both:

  int a = 3;  int


The compiler processes int a = 3 First, it creates a reference to a variable in the stack, and then finds out if there is a value of 3 in the stack, and if it does not, it stores the 3 in and then points a to 3. then the int b = 3 is processed, and after the reference variable of B is created, because there are already 3 values in the stack, B points directly to 3. In this case, A and B both point to 3. At this point, if you make a=4 again, then the compiler will re-search the stack for 4 values, if not, then store 4 in, and a point to 4; Therefore the change of a value does not affect the value of B. It is important to note that this sharing of data with two object references also points to an object where this share is different, because the modification of a does not affect B, which is done by the compiler, which facilitates space saving. While an object reference variable modifies the internal state of the object, it affects another object reference variable

Java Stack Memory Analysis detailed

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.