Site
Site
文章目录
  1. About
  2. Memory allocation strategy
  3. Heap and stacks in Java

JAVA Stacks And Heaps

About

  Some basic types of variables defined in the function and the object reference variables are allocated in the function’s stack memory.
  Define a variable in a block of code, java on the stack for this variable to allocate memory space,when the scope of variables beyond,java will automatically release the memory space of the variable, the memory space can be used for other purposes.
  Heap memory is used to store objects and arrays created by new. The allocated memory in the heap is managed by the java virtual machine’s automatic garbage collector. After an array or object is created in the heap,you can also define a special variable in the stack whose value is equal to the first address of the array or object in the heap memory. This special variable in the stack becomes Arrays or objects referenced variables can be used in the program after the reference variable in the stack memory to access the heap array or object,the reference variable is equivalent to an array or object from an alias, or code.
  For arrays, objects, the heap memory occupied by arrays and objects themselves will not be released,even if the program runs into the code block where the statements they produce are located. Arrays and objects become garbage when they are not referenced by variables. It can no longer be used, but still occupies memory and is freed by the garbage collector at a later time. This is also the main reason for java memory, in fact, the stack variable points to the heap memory variables, this is the pointer in Java

Memory allocation strategy

  Memory allocation has three strategies, namely static, stack, and heap.

  •   Static storage allocation refers to the storage space that each data object needs to be determined at compile time so that fixed memory space can be allocated at compile time.This allocation strategy requires that variable data is not allowed in the program code The existence of structures, such as mutable arrays, also does not allow nested or recursive structures to appear as they all result in the compiler not being able to calculate the exact memory requirements.
  •   Stacked storage allocation can also be referred to as dynamic storage allocation, implemented by a stack-like runtime stack. In a storied storage strategy, the program’s data area requirements are completely unknown at compile time and are not known until runtime , But when entering a program module on the fly it is necessary to know the size of the data area required by the program module to allocate memory for it.As with the well-known stack in the data structure, the stack memory allocation is advanced Principle of distribution.
  •   Heap storage allocations are solely responsible for allocating memory allocations for data structures that store requirements, such as variable length strings and object instances, at compile time or at runtime module entries. The heap is made up of large blocks of available or free blocks, The memory can be allocated and released in any order.
      A heap is an application that requires the operating system to allocate memory at run time and takes up time to allocate and destroy, so using heaps is inefficient, but the heap has the advantage that the compiler does not have to know how much memory to allocate from the heap , And you do not have to know how long the stored data stays in the heap, so you have greater flexibility in saving data with the heap. In fact, object-oriented polymorphism, heap memory allocation is essential, because the storage space required for polymorphic variables can only be determined after the object has been created at run time.When executing the code to create the object, it will be in the heap Automatically save the data, but it takes more time to save in the heap.

Heap and stacks in Java

  JVM is a stack-based virtual machine.JVM for each newly created thread is assigned a stack.That is, for a Java program, it runs through the stack to complete the operation. The stack saves the state of the thread in frames. The JVM performs only two operations on the stack: push and pop operations on a frame-by-frame basis.When a java method is activated,the JVM pushes a new frame into the thread’s stack, called the current frame, to hold parameters, local variables, intermediate calculations, and other data.
  When creating an object in Java, memory is allocated both in the stack and in the heap. The memory allocated in the heap actually establishes that for sex, and the memory allocated on the stack is just a pointer to the heap.
  Stacks and heaps are all used by Java to store data in Ram. Unlike C ++, Java automatically manages stacks and heaps.Java heap is a runtime data area, the object of the class from which to allocate space. They do not need to be explicitly released by the program code, but are responsible for garbage collection, Java’s garbage collector will automatically take away the data is no longer in use. Stack has a very important special, that is, there is data in the stack can be shared.

支持一下
扫一扫,支持xfan
  • 微信扫一扫
  • 支付宝扫一扫