【Java】How Java Memory Works

Xander / 56 / 2023-09-09 11:51:14

ChatGPT 可用网址,仅供交流学习使用,如对您有所帮助,请收藏并推荐给需要的朋友。


How Java Memory Works?. Before we move on to the performence… | by Berkay Haberal | Jul, 2023 | Stackademic (medium.com)

Before we move on to the performence things, we need to learn that what is really going on in the background of JVM (Java Virtual Machine).

在开始讨论性能问题之前,我们需要了解 JVM(Java 虚拟机)的后台到底发生了什么。

This is the starting point of every developer who wants to learn and tune performance in order to gain some speed. So let’s dive into the world of codes.


In Java, memory management is handled by JVM automatically to store your variables, classes, fields and beyond… The first thing we will learn is in JVM, memory splitted into two regions.

在 Java 中,内存管理由 JVM 自动处理,用于存储变量、类、字段和其他内容......我们首先要了解的是,在 JVM 中,内存分为两个区域。

One of them is called Stack and the other one is Heap.



What is Stack? 什么是堆栈?

First region we are going to learn is Stack.


In JVM, stack is very efficent approach to memory management and not only just one but also every thread has its own stack region.

在 JVM 中,堆栈是一种非常有效的内存管理方法,不仅是一个线程,每个线程都有自己的堆栈区域。

In stack, instantiated fieds are added to memory one on another just like its name stacking.


As you can see, this area is not big enough to store objects so what is happening is primitive types and object pointers can be stored directly instead of storing a whole object.



And when it is time to remove, first object needs to be removed first because like I said, data is stacked, we can’t reach the bottom unless we remove those items.



What is Heap? 什么是堆?

Now, let’s look at the other region called Heap.


As you can see it from the GIF, heap size is bigger than stack because heap is the main region for holding objects.

从 GIF 中可以看到,堆的大小大于栈,因为堆是存放对象的主要区域。

Every created object is held in heap and its reference is helding in stack.


Like figure shown below


 public List<String> test() {  
    String newString = "test";  
    List<String> testList = new ArrayList<>();  
    return testList;  

On the contrary, there is just one heap that application has.


That sounds quite reasonable because we might have more than one big object that passes one method to another.


So basically, stack is used for local variables and there could be many stacks but they all use one heap to store the objetcs.


Those objects pointers are held in stack so when we want to pass objects between methods, we are not copying objects like in stack, we are passing its reference.


Furthermore, heap is not one, solid region at all. If you zoom in to the heap, you will see 4 different areas

此外,堆根本不是一个完整的区域。如果放大堆,你会看到 4 个不同的区域


Actually those are called generations. Heap is builded on two main generations. One of them is young and the other one is old. Young generation is divided into three spaces. Eden, survivor zero and survivor one spaces. It will be more clear when you learn what they do. Created objects are first placed in eden space. Then eden is fulled, objects are moved to the survivor one or survivor zero. After, created objects are placed in eden again. When eden is full, both eden and survivor zero or one will be moved to the survivor zero or one. If objects are moved more than five, those objects are now placed in old generation. This means, now those objects are needed and will live in old generation unless it looses its pointer. If there are no variables in the stack that holds its reference, this means that object is eligible for garbage collection. Last one is so important for performance issues and thus we need to learn how Java memory works to understand it.

实际上,这就是所谓的 "代"。堆建立在两个主代上。一个是Young,另一个是Old。Young代分为三个空间。Edensurvivor0survivor1



如果移动的对象超过 5 次,这些对象就会被放置在Old中。



最后一条对于性能问题非常重要,因此我们需要学习 Java 内存的工作原理以便理解它。

One more room for Metaspace please! 请为 Metaspace 多留一个位置!

Besides these regions, there is one more region in memory I want to mention. ==Metaspace is the region where application’s metadata is stored.== 


Most of the times, we don’t need to know what is going on in metaspace due to its mission.


And there is one more mission it has and that is holding static variables, methods and classes in it.


This is why static keyword is accessable from anywhere because they are helding in metaspace so every thread can reach it easily.


Can we tune this size? 我们能调整这个尺寸吗?

Absolutely. There are flags we can use that tells JVM what to do when we are starting the application. We can some of these flags

当然可以。我们可以使用一些标记来告诉 JVM 在启动应用程序时要做什么。我们可以使用其中一些标志:

  • -XmsNg to set initial size
  • -XmxNg to set maximum size
  • -XX:NewRatio=N ratio of the young generation to the old generation
  • -XX:NewSize=N initial size of the young generariton
  • -XX:MaxNewSize=N maximum size of the young generation

-XmsNg :用于设置初始大小。
-XmxNg :用于设置最大大小。
-XX:NewRatio=N新生代与老年代的 N 比值

And we’ve reached our deadline. Before we move onto the Garbage Collection, it was essential to learn how Java memory works to have a better understanding about Garbage Collection. I hope I see you in my next article. Happy Coding!

在学习垃圾回收之前,我们有必要了解一下 Java 内存的工作原理,以便更好地理解垃圾回收。希望我们在下一篇文章中再见。编码快乐

【Java】How Java Memory Works
CC BY 4.0
Bonnie image