How to understand the Java stack

Source: Internet
Author: User

Java divides memory into two types: one is stack memory, and the other is heap memory.

The conceptual logic of the stack is that the content provided by all Java virtual machines is simulated by the software prior to the advent of Java processors that fully conform to the Java specification.

Some of the 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 automatic garbage collector of the Java Virtual machine.

When declaring an object, you first define a special variable in the stack, do not point to any memory address, and then create an array or object in the heap so that the value of the variable in the stack equals the array or the first address of the object in the heap memory.

A reference variable is a name that is an array or an object, and you can use reference variables from the stack to access the arrays or objects in the heap later in your program.

Unlike C + +, Java automatically manages stacks and heaps, and programmers cannot directly set up stacks or heaps.

The concept is finished, to see the specific difference:

1. Differences in data structure

Heaps and stacks are a data structure in which data items are ordered sequentially.

Stack is like a bucket or a box of data, it is a data structure with a last-in-first-out nature, that is, after the storage of the first fetch, the first storage after the take. It's like we're going to take out something that's under the box (put in an earlier object), and we'll first remove the object that's pressed on it (the relatively late object put in).

The heap is like an inverted tree, a sort of tree-shaped data structure with a value for each node. Usually what we call a heap of data structures is a two fork heap. The heap is characterized by the minimum (or maximum) value of the root node, and the two subtrees of the root node are also a heap. Because of this feature of the heap, commonly used to achieve the priority queue, heap access is arbitrary, it is like we in the library shelves to pick up books, although the book is placed in order, but we do not want to take any of the same as the stack, first remove all the previous books, bookshelf This mechanism is different from the box, we can directly take out the book

2. Security

The heap is flexible, but not secure. For the object, we want to create, destroy dynamically, not to say after the creation of the object is not destroyed, the previously created objects can not be destroyed, so that our program is not moving, so Java with the heap to store objects. And once the objects in the heap are destroyed and we continue to refer to this object, there will be the famous nullpointerexception, which is the heap's disadvantage-the wrong reference logic will only be found at runtime.

The stack is inflexible, but very strict, safe and easy to manage. Because as long as the above reference is not destroyed, the following reference must still be in, so, in the stack, the above reference can always find the object by the following reference, and if you confirm that the content of a certain interval will exist together, destroyed together, you can also refer to each other. In most programs, is the first defined variables, reference advanced stack, after the definition of the backward stack, while the variables inside the block, reference in the block when the stack, the block at the end of the stack, understand this mechanism, we can easily understand the concept of the scope of the various programming languages, and this is the advantages of the stack- The wrong reference logic can be found at compile time.

3. Different efficiency and flexibility

The Java heap is a run-time data area in which the objects of the class allocate space. These objects are created through directives such as new, which do not require program code to be explicitly released, but are responsible for garbage collection.

The advantage of the heap 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.

The disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.

The advantage of stack is: the access speed is faster than the heap, second only to register;

The disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible.

4, stack data can be shared.

Let's say we define both:

int a = 3;

int b = 3;

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. An object reference variable modifies the internal state of the object, affecting another object reference variable.

How to understand the Java stack

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.