Java heap, stack, stack

Source: Internet
Author: User

Stack: is an advanced data structure that is typically used to hold parameters in methods (functions), local variables. The advantage of the stack is that the access speed is faster than the heap, second only to the registers directly in the CPU
In Java, all base types and reference types are stored in the stack. The living space of the data in the stack is usually within the current scopes (that is, by {...} Area). The parameter value of the stored function, the value of the local variable, etc.

① Stack, also known as stacks, is a linear table with limited operations. The limitation is that only one end of the table is allowed to insert and delete operations. This end is called the top of the stack, and the opposite end is called the bottom of the stack.

② Stack is a bucket, and then put it in the first to take out, it has something to wait until it comes out ( advanced )

The ③ stack (stack) is the storage area that the operating system establishes for a process or thread (a thread in a multithreaded operating system) that has a FIFO feature that specifies the size of the stack that is required at compile time.

Heap: is a dynamically requested memory space (its list of free memory spaces is maintained by the operating system), and thememory space generated by the malloc statement in C is in the heap and needs to be released manually .
in Java, all objects constructed with new XXX () are stored in the heap , and the object is automatically destroyed when the garbage collector detects that an object is not referenced. So, theoretically, there is no limit to the living space of an object in Java, as long as a reference type points to it, It can be used anywhere. A heap is a memory space that is requested for a size when the program is running, rather than when the program is compiled . That is, allocating memory dynamically, without distinction between access and access to general memory.

In Java, there are six different places where data can be stored:
1. Register (Register). This is the fastest storage area because it is located in a different location from the other store-the inside of the processor. However, the number of registers is extremely limited, so the registers are allocated by the compiler on demand. You cannot directly control or feel any sign of register presence in the program.
2. Stacks (Stack). is located in Universal RAM, but it can be supported from the processor with its "stack pointer". If the stack pointer moves down, the new memory is allocated, and if you move up, the memory is freed. This is a fast and efficient method of allocating storage, second only to registers. When you create a program, the Java compiler must know the exact size and lifecycle of all the data stored in the stack, because it must generate the appropriate code to move the stack pointer up and down. This constraint limits the flexibility of the program, so while some Java data is stored on the stack-especially object references-the Java object does not store it.
3. Heap. A universal memory pool (also available in RAM) for storing so Java objects. The benefit of a heap that differs from the stack is that the compiler does not need to know how many storage areas to allocate from the heap, or how long the stored data survives in the heap. Therefore, there is great flexibility in allocating storage in the heap. When you need to create an object, you only need new to write a simple line of code, and when you execute this line of code, it will be automatically stored in the heap allocation. Of course, for this flexibility you have to pay the code accordingly. Storage allocation with heaps takes more time than storage storage with the stack.
4. Static storage (storage). "Static" here means "in a fixed position". The data stored in the static store that persists while the program is running. You can use the keyword static to identify a particular element of an object that is static, but the Java object itself never resides in a static storage space.
5. Constant storage (constant storage). Constant values are usually stored directly inside the program code, and it is safe to do so because they are never changed. Sometimes, in an embedded system, the constants themselves are separated from the rest, so in this case, you can choose to place them in ROM
6. Non-RAM storage. If the data is completely outside of the program, it can be free from any control of the program and can exist when the program is not running.
As far as speed is concerned, there are the following relationships:
Registers < Stacks < Heaps < other

It was written by a predecessor, very detailed//main.cpp  intA =0; Global Initialization ZoneChar*P1; Global uninitialized area main () {intb; StackCharS[] ="ABC"; StackChar*P2; StackChar*P3 ="123456";123456/0 in the constant area, p3 on the stack. Static   intc =0; Global (static) initialization zone P1=   (Char*) malloc (Ten); P2=   (Char*) malloc ( -);     Areas that are allocated 10 and 20 bytes are in the heap area. strcpy (P1,"123456");123456/0 is placed in the constant area, the compiler may associate it with the P3 point"123456"optimized into one place. }   

Using stacks like we eat in a restaurant, just order (apply), pay, and eat (use), eat full
Go, do not bother to cut vegetables, wash vegetables and other preparation work and washing dishes, brush pots and other finishing work, his advantage is fast, but since
Small by degrees.
Using the heap is like making your own favorite dishes, more troublesome, but more in line with their own tastes, and free
The high degree. Classic )

Java heap, stack, 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.