为什么 Java 字符串在堆上分配内存?
Why are Java Strings allocated memory on heap?
在Java中,为什么String
数据类型在堆上分配了内存?
原因很简单,所有对象都存储在堆上。它就是这样设计的。 String是一个class,它的对象会存储在堆上。
另请注意,String
文字先前存储在称为“permgen”堆的堆中。现在根据JVM Specification,存放字符串文字的区域在runtime constant pool.
只有原始数据类型存储在堆栈中。
Heap memory is used by java runtime to allocate memory to Objects and
JRE classes. Whenever we create any object, it’s always created in the
Heap space. Garbage Collection runs on the heap memory to free the
memory used by objects that doesn’t have any reference. Any object
created in the heap space has global access and can be referenced from
anywhere of the application.
的好点
Area: HotSpot
Synopsis: In JDK 7, interned strings are no longer
allocated in the permanent generation of the Java heap, but are
instead allocated in the main part of the Java heap (known as the
young and old generations), along with the other objects created by
the application. This change will result in more data residing in the
main Java heap, and less data in the permanent generation, and thus
may require heap sizes to be adjusted. Most applications will see only
relatively small differences in heap usage due to this change, but
larger applications that load many classes or make heavy use of the
String.intern() method will see more significant differences. RFE:
6962931
java 中的字符串是不可变的,因此它们在创建后无法更改。字符串文字存储在堆中的池中,因此如果您尝试使用相同的字符串文字创建另一个字符串。 java 不会创建另一个字符串,它将使用池中的字符串。
String a = "bla";
String b = "bla";
字符串文字 "bla" 将在池中创建一次。
默认情况下所有对象都在堆上。 String 有两个对象,String
和它包装的 char[]
。即使您直接创建 none,按类型查找数量最多的对象是 char[]
并不罕见。
令人惊讶的是,它并不总是在堆上创建对象,而是可以通过逃逸分析将对象放置在堆栈上。注意:它不能为字符串文字执行此操作,因为它们存储在字符串文字池中。
当用户输入字符串时,它始终是动态的,即字符串的大小可能会在每次执行时发生变化,因此编译器不知道字符串所需的确切内存要求。即使在运行期间,直到用户输入完整的字符串,才预测到字符串的大小,所以不能在栈上分配内存,所以一般会在栈上存储一个指向字符串的指针(在堆上)。
在Java中,为什么String
数据类型在堆上分配了内存?
原因很简单,所有对象都存储在堆上。它就是这样设计的。 String是一个class,它的对象会存储在堆上。
另请注意,String
文字先前存储在称为“permgen”堆的堆中。现在根据JVM Specification,存放字符串文字的区域在runtime constant pool.
只有原始数据类型存储在堆栈中。
的好点Heap memory is used by java runtime to allocate memory to Objects and JRE classes. Whenever we create any object, it’s always created in the Heap space. Garbage Collection runs on the heap memory to free the memory used by objects that doesn’t have any reference. Any object created in the heap space has global access and can be referenced from anywhere of the application.
Area: HotSpot
Synopsis: In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences. RFE: 6962931
java 中的字符串是不可变的,因此它们在创建后无法更改。字符串文字存储在堆中的池中,因此如果您尝试使用相同的字符串文字创建另一个字符串。 java 不会创建另一个字符串,它将使用池中的字符串。
String a = "bla";
String b = "bla";
字符串文字 "bla" 将在池中创建一次。
默认情况下所有对象都在堆上。 String 有两个对象,String
和它包装的 char[]
。即使您直接创建 none,按类型查找数量最多的对象是 char[]
并不罕见。
令人惊讶的是,它并不总是在堆上创建对象,而是可以通过逃逸分析将对象放置在堆栈上。注意:它不能为字符串文字执行此操作,因为它们存储在字符串文字池中。
当用户输入字符串时,它始终是动态的,即字符串的大小可能会在每次执行时发生变化,因此编译器不知道字符串所需的确切内存要求。即使在运行期间,直到用户输入完整的字符串,才预测到字符串的大小,所以不能在栈上分配内存,所以一般会在栈上存储一个指向字符串的指针(在堆上)。