Ali interview: How to store strings in JVM? 90% of people really only answer where to store

2020-08-02 21:28:15 0 Comment 1057 views
abstract

Most people can smoothly give the following answer: string objects may have two storage locations in the JVM: string constant pool or heap memory. Its

1. The introduction of an interview question

In interviews like BAT In the first-tier manufacturers, if the interviewer asks: How to store strings in JVM? Most people can smoothly give the following answers:

String objects may have two locations in the JVM: string constant pool or heap memory.

  • A string object initialized with a constant string, its value is stored in the string constant pool;
  • Use the string construction method to create a string object, and its value is stored in the heap memory;

But if you can address the above answer, you can expand it further , Will give you a lot of points for your interview performance, allowing you to stand out from a large wave of candidates. Let's analyze it together below.

Look at first, String provides an API, java.lang.String.intern(), this API can manually transfer the value of a string object to the string constant pool

Before 1.7, the string constant pool was in the PermGen area. The size of this area is fixed and cannot be expanded as needed during runtime, nor can it be recycled by the garbage collector. There are too manyIf the string calls the intern method, it may cause OOM.

After 1.7, the string constant pool is moved to the heap memory and can be recycled by the garbage collector. This change reduces the OOM risk of the string constant pool.

2. Case study


Ali interview: How to store strings in JVM? 90% of people really only answer where to store


authentication Code:

 public 

class

StringTest
{

public

static

void
main (
String
[] args) {

String
s1 =
"javaadu"
;

String
s2 =
"javaadu "
;

String
s3 =
new

String
(
" javaadu "
);

System

out
.println (s1 == s2);..
// true

System

out
.println (s1 = = s3);
// false

String
s4 = s3.intern ();.

System

out
.println (s1 == s4);
//true
}
}

3. Intern source code analysis

Let’s look at the implementation of the intern method, the intern method The bottom layer is a native method. The logic of the string constant pool in Hotspot JVM is clearly written in the comments:

If there is this string constant in the constant pool, it will return directly, otherwise Store the value of the string object in the constant pool and return.


Ali interview: How to store strings in JVM? 90% of people really only answer where to store


Take the source code of Openjdk1.8 as an example, and the underlying implementation of the intern method. The C file corresponding to the String.java file is String.c:

JNIEXPORT jobject JNICALL
Java_java_lang_String_intern
(
JNIEnv

*
env
,
jobject
this
)
{

return
JVM_InternString
(
env
,

this
);
}

this method is defined in JVM_InternString jvm.h, implemented in jvm.cpp, the JVM In the Java world and the C++ world, the connection layer is the two files jvm.h and jvm.cpp.

JVM_ENTRY
(
jstring
,
JVM_InternString
(
JNIEnv

*
env
,
jstring str
))

JVMWrapper
(
"JVM_InternString"
);

JvmtiVMObjectAllocEventCollector
oam
;

if

(
str
==
NULL
)

return
NULL
;
oop
string

=

JNIHandles
::
resolve_non_null
(
str
);
oop result
=

StringTable
::
intern
(
string
,
CHECK_NULL
);

return

(
jstring
)

JNIHandles
::
make_local
(
env
,
result
);
JVM_END

It can be seen that the string constant pool is a HashTable inside the JVM, which is Upper generationStringTable in the code.

Following the StringTable::intern method, you can find: If the string of this operation is found, it will directly return to found_string; if it is not found, the current string will be added to the HashTable, and then Come back again.

4. Summary

Appropriate use of String.intern() in Java applications can help to save memory space, but when used, it is also It should be noted that because the size of StringTable is fixed, if there are too many strings in the constant pool, it will affect the efficiency of the program.

avatar