jmap -histo可以触发完整的垃圾回收吗?

kul*_*lan 3 garbage-collection jmap

我们知道jmap -histo:live会触发一个完整的gc来确定活动对象:

当使用live选项时,jmap是否强制执行垃圾收集?

由于jmap -histo考虑堆中的所有对象(年轻和老一代中的对象),我的观点是,jmap -histo也可以触发完整的gc.但是,我无法找到关于jmap -histo是否可以触发完整gc的可靠文档.

jmap -histo可以触发完整的垃圾回收吗?

Chr*_*her 6

具有更多JDK经验的人应该验证这一点,但我相信它确实触发了完整的GC,至少在OpenJDK 1.7中.开始于jdk/src/share/classes/sun/tools/jmap/JMap.java:

public class JMap {
    ...
    private static String LIVE_HISTO_OPTION = "-histo:live";
    ...        
    ...
      } else if (option.equals(LIVE_HISTO_OPTION)) {
            histo(pid, true);
    ...
    private static final String LIVE_OBJECTS_OPTION = "-live";
    private static final String ALL_OBJECTS_OPTION = "-all";
    private static void histo(String pid, boolean live) throws IOException {
        VirtualMachine vm = attach(pid);
        InputStream in = ((HotSpotVirtualMachine)vm).
            heapHisto(live ? LIVE_OBJECTS_OPTION : ALL_OBJECTS_OPTION);
        drain(vm, in);
    }
Run Code Online (Sandbox Code Playgroud)

三元运算符使用参数Jmap.histo()调用heapHisto :jdk/src/share/classes/sun/tools/attach/HotSpotVirtualMachine.java-live

// Heap histogram (heap inspection in HotSpot)
public InputStream heapHisto(Object ... args) throws IOException {
    return executeCommand("inspectheap", args);
}
Run Code Online (Sandbox Code Playgroud)

如果我们看看inspectheap自己,在hotspot/src/share/vm/services/attachListener.cpp:

// Implementation of "inspectheap" command
//
// Input arguments :-
//   arg0: "-live" or "-all"
static jint heap_inspection(AttachOperation* op, outputStream* out) {
  bool live_objects_only = true;   // default is true to retain the behavior before this change is made
  const char* arg0 = op->arg(0);
  if (arg0 != NULL && (strlen(arg0) > 0)) {
    if (strcmp(arg0, "-all") != 0 && strcmp(arg0, "-live") != 0) {
      out->print_cr("Invalid argument to inspectheap operation: %s", arg0);
      return JNI_ERR;
    }
    live_objects_only = strcmp(arg0, "-live") == 0;
  }
  VM_GC_HeapInspection heapop(out, live_objects_only /* request full gc */, true /* need_prologue */);
  VMThread::execute(&heapop);
  return JNI_OK;
}
Run Code Online (Sandbox Code Playgroud)

注意,特别是live_objects_onlystrcmp和heapop后来调用两行.如果通过任何途径inspectheap获得-live参数,它会请求一个完整的gc.


Kin*_*ung 5

jmap -histo不会引发全面的GC,但jmap -histo:live意志。