Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java7如何决定OSX上分配的堆内存的最大值(-Xmx)_Java_Macos_Java 7 - Fatal编程技术网

Java7如何决定OSX上分配的堆内存的最大值(-Xmx)

Java7如何决定OSX上分配的堆内存的最大值(-Xmx),java,macos,java-7,Java,Macos,Java 7,Java7如何决定分配的堆内存的最大值(-Xmx)如果OSX包中没有指定,我已经阅读了手册页面,但没有给出任何指示。它似乎比Java 6上的默认值分配得多,我想知道它是否随机器上可用内存的不同而变化,这对我来说非常有用,因为我的应用程序是内存受限的,但我不能将默认值设置得太高,因为这样应用程序将无法在低规格机器上运行 来自openjdk源代码的默认堆大小hotspot/src/share/vm/runtime/arguments.cpp 从源代码复制注释 // If the maximum

Java7如何决定分配的堆内存的最大值(-Xmx)如果OSX包中没有指定,我已经阅读了手册页面,但没有给出任何指示。它似乎比Java 6上的默认值分配得多,我想知道它是否随机器上可用内存的不同而变化,这对我来说非常有用,因为我的应用程序是内存受限的,但我不能将默认值设置得太高,因为这样应用程序将无法在低规格机器上运行

来自openjdk源代码的默认堆大小
hotspot/src/share/vm/runtime/arguments.cpp

从源代码复制注释

  // If the maximum heap size has not been set with -Xmx,
  // then set it as fraction of the size of physical memory,
  // respecting the maximum and minimum sizes of the heap.
分数=4

  product(uintx, MaxRAMFraction, 4,                                         \
          "Maximum fraction (1/n) of real memory used for maximum heap "    \
          "size")                                                           \
                                                                            \
  product(uintx, DefaultMaxRAMFraction, 4,                                  \
          "Maximum fraction (1/n) of real memory used for maximum heap "    \
          "size; deprecated: to be renamed to MaxRAMFraction")              \
关于mx的完整代码

