Java programs typically have three ways to allocate memory:
(1) Allocation from a static storage area. Memory is allocated at the time of program compilation, and the entire running period of the program is present in this block. For example, global variables.
(2) Create on the stack. In the execution function, the storage unit of local variables within the function can be created on the stack, which is freed automatically at the end of the function execution. Stack memory allocations are computationally efficient in the processor's instruction set, but the allocated memory capacity is limited.
(3) Allocation on the heap, also known as dynamic memory allocation. When the program is running, it uses malloc or new to request any amount of memory, and the programmer is responsible for freeing the memory with free or delete. The life cycle of dynamic memory is determined by us and is very flexible to use, but the problem is the most.
Introduction to Java Memory