Reproduced:
One, Java stack
The Java stack is always associated with threads, and whenever a thread is created, the JVM creates a corresponding Java stack for that thread, which in turn contains multiple stacks (Frames), which are associated with each method, and each one that runs a method creates a stack frame that contains some Information such as " internal variables " (variables defined within a method), " Action Stacks ", and " method return Values ".
Second, the heap
The heap is where Java objects are stored, which is the core storage area of the JVM's management Java objects, and the heap is the most important concern for Java programmers because it is the storage area where our application is most closely related to memory.
Each Java object stored in the heap is a copy of the object's class, and it replicates all non-static properties that inherit from its parent class.
The heap is shared by all Java threads, so access to it requires attention to synchronization issues, and the methods and corresponding attributes need to be consistent.
Memory can be divided into 3 zones: Method Area (also called static Area method), stack (stack), and heap
1. Method area
Store all classes (that is, functions in the class are also stored in this area) and static variables, and the method area holds the unique elements of the entire program, such as class and static variables.
Can be shared by all threads, just like heap heaps.
2. Stack Memory:
All stored are local variables.
And once the scope of the variable belongs to the end, the variable is automatically freed.
3. Heap Memory
all are stored as entities, arrays and objects (arrays are objects), and each entity has an initial address value.
All new is in heap memory and will be initialized by default.
No manual release, automatic garbage collection is required.
?
The Entity data addresses in the heap memory are stored in the stack variable (that is, referenced) so that they can be accessed at high speed. When a reference is lost, it is added to the recyclable "list" by GC (garbage collector), which becomes garbage, waiting for automatic recycling (unlike the new array in c++,c++, which requires delete, the destructor needs to be destroyed in order to free memory).
About memory addresses:
Just focus on the left-most two characters:
"[" indicates that the entity is an array-type entity
I means that int f means float
The allocation of variables in memory in Java
1. Class variable (static modified variable): When the program loads, the system opens up memory in the heap, and the memory address in the heap is stored on the stack for high-speed access. The life cycle of a static variable--continues until the entire system shuts down
2, instance variables: When you use the Java keyword New, the system in the heap is not necessarily a continuous space allocated to variables (such as class instances), and then based on the scattered heap memory address, the hash algorithm is converted to a long series of numbers to characterize the variable in the heap "physical location." Life cycle of instance variables-when a reference to an instance variable is lost, it is included in the recyclable "list" by the GC (garbage collector), but the memory in the heap is not released immediately
3, local variables: local variables, by declaring in a method, or a code snippet (such as a For loop), execution to it in the stack to open up memory, when the local variable one but out of scope, memory immediately released
Java divides memory into two types: one is stack memory and the other is heap memory. Some basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function, and when a variable is defined in a block of code, Java allocates memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable. The memory space can be used immediately by another.
Heap memory is used to store the objects and arrays created by new, and the memory allocated in the heap is managed by the automatic garbage collector of the Java virtual machine. After creating an array or an object in the heap, you can also define a special variable in the stack that is equal to the first address of the array or object in the heap memory, and this variable in the stack becomes the reference variable of the array or object. The reference variable in the stack can then be used in the program to access an array or an object in the heap, which is equivalent to a name that is an array or an object. A reference variable is a normal variable that is allocated in the stack when defined, and the reference variable is freed after the program runs outside its scope. While the array and the object itself are allocated in the heap, the memory occupied by the array and the object itself is not freed when the program runs beyond the block of code that uses new to produce the array or object's statement, and the array and object become garbage when no reference variable points to it, not in use, but still occupy memory space. The garbage collector takes off (releases) at a later indeterminate time.
This is the reason why Java compares memory, in fact, the variables in the stack point to the variables in the heap memory, this is the pointer in Java!
The difference between a Java stack and a heap