Java stack memory of understanding

 

Java, variables are allocated in memory
1) class variables (static variables modified): when the program loads the system as it opens up memory on the heap, the heap memory address stored in the stack for high-speed access. The life cycle of static variables - until the whole "system" closed
2) instance variables: java when you use the new keyword, the system is not necessarily open up the heap variables (such as class instance contiguous space to allocate. ) and heap memory address according scattered by hash algorithm to characterize in terms of the long string of numbers in the stack variable "physical location." Lifecycle instance variables - instance variables when the reference is lost, is GC (garbage collector) as recyclable column "list", but does not immediately release the heap memory
3) local variables: Local variables by. declared in a method, or a snippet in (such as the for loop), it's time to execute on the heap memory to open up, the authorities once the variable goes out of scope, the memory is released immediately

Heap memory
What is the heap?
Java heap memory is a memory, its role is to store java objects and arrays, when we create a new array or an object and they will open up some space in the heap memory to it, for storage.
What features are heap memory?
The first point: the stack can actually be seen as similar to the pipeline, or a situation usually go to queue up for tickets almost, so the heap memory is characterized by: FIFO, after a backward, that is your first line is good, you first buy a ticket.
The second point: the size of heap memory can be allocated dynamically, survival do not have to tell the compiler in advance, because it is at run-time dynamic allocation of memory, but the disadvantage is that due to the slower dynamic allocation of memory at run-time access speed .
how to allocate new objects in the heap?
Managed by automatic garbage collector Java virtual machine.

Stack memory
stack memory is another Java memory is mainly used to perform procedures, such as: object reference variables and basic types of variable
stack memory features
first point: the stack memory is like a water bottle, fill it into something that Ma first into the sink to the bottom, so it is characterized by: last-out, LIFO
second point: access speed is faster than the heap, after register, stack data can be shared, but the downside is that there is data in the stack size and survival must be determined, lack of flexibility
stack memory allocation mechanism
stack memory can be called a cache, automatically recovered by the garbage collector
data sharing

The difference between stack and heap

JVM is a stack-based virtual machine, JVM for the newly created thread is assigned a stack, that is, for a Java program, it is run through the operation of the stack to complete. Stack units of frames saved state of the thread. JVM stacks of only two operations: a frame unit of the push and pop operations.

Differences:
1) heap memory used to store objects created by an array of new and
2) or a stack memory used to store local variables, etc. methods.
3) is a FIFO stack, after the backward.
4) after the stack is advanced out. , LIFO
. 5) share of the differences:

Stack memory is private to the thread of
heap memory is shared by all threads

Space 
stack space is much smaller than the size of the heap.

 

 

reference:

https://blog.csdn.net/weixin_37618354/article/details/80239219

 

Guess you like

Origin www.cnblogs.com/sea-stream/p/11361464.html