--- old/src/hotspot/share/memory/metaspace.cpp 2017-10-10 12:02:48.238338380 +0900 +++ new/src/hotspot/share/memory/metaspace.cpp 2017-10-10 12:02:47.765328363 +0900 @@ -3324,6 +3324,17 @@ CompressedClassSpaceSize = align_down_bounded(CompressedClassSpaceSize, _reserve_alignment); set_compressed_class_space_size(CompressedClassSpaceSize); + + size_t min_metaspace_sz = calculate_min_metaspace_size(); + if (!UseCompressedClassPointers && (min_metaspace_sz >= MaxMetaspaceSize)) { + FLAG_SET_ERGO(size_t, InitialBootClassLoaderMetaspaceSize, + min_metaspace_sz); + } + +} + +size_t Metaspace::calculate_min_metaspace_size() { + return InitialBootClassLoaderMetaspaceSize + (MediumChunk * HeapWordSize); } void Metaspace::global_initialize() { --- old/src/hotspot/share/memory/metaspace.hpp 2017-10-10 12:02:48.757349370 +0900 +++ new/src/hotspot/share/memory/metaspace.hpp 2017-10-10 12:02:48.563345262 +0900 @@ -182,6 +182,7 @@ #ifdef _LP64 static void allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base); #endif + static size_t calculate_min_metaspace_size(); private: --- old/src/hotspot/share/runtime/arguments.cpp 2017-10-10 12:02:49.195358645 +0900 +++ new/src/hotspot/share/runtime/arguments.cpp 2017-10-10 12:02:48.997354452 +0900 @@ -1737,6 +1737,16 @@ } // Check the CompressedClassSpaceSize to make sure we use compressed klass ptrs. if (UseCompressedClassPointers) { + size_t min_metaspace_sz = Metaspace::calculate_min_metaspace_size(); + if ((min_metaspace_sz + CompressedClassSpaceSize) > MaxMetaspaceSize) { + if (min_metaspace_sz >= MaxMetaspaceSize) { + vm_exit_during_initialization("MaxMetaspaceSize is too small."); + } else { + FLAG_SET_ERGO(size_t, CompressedClassSpaceSize, + MaxMetaspaceSize - min_metaspace_sz); + } + } + if (CompressedClassSpaceSize > KlassEncodingMetaspaceMax) { warning("CompressedClassSpaceSize is too large for UseCompressedClassPointers"); FLAG_SET_DEFAULT(UseCompressedClassPointers, false); --- /dev/null 2017-10-10 09:20:29.244234460 +0900 +++ new/test/hotspot/jtreg/runtime/Metaspace/MaxMetaspaceSizeTest.java 2017-10-10 12:02:49.449364024 +0900 @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import jdk.test.lib.process.ProcessTools; +import jdk.test.lib.process.OutputAnalyzer; + +/* + * @test MaxMetaspaceSizeTest + * @bug 8087291 + * @library /test/lib + * @run main/othervm MaxMetaspaceSizeTest + */ + +public class MaxMetaspaceSizeTest { + public static void main(String... args) throws Exception { + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( + true, + "-Xmx1g", + "-XX:InitialBootClassLoaderMetaspaceSize=4195328", + "-XX:MaxMetaspaceSize=4195328", + "-XX:+UseCompressedClassPointers", + "-XX:CompressedClassSpaceSize=1g", + "--version"); + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + output.shouldContain("MaxMetaspaceSize is too small."); + } +}