void Arguments::set_heap_size() {
  if (!FLAG_IS_DEFAULT(DefaultMaxRAMFraction)) {
    // Deprecated flag
    FLAG_SET_CMDLINE(uintx, MaxRAMFraction, DefaultMaxRAMFraction);
  }

  const julong phys_mem =
    FLAG_IS_DEFAULT(MaxRAM) ? MIN2(os::physical_memory(), (julong)MaxRAM)
                            : (julong)MaxRAM;

  // If the maximum heap size has not been set with -Xmx,
  // then set it as fraction of the size of physical memory,
  // respecting the maximum and minimum sizes of the heap.
  if (FLAG_IS_DEFAULT(MaxHeapSize)) {
    julong reasonable_max = phys_mem / MaxRAMFraction;

    if (phys_mem <= MaxHeapSize * MinRAMFraction) {
      // Small physical memory, so use a minimum fraction of it for the heap
      reasonable_max = phys_mem / MinRAMFraction;
    } else {
      // Not-small physical memory, so require a heap at least
      // as large as MaxHeapSize
      reasonable_max = MAX2(reasonable_max, (julong)MaxHeapSize);
    }
    if (!FLAG_IS_DEFAULT(ErgoHeapSizeLimit) && ErgoHeapSizeLimit != 0) {
      // Limit the heap size to ErgoHeapSizeLimit
      reasonable_max = MIN2(reasonable_max, (julong)ErgoHeapSizeLimit);
    }
    if (UseCompressedOops) {
      // Limit the heap size to the maximum possible when using compressed oops
      julong max_coop_heap = (julong)max_heap_for_compressed_oops();
      if (HeapBaseMinAddress + MaxHeapSize < max_coop_heap) {
        // Heap should be above HeapBaseMinAddress to get zero based compressed oops
        // but it should be not less than default MaxHeapSize.
        max_coop_heap -= HeapBaseMinAddress;
      }
      reasonable_max = MIN2(reasonable_max, max_coop_heap);
    }
    reasonable_max = os::allocatable_physical_memory(reasonable_max);

    if (!FLAG_IS_DEFAULT(InitialHeapSize)) {
      // An initial heap size was specified on the command line,
      // so be sure that the maximum size is consistent.  Done
      // after call to allocatable_physical_memory because that
      // method might reduce the allocation size.
      reasonable_max = MAX2(reasonable_max, (julong)InitialHeapSize);
    }

    if (PrintGCDetails && Verbose) {
      // Cannot use gclog_or_tty yet.
      tty->print_cr("  Maximum heap size " SIZE_FORMAT, reasonable_max);
    }
    FLAG_SET_ERGO(uintx, MaxHeapSize, (uintx)reasonable_max);
  }

  // If the initial_heap_size has not been set with InitialHeapSize
  // or -Xms, then set it as fraction of the size of physical memory,
  // respecting the maximum and minimum sizes of the heap.
  if (FLAG_IS_DEFAULT(InitialHeapSize)) {
    julong reasonable_minimum = (julong)(OldSize + NewSize);

    reasonable_minimum = MIN2(reasonable_minimum, (julong)MaxHeapSize);

    reasonable_minimum = os::allocatable_physical_memory(reasonable_minimum);

    julong reasonable_initial = phys_mem / InitialRAMFraction;

    reasonable_initial = MAX2(reasonable_initial, reasonable_minimum);
    reasonable_initial = MIN2(reasonable_initial, (julong)MaxHeapSize);

    reasonable_initial = os::allocatable_physical_memory(reasonable_initial);

    if (PrintGCDetails && Verbose) {
      // Cannot use gclog_or_tty yet.
      tty->print_cr("  Initial heap size " SIZE_FORMAT, (uintx)reasonable_initial);
      tty->print_cr("  Minimum heap size " SIZE_FORMAT, (uintx)reasonable_minimum);
    }
    FLAG_SET_ERGO(uintx, InitialHeapSize, (uintx)reasonable_initial);
    set_min_heap_size((uintx)reasonable_minimum);
  }
}
void参数::set\u heap\u size(){
如果(!FLAG_为默认值(DefaultMaxRAMFraction)){
//弃用旗
标志设置CMDLINE(uintx、MaxRAMFraction、DefaultMaxRAMFraction);
}
康斯特巨龙体育俱乐部=
标志是默认值(MaxRAM)?MIN2(操作系统::物理内存(),(巨龙)MaxRAM)
:(巨龙)MaxRAM;
//如果未使用-Xmx设置最大堆大小,
//然后将其设置为物理内存大小的分数,
//考虑堆的最大和最小大小。
如果(标志为默认值(最大化)){
巨龙合理最大值=物理最小值/最大分数;
if(phys_mem print_cr(“最大堆大小”大小格式,合理的最大值);
}
标志设置(uintx,MaxHeapSize,(uintx)合理的最大值);
}
//如果未使用InitialHeapSize设置初始堆大小
//或者-Xms,然后将其设置为物理内存大小的一部分,
//考虑堆的最大和最小大小。
如果(标志为默认值(初始化){
巨龙合理_最小值=(巨龙)(旧尺寸+新尺寸);
合理最小值=最小值2(合理最小值,(巨龙)最大值);
合理的最小值=os::可分配的物理内存(合理的最小值);
巨龙合理初始值=物理最小值/初始分数;
合理初始值=最大值2(合理初始值,合理最小值);
合理初始值=MIN2(合理初始值,(巨龙)最大值);
合理的初始值=os::可分配的物理内存(合理的初始值);
如果(打印详细信息和详细信息(&R)){
//还不能使用gclog\u或\u tty。
tty->打印\u cr(“初始堆大小”大小\u格式,(uintx)合理\u初始);
tty->打印\u cr(“最小堆大小”大小\u格式,(uintx)合理\u最小值);
}
标志设置(uintx,InitialHeapSize,(uintx)合理的初始值);
设置堆的最小值((uintx)合理的最小值);
}
}

System.out.println(Runtime.getRuntime().maxMemory());我知道如何获取值,但我不明白它是如何决定将值设置为什么的。多亏了这一点,尽管了解它实际使用的物理内存的比例会很好。这也是openjdk。Oracle版本实际上做了同样的事情,现在这是否也适用于Windows版本。@PaulTaylorcle和openjdk基本上占了物理内存的四分之一,如果有办法设置这个比例的话?