--- old/src/share/vm/runtime/arguments.cpp 2016-11-30 16:56:29.675299822 +0000 +++ new/src/share/vm/runtime/arguments.cpp 2016-11-30 16:56:29.567301583 +0000 @@ -2034,10 +2034,22 @@ static const size_t DefaultHeapBaseMinAddress = HeapBaseMinAddress; void Arguments::set_heap_size() { - const julong phys_mem = + julong phys_mem = FLAG_IS_DEFAULT(MaxRAM) ? MIN2(os::physical_memory(), (julong)MaxRAM) : (julong)MaxRAM; + if (UseCGroupLimits) { + FILE *fp = fopen("/sys/fs/cgroup/memory/memory.limit_in_bytes", "r"); + if (fp != NULL) { + julong cgroup_max; + fscanf(fp, "%llu", &cgroup_max); + tty->print("Setting phys_mem to the min of Cgroup %llu and phys_mem %llu\n", + cgroup_max, phys_mem); + phys_mem = MIN2(cgroup_max, phys_mem); + fclose(fp); + } + } + // 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.