JVM Storage for Static Members – 静态成员的JVM存储

最后修改: 2021年 4月 26日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In our day-to-day work, we often don’t care about JVM’s internal memory allocation.

在我们的日常工作中,我们往往不关心JVM的内部内存分配。

However, knowing the basics of the JVM memory model comes in handy for performance optimization and improving code quality.

然而,了解JVM内存模型的基础知识对性能优化和提高代码质量很有帮助

In this article, we’ll explore JVM storage for the static methods and members.

在这篇文章中,我们将探讨JVM对静态方法和成员的存储。

2. JVM’s Memory Classification

2.JVM的内存分类

Before deep-diving into memory allocation for the static members, we must refresh our understanding of JVM’s memory structure.

在深入研究静态成员的内存分配之前,我们必须重新理解JVM的内存结构。

2.1. Heap Memory

2.1.堆内存

Heap memory is the runtime data area shared among all JVM threads to allocate memory for all class instances and arrays.

堆内存是所有JVM线程之间共享的运行时数据区域,用于为所有类实例和数组分配内存。

Java classifies heap memory into two categories – Young Generation and Old Generation.

Java将堆内存分为两类–年轻一代和老一代。

The JVM internally separates the Young Generation into Eden and Survivor Space. Similarly, Tenured Space is the official name of the Old Generation.

JVM在内部将年轻一代分为伊甸园和生存者空间。同样地,”终身制空间 “是 “老一代 “的正式名称。

The lifecycle of an object in the heap memory is managed by an automatic memory management system known as a garbage collector.

对象在堆内存中的生命周期由自动内存管理系统管理,该系统被称为垃圾收集器

Therefore, the garbage collector can automatically either deallocate an object or move it in the various sections of the heap memory (young to old generation).

因此,垃圾收集器可以自动删除一个对象或在堆内存的各个部分移动它(年轻一代到老一代)。

2.2. Non-Heap Memory

2.2.非堆栈式内存

Non-heap memory consists primarily of a method area that stores class structures, fields, method data, and the code for methods/constructors.

非堆内存主要由方法区组成,存储类结构、字段、方法数据和方法/构造器的代码

Similar to the Heap memory, all JVM threads have access to the method area.

与Heap内存类似,所有JVM线程都可以访问方法区。

The method area, also known as Permanent Generation (PermGen), is considered a part of Heap memory, logically, although the simpler implementations of JVM may choose not to garbage-collect it.

方法区,也被称为永久生成(PermGen),在逻辑上被认为是堆内存的一部分,尽管JVM的简单实现可能选择不对其进行垃圾收集。

However, Java 8 removes the PermGen space and introduces a new native memory space named Metaspace.

然而,Java 8取消了PermGen空间,并引入了一个名为Metaspace的新的本地内存空间

2.3. Cache Memory

2.3 缓存内存

The JVM reserves the cache memory area for the compilation and storage of native code, such as JVM internal structures and native code produced by the JIT compiler.

JVM为本地代码的编译和存储保留了缓存内存区域,例如JVM内部结构和JIT编译器产生的本地代码。

3. Static Members Storage Before Java 8

3.Java 8之前的静态成员存储

Before Java 8, PermGen stores static members like static methods and static variables. Additionally, PermGen also stores interned strings.

在Java 8之前,PermGen存储静态成员如静态方法和静态变量。此外,PermGen还存储了内部的字符串。

In other words, PermGen space stores the variables and their technical values, which can be primitives or references.

换句话说,PermGen空间存储了变量和它们的技术值,它们可以是原语或引用。

4. Static Members Storage From Java 8 and Beyond

4.静态成员存储从Java 8及以后

As we’ve already discussed, PermGen space is replaced with Metaspace in Java 8, resulting in a change for memory allocation of the static members.

正如我们已经讨论过的,PermGen空间在Java 8中被Metaspace所取代,导致静态成员的内存分配发生了变化。

Since Java 8, Metaspace only stores the class metadata, and heap memory keeps the static members. Furthermore, the heap memory also provides storage for interned strings.

从Java 8开始,Metaspace只存储类的元数据,而堆内存保存静态成员。此外,堆内存还为内部的字符串提供了存储。

5. Conclusion

5.总结

In this short article, we explored JVM storage for static members.

在这篇短文中,我们探讨了静态成员的JVM存储。

First, we took a quick look at the JVM’s memory model. Then, we discussed JVM storage for static members before and after Java 8.

首先,我们快速浏览了JVM的内存模型。然后,我们讨论了Java 8之前和之后JVM对静态成员的存储。

Simply put, we know that the static members were a part of PermGen before Java 8. However, since Java 8, they’re a part of heap memory.

简单地说,我们知道,静态成员在Java 8之前是PermGen的一部分。然而,从Java 8开始,它们是堆内存的一部分