Java Memory Allocation
-------------------------------------------------------------------------
Java memory allocation mainly includes the following areas:
1.Register: We cannot control it in the program.
2.Stack: Stores basic types of data and object references, but the object itself is not stored in the stack, but stored in the heap.
3.Heap: Store data generated with new
4.Static domain: Static members stored in objects defined using static
5.Constant pool: Stores constants.
6.Non-RAM (Random Access Memory) Storage: Permanent storage space such as hard disk
Bytes ------------------------------------------------------------------------------------
1. Register (Registers ):
This is the fastest storage space, because registers are inside the processor, which is different from other storage media. However, the number of registers is limited.
The register area in the memory is allocated by the compiler as needed. Our program developers cannot control the register allocation through code.
Therefore, we can only look at the first storage region register without any impact on it ., There is no way to feel any signs of register in the program.
JVM only sets four most common registers.
Pc program counter
Optop operand stack top pointer
Frame current execution environment pointer
Vars pointer to the first local variable in the current execution environment
All registers are 32-bit.
The pc is used to record program execution. Optop, frame, and vars are used to record pointers to the Java stack.
Ii. Stack ):
Also knownStack
In normal RAM. The processor provides direct support through pointers.
When a program configures a new memory, the stack pointer moves back; when the memory is released, the pointer moves forward.
This method is not only fast, but also efficient, and the speed is second only to registers.
It is used to store object references and basic data type objects. It cannot be used to store Java objects.
III.Heap(Heap):
A common memory space used to store Java objects.
The difference between Heap and stack is that the compiler does not need to know how much space is configured from heap or how long it will take to configure from heap.
Therefore, the storage space configured from heap can be highly elastic. Every time you need to generate an object, you only need to use new in the program, and the heap configuration space will be used during execution.
Of course, you have to pay for this elasticity: it takes a lot of time to configure the space from the heap, compared to the configuration from the stack.
Heap and stack in single-thread memory space:
1.Both stack and heap are places where Java is used to store data in Ram. Java automatically manages stacks and stacks. Programmers cannot directly set stacks or stacks.
2.Advantages and disadvantages:
STACK:
The advantage of stack is that the access speed is faster than the heap speed, second only to the registers directly in the CPU.
However, the disadvantage is that the data size and lifetime in the stack must be fixed, and there is a lack of flexibility. In addition, stack data cannot be shared among multiple threads or stacks. However, variables with multiple equal values within the stack can point to one address.
Heap:
The advantage of heap is that the memory size can be dynamically allocated, and the lifetime does not have to be told to the compiler in advance. The Java garbage collector will automatically collect the data that is no longer used.
However, the slow access speed is due to the need to dynamically allocate memory during runtime.
3. The stack has a very important feature, that is, data in the stack can be shared.
4.Static domain:
The static storage area is used to store the data that always exists when the application is running at a fixed location, java specifically divides a static storage area in the memory to manage some special data variables, such as static data variables. It must be clear that Java objects are not stored in this area, instead, place some special elements in the object.
A class variable is also called a static variable, that is, a static variable is added before the variable;
Instance variables are also called object variables, that is, variables without static;
The difference is:
The difference between a class variable and an instance variable is that a class variable is shared by all objects. One object changes its value, and other objects get the changed result; instance variables are private objects. A certain object changes its value without affecting other objects;
Features:
Here "Static" refers to "in a fixed position" (although also in RAM ). During the running of the program, the static storage data will be waiting for calling at any time. The static keyword can be used to indicate that a specific element of an object is static. However, Java objects are never placed in static buckets.
Example:
Public class A {static int a = 0; // class variable private int B = 0; // instance variable} public class B {public void main (String [] args) {A a1 = new A (); A a2 = new A (); a1.a = 3; // equivalent to. a = 3; a1. B = 4; System. out. println (a2.a); // The result is 3 // class variables are for all objects, so a1 changes a and a2's a also changes System. out. println (a2. B); // The result is 0 // The instance only changes itself, so the B change of the a1 object does not affect the B variable of the a2 object }}
5. Constant pool:
The constant pool is used in java to save a copy of data in the compiled class file that has been determined during the compilation period. It includes constants in classes, methods, and interfaces, as well as String constants, such as String s = "java" declarative methods. Of course, it can also be expanded, constants generated by the executor are also placed in the constant pool. Therefore, the constant pool is considered a special memory space of JVM.
Background:
There is also a special type of elements in Java objects, which are called constants. Because the constant value remains stable, such as the circumference rate. Therefore, it is feasible to put them inside the code.
However, sometimes we do not do this when developing embedded systems. Instead, constant elements are separated from the code and saved.
For example, we store the constant values in some read-only memory as needed. This is mainly for some special functional considerations.
For example, due to copyright control. For example, to protect the copyright of original consumables on a printer, constants and codes are often stored separately.
Features:
In Java programs, there are many things that are permanent and will not change during the running process.
For example, a class name, a class field name/type, a class method name/return type/parameter name and type, a constant, there are also a large number of nominal values in the program.
Constant values are usually placed directly inside the program code. This is safe because they will never change. Some constants must be strictly protected, so you may consider placing them in read-only memory (ROM ).
6. Non-RAM storage:
If the data is completely independent from a program, the program may still exist when it is not run and is out of the control scope of the program. Two major examples are "stream object" and "fixed object ". For stream objects, the object is converted into a byte stream, which is usually sent to another machine. For fixed objects, objects are stored on disks.
Even if the program stops running, they can remain in their own State. A particularly useful technique for these types of data storage is that they can be stored in other media. Once needed, they can even be restored to common, RAM-based objects.
Background:
Sometimes, the data required for running some programs is stored elsewhere. For example, stream objects need to be used in some systems. The data of this object is not stored in any storage area mentioned above. This object is directly converted to a byte stream, it is sent to another host. Another object called persistence is stored in the hard disk.
7. On the execution speed of various types of memory:
Register> stack> heap> Other
(C) cats in the room. All rights reserved.
Https://www.cnblogs.com/lsy131479/
If you need to reprint it, please indicate the source !!!