Home > 类库下载 > java类库 > body text

Are static member variables in Java placed in heap memory or stack memory?

高洛峰
Release: 2016-10-13 15:25:49
Original
2266 people have browsed it

Heap area:

1. All objects are stored, and each object contains information about a corresponding class. (The purpose of class is to get operation instructions)
2. JVM has only one heap area (heap) shared by all threads. Basic types and object references are not stored in the heap, only the objects themselves are stored
Stack area:
1. Each thread contains A stack area. The stack only stores objects of basic data types and references to custom objects (not objects). Objects are all stored in the heap area.
2. The data in each stack (original types and object references) are private , other stacks cannot be accessed.
3. The stack is divided into 3 parts: basic type variable area, execution environment context, and operation instruction area (storage operation instructions).
Method area:
1. Also called the static area, like the heap, it is shared by all threads. The method area contains all class and static variables.

2. The method area contains elements that are always unique in the entire program, such as class and static variables.


When the program is running, we'd better know where to save the data. Special attention should be paid to the internal allocation. There are six places where data can be saved:

1. Register. This is the fastest save area because it's located in a different place than all other saves: inside the processor. However, the number of registers is very limited, so registers are allocated by the compiler as needed. We have no direct control over this, and it is impossible to find any trace of the register's existence in our program.
2. Stack. Resides in regular RAM (Random Access Memory) area. However, direct support for processing is available through its "stack pointer". Moving the stack pointer down creates new memory; moving it up releases that memory. This is an extremely fast and efficient way to save data, second only to registers. When creating a program, the Java compiler must know exactly the "length" and "time of existence" of all data saved in the stack. This is due to the fact that it has to generate the appropriate code to move the pointer up and down. This limitation undoubtedly affects the flexibility of the program, so although some Java data is saved on the stack - especially object handles, Java objects are not placed in it.
3. Heap. A general purpose memory pool (also a RAM area) in which java objects are saved. Unlike the stack: The most attractive thing about the "memory heap" or "heap" is that the compiler does not have to know how much storage space to allocate from the heap, nor how long the stored data will stay in the heap. Therefore, you will get greater flexibility when using the heap to save data. When you need to create an object, you only need to use the new command to compile the corresponding code. When these codes are executed, data will be automatically saved in the heap. Of course, to achieve this flexibility, you must pay a certain price: it will take longer to allocate storage space in the heap
4. Static storage. "Static" here means "located in a fixed position." While the program is running, statically stored data will be waiting to be called at any time. You can use the static keyword to indicate that a specific element of an object is static. But the Java object itself will never be placed in static storage space.
5. Constant storage. Constant values ​​are usually placed directly within the program code. It is safe to do so. Because they will never change, some constants need to be strictly protected, so consider placing them in read-only memory (ROM).
6. Non-RAM storage. If the data is completely independent of a program, it can exist when the program is not running and is outside the control of the program. Two of the most important examples are "streaming objects" and "fixed objects". With streaming objects, the object is turned into a stream of bytes, usually sent to another machine, whereas with fixed objects, the object is saved on disk. Even if the program is terminated, they can still maintain their state. A particularly useful trick for these types of data storage is that they can exist on other media and even be restored to regular, RAM-based objects if needed.


First of all, there is no concept of static variables in Java. If you don’t believe it, you can define a static int i = 0 in the method; there are only static member variables in Java. It is a property of the class. As for where he put it? What I'm talking about upstairs is the static area. I don't know if there is such a translation. But when you go deep into the jvm, it is translated into the method area. The architecture of the virtual machine: heap, method area, local method stack, pc register. The method area saves the template of a class, and the heap holds instances of the class. The stack is generally used for function calculations. Just look for any book on the basics of computers and you’ll know it. The data on the stack will not be stored after the function is executed. That's why local variables are the same every time. Even if you add one to it, it will still be the same the next time you execute the function.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!