Eclipse的字符串分区共享优化机制

http://tech.ddvip.com   2006年11月20日    社区交流

本文详细介绍Eclipse的字符串分区共享优化机制

  代码:

//
// 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 提供的是全局性的基于哈希表的共享支持。这样的实现虽然简单,并能够在最大限度上进行字符串共享;但同时也存在共享粒度太大,优化效果无法度量,大量字符串可能导致全局字符串表性能降低等问题。

来源:JAVAEYE    责编:豆豆技术应用

正在加载评论...