Java in stacks (stack) and heap (heap)

Source: Internet
Author: User

A fuzzy point in previous studies:

Java in stacks (stack) and heap (heap)

One, stacks (stack) and heap (heap).

(1) Memory allocation strategy

According to the principle of compiling, there are three kinds of strategies for the memory allocation of program running, which are static, stack type, and heap type.


Static storage allocation refers to the need to determine the storage space for each data target at run time at compile time, as a result, they can be allocated a fixed memory space at compile time. This allocation policy requires that variable data structures (such as variable groups) are not allowed in the program code, and that nested or recursive structures are not allowed to appear. Because they all cause the compiler to not compute the exact storage space requirements.


Stack-type storage allocation, also known as dynamic storage allocation, is implemented by a stack of stacks. In contrast to static storage allocation, in a stack storage scenario, the requirements of a program for the data area are completely unknown at compile time, only to be known at runtime, but when running into a program module, You must know the size of the data area required by the program module to allocate memory for it. Like the stacks we know about in data structures, stack storage allocations are distributed according to the advanced principle.


Static storage allocation requires that the storage requirements of all variables be known at compile time, and stack storage allocation requires that all storage requirements be known at the entrance of the process, while the heap storage allocation is dedicated to the inability to determine the memory allocation of the data structure of the storage requirement at compile-time or runtime module entrances. such as variable length strings and object instances. The heap is made up of large chunks of available blocks or free blocks, and the memory in the heap can be allocated and released in any order.

(2) Comparison of Heap and stack

The above definition is summarized from the textbook of compiling principles, except for static storage allocation, it appears very stiff and difficult to understand, the following aside from static storage allocation, concentrated comparison heap and stack:


From the stack and stack function and function to the popular comparison, the heap is mainly used to store objects, the stack is mainly used to execute the program. And this difference is mainly due to the characteristics of the heap and stack:


In programming, for example, C/s + +, all method calls are done through stacks, and all local variables, formal parameters, are allocated memory space from the stack. It's not really a distribution, it's just going up from the top of the stack, like a conveyor belt in a factory (conveyor belt), stack pointer will automatically guide you to the place where you put things, All you have to do is just put things down. When you exit the function, you can destroy the contents of the stack by modifying the stack pointer. This mode is the fastest, of course, to run the program. It is important to note that when assigning a data area for a program module that is about to be called, Should know the size of the data area in advance, it is said that although the allocation is performed while the program is running, but the size of the allocation is determined, unchanged, and this "size" is determined at compile time, not at runtime.


A heap is an application that requests the operating system to allocate its own memory when it is running, and because of the memory allocations managed from the operating system, it takes time to allocate and destroy, so the heap is inefficient. But the advantage of the heap is that the compiler doesn't have to know how much storage to allocate from the heap, It is also not necessary to know how long the stored data will stay in the heap, so it is much more flexible to save the data with the heap. In fact, object-oriented polymorphism, heap memory allocation is essential, because the storage space required for polymorphic variables can only be determined after the object has been created at run time. In C + +, when you want to create an object, you simply use the new command to compile the relevant code. When this code is executed, the data is automatically saved in the heap. Of course, to achieve this flexibility, there is a certain cost: it takes longer to allocate storage space in the heap. This is what led to the inefficiency of what we have just said, it seems that comrade Lenin said good, people are often the advantages of human shortcomings, human shortcomings are often the advantages of people (halo ~).

(3) heap and stack in the JVM

The JVM is a stack based virtual machine. The JVM assigns a stack to each newly created thread. That is, for a Java program, it runs through the operation of the stack. The stack saves the state of the thread in frames. The JVM does only two things on the stack: A frame-by-stack and a stack operation.


We know that a method that a thread is executing is called the current method of this thread. We may not know that the current method uses a frame called the current frame. When a thread activates a Java method, the JVM presses a new frame into the Java stack on the thread. This frame naturally becomes the current frame. During the execution of this method, this frame is used to hold parameters, local variables, intermediate computations, and other data. This frame here is similar to the concept of the activity record in the compiler principle.


From this allocation mechanism in Java, the stack can also be understood: stacks (stack) is the storage area that the operating system establishes for this thread when it establishes a process or a thread (a thread in an operating system that supports multithreading), and the region has an advanced feature.


Each Java application uniquely corresponds to a JVM instance, with each instance uniquely corresponding to a heap. All of the class instances or arrays that the application creates in the run are placed in this heap and are shared by all threads that apply. Unlike C + +, the allocation heap memory in Java is automatically initialized. The storage space for all objects in Java is allocated in the heap, but the reference to this object is allocated in the stack, that is, allocating memory from two places when an object is created, the memory allocated in the heap actually establishes the object, and the memory allocated in the stack is just a pointer to the heap object (reference) Just.

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.