1. Introduction
1.绪论
Whenever we declare a variable or create an object, it is stored in the memory. At a high level, Java divides the memory into two blocks: stack and heap. Both memories store specific types of data and have different patterns for their storage and access.
每当我们声明一个变量或创建一个对象时,它就被存储在内存中。在高层次上,Java将内存划分为两个块。stack和heap。这两种内存都存储着特定类型的数据,并且有不同的存储和访问模式。。
In this tutorial, we’ll look at different parameters and learn which is the most appropriate area to store the String constant pool.
在本教程中,我们将查看不同的参数,并了解哪个是最适合存储String常量池的区域。
2. String Constant Pool
2.字符串常量库
The String constant pool is a special memory area. When we declare a String literal, the JVM creates the object in the pool and stores its reference on the stack. Before creating each String object in memory, the JVM performs some steps to decrease the memory overhead.
String 常量池是一个特殊的内存区域。当我们声明一个String字面时,JVM会在池中创建对象并将其引用存储在堆栈中。在内存中创建每个String对象之前,JVM会执行一些步骤来减少内存开销。
The String constant pool uses a Hashmap in its implementation. Each bucket of the Hashmap contains a list of Strings with the same hash code. In earlier versions of Java, the storage area for the pool was a fixed size and could often lead to the “Could not reserve enough space for object heap” error.
字符串常量池在其实现中使用Hashmap。Hashmap的每个桶都包含一个具有相同哈希代码的Strings列表。在早期的 Java 版本中,该池的存储区域是一个固定的大小,并且常常会导致 “无法为对象堆保留足够的空间“错误。
When the system loads the classes, String literals of all classes go to the application-level pool. It is because of the fact that equal String literals of different classes have to be the same Object. In these situations, data in the pool should be available to each class without any dependency.
当系统加载类时,所有类的String字头都会进入应用级池。这是因为不同类的相等String字头必须是相同的Object。在这种情况下,池子里的数据应该对每个类都可用,没有任何依赖性。
Usually, the stack stores the data that is short-lived. It includes local primitive variables, references of heap objects, and methods in execution. Heap allows dynamic memory allocation, stores the Java objects and JRE classes at the runtime.
通常情况下,堆栈存储的数据都是短期的。它包括本地原始变量、堆对象的引用和执行中的方法。堆允许动态内存分配,在运行时存储Java对象和JRE类。
The heap allows global access and data stores in the heap are available to all threads during the lifetime of the application, whereas the data stores on the stack have the private scope and only the owner thread can access them.
堆允许全局访问,在应用程序的生命周期中,堆中的数据存储对所有线程都是可用的,而堆中的数据存储具有私有范围,只有所有者线程可以访问它们。
The stack stores the data in contiguous memory blocks and permits random access. If a class needs a random String from the pool, it might not be available due to the LIFO (last-in-first-out) rule of the stack. In contrast, the heap allocates the memory dynamically and allows us to access the data in any way.
堆栈将数据存储在连续的内存块中,并允许随机访问。如果一个类需要一个随机的String,由于堆栈的LIFO(后进先出)规则,它可能无法使用。相比之下,堆动态地分配内存,允许我们以任何方式访问数据。
Let’s assume we have a code snippet consisting of different types of variables. The stack will store the value of the int literal and references of String and Demo objects. The value of any object will be stored in the heap, and all the String literals go in the pool inside the heap:
让我们假设我们有一个由不同类型的变量组成的代码片断。堆栈将存储int字面的值以及String和Demo对象的引用.任何对象的值都将存储在堆中,所有String字面的值都进入堆内的池。
The variables created on the stack are deallocated as soon as the thread completes execution. In contrast, a garbage collector reclaims the resources in the heap. Similarly, the garbage collector collects the un-referenced items from the pool.
一旦线程完成执行,在堆栈上创建的变量就会被删除。相反,垃圾收集器会回收堆中的资源。同样地,垃圾收集器也会从池中收集未被引用的项目。
The default size of the pool may differ on the different platforms. In any case, it is still much bigger than the available stack size. Before JDK 7, the pool was part of permgen space, and from JDK 7 to now, it is part of the main heap memory.
池的默认大小在不同的平台上可能有所不同。在任何情况下,它仍然比可用的堆栈大小大得多。在JDK 7之前,池是permgen空间的一部分,而从JDK 7到现在,它是主堆内存的一部分。
3. Conclusion
3.总结
In this short article, we learned about the storage area for String constant pool. Stack and heap have different characteristics to store and access data. From memory allocation to its access and availability, a heap is the most suitable area to store the String constant pool.
在这篇短文中,我们了解了String常量池的存储区域。堆栈和堆在存储和访问数据方面有不同的特点。从内存分配到其访问和可用性,堆是最适合存储字符串常量池的区域。
In fact, the pool has never been a part of stack memory.
事实上,池子从来没有成为堆栈内存的一部分。