--- old/src/os/bsd/vm/os_bsd.cpp 2013-03-15 21:54:25.555485181 +0100 +++ new/src/os/bsd/vm/os_bsd.cpp 2013-03-15 21:54:25.443485186 +0100 @@ -166,20 +166,6 @@ return Bsd::physical_memory(); } -julong os::allocatable_physical_memory(julong size) { -#ifdef _LP64 - return size; -#else - julong result = MIN2(size, (julong)3800*M); - if (!is_allocatable(result)) { - // See comments under solaris for alignment considerations - julong reasonable_size = (julong)2*G - 2 * os::vm_page_size(); - result = MIN2(size, reasonable_size); - } - return result; -#endif // _LP64 -} - //////////////////////////////////////////////////////////////////////////////// // environment support --- old/src/os/linux/vm/os_linux.cpp 2013-03-15 21:54:26.247485148 +0100 +++ new/src/os/linux/vm/os_linux.cpp 2013-03-15 21:54:26.131485153 +0100 @@ -191,20 +191,6 @@ return Linux::physical_memory(); } -julong os::allocatable_physical_memory(julong size) { -#ifdef _LP64 - return size; -#else - julong result = MIN2(size, (julong)3800*M); - if (!is_allocatable(result)) { - // See comments under solaris for alignment considerations - julong reasonable_size = (julong)2*G - 2 * os::vm_page_size(); - result = MIN2(size, reasonable_size); - } - return result; -#endif // _LP64 -} - //////////////////////////////////////////////////////////////////////////////// // environment support @@ -1142,7 +1128,7 @@ struct rlimit rlim; getrlimit(RLIMIT_STACK, &rlim); stack_size = rlim.rlim_cur; - + // 6308388: a bug in ld.so will relocate its own .data section to the // lower end of primordial stack; reduce ulimit -s value a little bit // so we won't install guard page on ld.so's data section. --- old/src/os/posix/vm/os_posix.cpp 2013-03-15 21:54:26.971485113 +0100 +++ new/src/os/posix/vm/os_posix.cpp 2013-03-15 21:54:26.843485120 +0100 @@ -188,4 +188,68 @@ st->cr(); } +bool os::has_allocatable_memory_limit(julong* limit) { + struct rlimit rlim; + int getrlimit_res = getrlimit(RLIMIT_AS, &rlim); + // if there was an error when calling getrlimit, assume that there is no limitation + // on virtual memory. + bool result; + if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) { + result = false; + } else { + *limit = (julong)rlim.rlim_cur; + result = true; + } +#ifdef _LP64 + return result; +#else + // arbitrary virtual space limit for 32 bit Unices found by testing. If + // getrlimit above returned a limit, bound it with this limit. Otherwise + // directly use it. + const julong max_virtual_limit = (julong)3800*M; + if (result) { + *limit = MIN2(*limit, max_virtual_limit); + } else { + *limit = max_virtual_limit; + } + // bound by actually allocatable memory. The algorithm uses two bounds, an + // upper and a lower limit. The upper limit is the current highest amount of + // memory that could not be allocated, the lower limit is the current highest + // amount of memory that could be allocated. + // The algorithm iteratively refines the result by halving the difference + // between these limits, updating either the upper limit (if that value could + // not be allocated) or the lower limit (if the that value could be allocated) + // until the difference between these limits is "small". + + // the minimum amount of memory we care about allocating. + const julong min_allocation_size = M; + + julong upper_limit = *limit; + + // first check a few trivial cases + if (is_allocatable(upper_limit)) { + *limit = upper_limit; + } else if (upper_limit < min_allocation_size) { + *limit = upper_limit; + } else if (!is_allocatable(min_allocation_size)) { + // we found that not even min_allocation_size is allocatable. Return it + // anyway. There is no point to search for a better value any more. + *limit = min_allocation_size; + } else { + // perform the binary search. + julong lower_limit = min_allocation_size; + while ((upper_limit - lower_limit) > min_allocation_size) { + julong temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit; + temp_limit = align_size_down_(temp_limit, min_allocation_size); + if (is_allocatable(temp_limit)) { + lower_limit = temp_limit; + } else { + upper_limit = temp_limit; + } + } + *limit = lower_limit; + } + return true; +#endif +} --- old/src/os/solaris/vm/os_solaris.cpp 2013-03-15 21:54:27.547485086 +0100 +++ new/src/os/solaris/vm/os_solaris.cpp 2013-03-15 21:54:27.423485092 +0100 @@ -476,24 +476,6 @@ return Solaris::physical_memory(); } -julong os::allocatable_physical_memory(julong size) { -#ifdef _LP64 - return size; -#else - julong result = MIN2(size, (julong)3835*M); - if (!is_allocatable(result)) { - // Memory allocations will be aligned but the alignment - // is not known at this point. Alignments will - // be at most to LargePageSizeInBytes. Protect - // allocations from alignments up to illegal - // values. If at this point 2G is illegal. - julong reasonable_size = (julong)2*G - 2 * LargePageSizeInBytes; - result = MIN2(size, reasonable_size); - } - return result; -#endif -} - static hrtime_t first_hrtime = 0; static const hrtime_t hrtime_hz = 1000*1000*1000; const int LOCK_BUSY = 1; --- old/src/os/windows/vm/os_windows.cpp 2013-03-15 21:54:28.299485050 +0100 +++ new/src/os/windows/vm/os_windows.cpp 2013-03-15 21:54:28.171485056 +0100 @@ -685,12 +685,17 @@ return win32::physical_memory(); } -julong os::allocatable_physical_memory(julong size) { +bool os::has_allocatable_memory_limit(julong* limit) { + MEMORYSTATUSEX ms; + ms.dwLength = sizeof(ms); + GlobalMemoryStatusEx(&ms); #ifdef _LP64 - return size; + *limit = (julong)ms.ullAvailVirtual; + return true; #else // Limit to 1400m because of the 2gb address space wall - return MIN2(size, (julong)1400*M); + *limit = MIN2((julong)1400*M, (julong)ms.ullAvailVirtual); + return true; #endif } --- old/src/share/vm/runtime/arguments.cpp 2013-03-15 21:54:29.067485014 +0100 +++ new/src/share/vm/runtime/arguments.cpp 2013-03-15 21:54:28.927485020 +0100 @@ -1546,6 +1546,15 @@ } } +julong Arguments::limit_by_allocatable_memory(julong limit) { + julong max_allocatable; + julong result = limit; + if (os::has_allocatable_memory_limit(&max_allocatable)) { + result = MIN2(result, max_allocatable / MaxVirtMemFraction); + } + return result; +} + void Arguments::set_heap_size() { if (!FLAG_IS_DEFAULT(DefaultMaxRAMFraction)) { // Deprecated flag @@ -1561,7 +1570,7 @@ // 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; @@ -1584,12 +1593,12 @@ } reasonable_max = MIN2(reasonable_max, max_coop_heap); } - reasonable_max = os::allocatable_physical_memory(reasonable_max); + reasonable_max = limit_by_allocatable_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 + // after call to limit_by_allocatable_memory because that // method might reduce the allocation size. reasonable_max = MAX2(reasonable_max, (julong)InitialHeapSize); } @@ -1609,14 +1618,14 @@ reasonable_minimum = MIN2(reasonable_minimum, (julong)MaxHeapSize); - reasonable_minimum = os::allocatable_physical_memory(reasonable_minimum); + reasonable_minimum = limit_by_allocatable_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); + reasonable_initial = limit_by_allocatable_memory(reasonable_initial); if (PrintGCDetails && Verbose) { // Cannot use gclog_or_tty yet. @@ -2595,9 +2604,7 @@ initHeapSize = MIN2(total_memory / (julong)2, total_memory - (julong)160*M); - // Make sure that if we have a lot of memory we cap the 32 bit - // process space. The 64bit VM version of this function is a nop. - initHeapSize = os::allocatable_physical_memory(initHeapSize); + initHeapSize = limit_by_allocatable_memory(initHeapSize); if (FLAG_IS_DEFAULT(MaxHeapSize)) { FLAG_SET_CMDLINE(uintx, MaxHeapSize, initHeapSize); --- old/src/share/vm/runtime/arguments.hpp 2013-03-15 21:54:29.787484980 +0100 +++ new/src/share/vm/runtime/arguments.hpp 2013-03-15 21:54:29.655484986 +0100 @@ -311,6 +311,9 @@ // GC ergonomics static void set_ergonomics_flags(); static void set_shared_spaces_flags(); + // limits the given memory size by the maximum amount of memory this process is + // currently allowed to allocate or reserve. + static julong limit_by_allocatable_memory(julong size); // Setup heap size static void set_heap_size(); // Based on automatic selection criteria, should the --- old/src/share/vm/runtime/globals.hpp 2013-03-15 21:54:30.399484950 +0100 +++ new/src/share/vm/runtime/globals.hpp 2013-03-15 21:54:30.271484956 +0100 @@ -1958,6 +1958,10 @@ product(uintx, InitialRAMFraction, 64, \ "Fraction (1/n) of real memory used for initial heap size") \ \ + develop(uintx, MaxVirtMemFraction, 2, \ + "Maximum fraction (1/n) of virtual memory used for ergonomically" \ + "determining maximum heap size") \ + \ product(bool, UseAutoGCSelectPolicy, false, \ "Use automatic collection selection policy") \ \ --- old/src/share/vm/runtime/os.hpp 2013-03-15 21:54:31.167484914 +0100 +++ new/src/share/vm/runtime/os.hpp 2013-03-15 21:54:31.043484920 +0100 @@ -184,7 +184,7 @@ } static julong available_memory(); static julong physical_memory(); - static julong allocatable_physical_memory(julong size); + static bool has_allocatable_memory_limit(julong* limit); static bool is_server_class_machine(); // number of CPUs