在 Java/C# 這樣基於引用語義處理字串的語言中,作為不可變對象存在的字串,如果內容相同,則可以通過某種機制實現重用。因為對這類語言來說,指向記憶體中兩塊記憶體位置不同內容相同的字串,與同時指向一個字串並沒有任何區別。特別是對大量使用字串的 XML 檔案解析類似場合,這樣的最佳化能夠很大程度上降低程式的記憶體佔用,如 SAX 解析引擎標準中就專門定義了一個 http://xml.org/sax/features/string-interning 特性用於字串重用。
在語言層面,Java/C# 中都直接提供了 String.Intern 的支援。而對 Java 來說,實現上的非常類似。由 String.intern 方法,將當前字串以內容為鍵,對象引用為值,放入一個全域性的雜湊表中。
代碼:
// // java/lang/String.java //public final class String { //... public native String intern(); // 使用 JNI 函數實現以保障效率 } // // hotspot/src/share/vm/prims/jvm.cpp // JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str)) JVMWrapper("JVM_InternString"); if (str == NULL) return NULL; oop string = JNIHandles::resolve_non_null(str); // 將引用解析為內部控制代碼 oop result = StringTable::intern(string, CHECK_0); // 進行實際的字串 intern 操作 return (jstring) JNIHandles::make_local(env, result); // 擷取內部控制代碼的引用 JVM_END // // hotspot/src/share/vm/memory/symbolTable.cpp // oop StringTable::intern(oop string, TRAPS) { if (string == NULL) return NULL; ResourceMark rm(THREAD); // 保護線程資來源區域 int length; Handle h_string (THREAD, string); jchar* chars = java_lang_String::as_unicode_string(string, length); // 擷取實際字串內容 oop result = intern(h_string, chars, length, CHECK_0); // 完成字串 intern 操作 return result; } oop StringTable::intern(Handle string_or_null, jchar* name, int len, TRAPS) { int hashValue = hash_string(name, len); // 首先根據字串內容計算雜湊值 stringTableBucket* bucket = bucketFor(hashValue); // 根據雜湊值擷取目標容器 oop string = bucket->lookup(name, len); // 然後檢測字串是否已經存在 // Found if (string != NULL) return string; // Otherwise, add to symbol to table return basic_add(string_or_null, name, len, hashValue, CHECK_0); // 將字串放入雜湊表 } |
對全域字串表中的字串,是沒有辦法顯式手動清除的。只能在不使用此字串後,由記憶體回收線程在進行不可達對象標記時進行分析,並最終調用 StringTable::unlink 方法去遍曆清除。
代碼:
// // hotspot/src/share/vm/memory/genMarkSweep.cpp //void GenMarkSweep::mark_sweep_phase1(...) { //... StringTable::unlink(); } // // hotspot/src/share/vm/memory/symbolTable.cpp // void StringTable::unlink() { // Readers of the string table are unlocked, so we should only be // removing entries at a safepoint. assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint") for (stringTableBucket* bucket = firstBucket(); bucket <= lastBucket(); bucket++) { for (stringTableEntry** p = bucket->entry_addr(); *p != NULL;) { stringTableEntry* entry = *p; assert(entry->literal_string() != NULL, "just checking"); if (entry->literal_string()->is_gc_marked()) { // 字串對象是否可達 // Is this one of calls those necessary only for verification? (DLD) entry->oops_do(&MarkSweep::follow_root_closure); p = entry->next_addr(); } else { // 如不可達則將其記憶體塊回收到記憶體池中 *p = entry->next(); entry->set_next(free_list); free_list = entry; } } } } |
通過上面的代碼,我們可以直觀瞭解到,對 JVM (Sun JDK 1.4.2) 來說,String.intern 提供的是全域性的基於雜湊表的共用支援。這樣的實現雖然簡單,並能夠在最大限度上進行字串共用;但同時也存在共用粒度太大,最佳化效果無法度量,大量字串可能導致全域字串表效能降低等問題。
為此 Eclipse 捨棄了 JVM 一級的字串共用最佳化機制,而通過提供細粒度、完全可控、可測量的字串分區共用最佳化機制,一定程度上緩解此問題。Eclipse 核心的 IStringPoolParticipant 介面由使用者顯式實現,在其 shareStrings 方法中提交需要共用的字串。
代碼:
// // org.eclipse.core.runtime.IStringPoolParticipant //public interface IStringPoolParticipant { /** * Instructs this participant to share its strings in the provided * pool. */ public void shareStrings(StringPool pool); } |
例如 MarkerInfo 類型實現了 IStringPoolParticipant 介面,在其 shareStrings 方法中,提交自己需要共用的字串 type,並通知其下級節點進行相應的提交。
代碼:
// // org.eclipse.core.internal.resources.MarkerInfo //public class MarkerInfo implements ..., IStringPoolParticipant { public void shareStrings(StringPool set) { type = set.add(type); Map map = attributes; if (map instanceof IStringPoolParticipant) ((IStringPoolParticipant) map).shareStrings(set); } } |
這樣一來,只要一個對象樹各級節點選擇性實現 IStringPoolParticipant 介面,就可以一次性將所有需要共用的字串,通過遞迴提交到一個字串緩衝池中進行複用最佳化。如 Workspace 就是這樣一個字串共用根入口,其 open 方法在完成工作區開啟操作後,將需要進行字串共用最佳化的緩衝管理對象,加入到全域字串緩衝區分區最佳化列表中。
代碼:
// // org.eclipse.core.internal.resources // public class Workspace ... { protected SaveManager saveManager; public IStatus open(IProgressMonitor monitor) throws CoreException { // 開啟工作空間 // 最終註冊一個新的字串緩衝池分區 InternalPlatform.getDefault().addStringPoolParticipant(saveManager, getRoot()); return Status.OK_STATUS; } } |
在 Java/C# 這樣基於引用語義處理字串的語言中,作為不可變對象存在的字串,如果內容相同,則可以通過某種機制實現重用。因為對這類語言來說,指向記憶體中兩塊記憶體位置不同內容相同的字串,與同時指向一個字串並沒有任何區別。特別是對大量使用字串的 XML 檔案解析類似場合,這樣的最佳化能夠很大程度上降低程式的記憶體佔用,如 SAX 解析引擎標準中就專門定義了一個 http://xml.org/sax/features/string-interning 特性用於字串重用。
在語言層面,Java/C# 中都直接提供了 String.Intern 的支援。而對 Java 來說,實現上的非常類似。由 String.intern 方法,將當前字串以內容為鍵,對象引用為值,放入一個全域性的雜湊表中。
代碼:
// // java/lang/String.java //public final class String { //... public native String intern(); // 使用 JNI 函數實現以保障效率 } // // hotspot/src/share/vm/prims/jvm.cpp // JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str)) JVMWrapper("JVM_InternString"); if (str == NULL) return NULL; oop string = JNIHandles::resolve_non_null(str); // 將引用解析為內部控制代碼 oop result = StringTable::intern(string, CHECK_0); // 進行實際的字串 intern 操作 return (jstring) JNIHandles::make_local(env, result); // 擷取內部控制代碼的引用 JVM_END // // hotspot/src/share/vm/memory/symbolTable.cpp // oop StringTable::intern(oop string, TRAPS) { if (string == NULL) return NULL; ResourceMark rm(THREAD); // 保護線程資來源區域 int length; Handle h_string (THREAD, string); jchar* chars = java_lang_String::as_unicode_string(string, length); // 擷取實際字串內容 oop result = intern(h_string, chars, length, CHECK_0); // 完成字串 intern 操作 return result; } oop StringTable::intern(Handle string_or_null, jchar* name, int len, TRAPS) { int hashValue = hash_string(name, len); // 首先根據字串內容計算雜湊值 stringTableBucket* bucket = bucketFor(hashValue); // 根據雜湊值擷取目標容器 oop string = bucket->lookup(name, len); // 然後檢測字串是否已經存在 // Found if (string != NULL) return string; // Otherwise, add to symbol to table return basic_add(string_or_null, name, len, hashValue, CHECK_0); // 將字串放入雜湊表 } |
對全域字串表中的字串,是沒有辦法顯式手動清除的。只能在不使用此字串後,由記憶體回收線程在進行不可達對象標記時進行分析,並最終調用 StringTable::unlink 方法去遍曆清除。
代碼:
// // hotspot/src/share/vm/memory/genMarkSweep.cpp //void GenMarkSweep::mark_sweep_phase1(...) { //... StringTable::unlink(); } // // hotspot/src/share/vm/memory/symbolTable.cpp // void StringTable::unlink() { // Readers of the string table are unlocked, so we should only be // removing entries at a safepoint. assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint") for (stringTableBucket* bucket = firstBucket(); bucket <= lastBucket(); bucket++) { for (stringTableEntry** p = bucket->entry_addr(); *p != NULL;) { stringTableEntry* entry = *p; assert(entry->literal_string() != NULL, "just checking"); if (entry->literal_string()->is_gc_marked()) { // 字串對象是否可達 // Is this one of calls those necessary only for verification? (DLD) entry->oops_do(&MarkSweep::follow_root_closure); p = entry->next_addr(); } else { // 如不可達則將其記憶體塊回收到記憶體池中 *p = entry->next(); entry->set_next(free_list); free_list = entry; } } } } |
通過上面的代碼,我們可以直觀瞭解到,對 JVM (Sun JDK 1.4.2) 來說,String.intern 提供的是全域性的基於雜湊表的共用支援。這樣的實現雖然簡單,並能夠在最大限度上進行字串共用;但同時也存在共用粒度太大,最佳化效果無法度量,大量字串可能導致全域字串表效能降低等問題。
為此 Eclipse 捨棄了 JVM 一級的字串共用最佳化機制,而通過提供細粒度、完全可控、可測量的字串分區共用最佳化機制,一定程度上緩解此問題。Eclipse 核心的 IStringPoolParticipant 介面由使用者顯式實現,在其 shareStrings 方法中提交需要共用的字串。
代碼:
// // org.eclipse.core.runtime.IStringPoolParticipant //public interface IStringPoolParticipant { /** * Instructs this participant to share its strings in the provided * pool. */ public void shareStrings(StringPool pool); } |
例如 MarkerInfo 類型實現了 IStringPoolParticipant 介面,在其 shareStrings 方法中,提交自己需要共用的字串 type,並通知其下級節點進行相應的提交。
代碼:
// // org.eclipse.core.internal.resources.MarkerInfo //public class MarkerInfo implements ..., IStringPoolParticipant { public void shareStrings(StringPool set) { type = set.add(type); Map map = attributes; if (map instanceof IStringPoolParticipant) ((IStringPoolParticipant) map).shareStrings(set); } } |
這樣一來,只要一個對象樹各級節點選擇性實現 IStringPoolParticipant 介面,就可以一次性將所有需要共用的字串,通過遞迴提交到一個字串緩衝池中進行複用最佳化。如 Workspace 就是這樣一個字串共用根入口,其 open 方法在完成工作區開啟操作後,將需要進行字串共用最佳化的緩衝管理對象,加入到全域字串緩衝區分區最佳化列表中。
代碼:
// // org.eclipse.core.internal.resources //public class Workspace ... { protected SaveManager saveManager; public IStatus open(IProgressMonitor monitor) throws CoreException { // 開啟工作空間 // 最終註冊一個新的字串緩衝池分區 InternalPlatform.getDefault().addStringPoolParticipant(saveManager, getRoot()); return Status.OK_STATUS; }
|