Home>Article>Java> Introduction to JVM memory allocation methods

Introduction to JVM memory allocation methods

青灯夜游
青灯夜游 forward
2019-03-28 13:34:46 3493browse

The content of this article is to introduce the memory allocation method of JVM. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Preface

This article explains how memory allocation is performed when a java program is running?

There are three types of memory storage when Java virtual machine is compiled:

1,static (method area) storage

2,stack type Storage

3,Heap storage

Static storage means that the storage requirements of the data must be determined during compilation, and then a fixed amount of data must be allocated to it. Memory, so static storage does not allow variable data structures to appear, because variable data does not determine the storage space

Stack storage is just the opposite compared to static storage. At compile time, stack storage The specified storage data is uncertain. It will only be known when the data is actually run. At that time, memory space can be allocated for it.

Heap storage is compared to stack storage, which is stored before allocating space. It is necessary to specify how much memory to allocate for data, but heap storage cannot determine the memory space required for data structures, such as variable arrays and object instances, so the heap is composed of large areas of available blocks and free blocks

Stack and heap

Static storage is relatively simple, so we focus on analyzing the relationship and difference between stack and heap

Difference:
Once the data in the stack exceeds its scope, it will be released, and the memory will be occupied by other data
In the heap, the allocated memory is managed by the Java virtual machine automatic garbage collector. These Mutable arrays and objects will become garbage when there are no reference variables pointing to them, but they still occupy memory, and will be released by the garbage collector at an uncertain time.

In a JVM instance , there is only one heap area, and the stack can have multiple

relationships
After creating a data in the heap, you can define a variable in the stack, this variable points to the heap Certain data (pointing to the first address of the data), that is to say, this variable becomes a reference variable for the data in the heap. You can use the reference variable to access the data in the heap. This is the pointer of Java.

And each Java application will have a JVM instance, and each instance corresponds to a heap. During the running of this application, all class instances and arrays are placed in this heap, and when creating an object When memory is allocated from two places, in the heap it is the actual value of the object, and in the stack (stack, also called stack), the index of the object in the heap is allocated

Stack(stack)

Let’s take a look at this picture first (well, it’s very vivid)

Introduction to JVM memory allocation methods

JVM is based on stack Yes, every time a new thread is created, a stack will be allocated. It is based on frames and has the characteristics of first in, last out (see the picture to understand)

When a java method is activated, it is put into the stack. A frame (this isPushing on the stack). During the execution of this method, this frame will be used to save data. The existence of the

method is determined by the stack, and due to the first-in, last-out Form, the deeper the methods are nested, the harder it is to release the memory of the stack, so I do not recommend using recursive methods

The specific implementation of pushing and popping the stack is posted below

Use push and pop to reverse the string

String value = "test 1234567890"; StringBuffer result = new StringBuffer(); Stack stack = new Stack(); for(char c : value.toCharArray()) { stack.push(c); } while (!stack.empty()) { result.append(stack.pop()); } value = result.toString();

Related video tutorial recommendations: "Java Tutorial"

The above is the entire content of this article, I hope it can It will be helpful to everyone’s study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of Introduction to JVM memory allocation methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete