JVM, Heap, and stack in Java--Preliminary understanding

Source: Internet
Author: User

JVM --Java Virtual machine (Java VM)

--because of the heap and stack, we have to speak briefly about the JVM. (JVM details please find Niang ~)

First of all, we all know that Java has been promoting the slogan is: Once compiled, run everywhere. In fact, its implementation is because the Java program after a compilation, the Java code compiled into a byte code is a class file, and then as long as the corresponding JVM installed on different platforms, you can run the bytecode file, run our Java program written.

So it is the core and foundation of Java.

Personally, the approximate execution process is:

① loading A. class file (it can also load class files only)

② managing and allocating memory

③ Performing garbage collection

(Emmm. Let's get here. Here's the point)

 JVM memory Allocation-The JVM's memory is divided into five pieces, namely: PC Register, method area, Heap, Java stack, local method stack

      1. Method areas and heaps are shared by all threads

      2. Java stacks and PC registers are exclusive to threads, and in the time of creation of new threads

      3. Local method Stack: Stores the state of the local method call

The following focuses on heap and stack.

Heaps (heap) and stacks ( Stack)

A simple summary:

Stack: small space, faster, used to put object references, access faster than the heap.

Heap (Heep): Large, generally all created objects are placed here.

  

Heaps and stacks are the two collectively referred to in the two memory allocations, where Java is used to store data in RAM (Java automatically manages stacks and heaps, which programmers cannot set directly). There may be many different implementations, but the implementation conforms to several basic concepts:

1. Stack--LIFO. for the stack, the new data item in the stack is placed at the top of the other data, and you can only remove the topmost data (not overrun) when you remove it.

2. For heaps, data item locations are not in a fixed order. You can insert and delete them in any order, because they don't have the concept of "top" data.

  Stack : The Stack memory is the first memory area, storing all the local variables (usually defined in the method are local variables, outside the method is a global variable)

Note: The For loop is also defined as a local variable, which is the first load function to define the local variable, so the method advances the stack and then defines the variable.

A variable has its own scope (that is, by {...} area), the variable is freed once it leaves the scope (presumably the meaning of the variable is destroyed when the method executes outside the method). stack memory updates are fast because the life cycle of local variables is very short.

  Stack has a very important particularity, is that there is data in the stack can be shared . Cases:

    We also define:

            


  Heap: stores arrays and objects (actually arrays are objects), all new is built in the heap , the heap is the entity, the entity is used to encapsulate the data, and it is encapsulated multiple (multiple attributes of the entity), If a data disappears, the entity does not disappear, it can also be used, so the heap will not be released at any time , but the stack is not the same, the stack is stored in a single variable, the variable is released, then there is no. The entities inside the heap are not released, but are treated as garbage (the Java garbage collection mechanism is collected at irregular intervals).

Heap and Stack connections:

Suppose we now declare an array int in the main function arr = new Int[3], it now has no value, just an array object created in the heap, then it has a memory address, and the default initialization (uninitialized data is not available, so it is not available in the stack, The heap can be used, because the default initialization is too).

The stack then stores only the heap memory address (the object's reference), not this entity of the ARR array. We use this address in the stack to refer to the ARR array as an entity.

When ARR is set to NULL, that is, there is no point to reference. ARR is now treated as a garbage, automatic recovery in an unscheduled time (Java Auto-recycle mechanism).

The difference between heap and stack:

1. The stack memory is a local variable and the heap memory is the entity;

2. Stack memory is updated faster than heap memory, because the life cycle of local variables is very short;

3. Stack memory storage The variable life cycle is freed once it is finished, and the entity that the heap memory is stored in is recycled by the garbage collection mechanism.

4. The stack is last-in, first-out, with the data at the top. And the heap does not have the concept of the top.

JVM, Heap, and stack in Java--Preliminary understanding

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.