--- old/src/hotspot/share/memory/metaspace.cpp 2019-01-30 22:09:09.816658926 +0900 +++ new/src/hotspot/share/memory/metaspace.cpp 2019-01-30 22:09:09.265657188 +0900 @@ -149,6 +149,12 @@ return false; } + // Check after the increment that we did not go over the maximum. + // We can not do this earlier due to potential races. + assert(new_value <= MaxMetaspaceSize, + "new_value: " SIZE_FORMAT " > MaxMetaspaceSize: " SIZE_FORMAT, + new_value, MaxMetaspaceSize); + if (new_cap_until_GC != NULL) { *new_cap_until_GC = new_value; } @@ -236,7 +242,7 @@ const double min_tmp = used_after_gc / maximum_used_percentage; size_t minimum_desired_capacity = - (size_t)MIN2(min_tmp, double(max_uintx)); + (size_t)MIN2(min_tmp, double(MaxMetaspaceSize)); // Don't shrink less than the initial generation size minimum_desired_capacity = MAX2(minimum_desired_capacity, MetaspaceSize); @@ -283,7 +289,7 @@ const double maximum_free_percentage = MaxMetaspaceFreeRatio / 100.0; const double minimum_used_percentage = 1.0 - maximum_free_percentage; const double max_tmp = used_after_gc / minimum_used_percentage; - size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx)); + size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(MaxMetaspaceSize)); maximum_desired_capacity = MAX2(maximum_desired_capacity, MetaspaceSize); log_trace(gc, metaspace)(" maximum_free_percentage: %6.2f minimum_used_percentage: %6.2f", @@ -1477,6 +1483,9 @@ // the HWM, an allocation is still attempted. This is because another thread must then // have incremented the HWM and therefore the allocation might still succeed. do { + if (MetaspaceGC::capacity_until_GC() + delta_bytes > MaxMetaspaceSize) { + return NULL; + } incremented = MetaspaceGC::inc_capacity_until_GC(delta_bytes, &after, &before); res = allocate(word_size, mdtype); } while (!incremented && res == NULL); --- old/test/hotspot/jtreg/vmTestbase/metaspace/shrink_grow/ShrinkGrowTest/ShrinkGrowTest.java 2019-01-30 22:09:10.923662416 +0900 +++ new/test/hotspot/jtreg/vmTestbase/metaspace/shrink_grow/ShrinkGrowTest/ShrinkGrowTest.java 2019-01-30 22:09:10.449660922 +0900 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2019, 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 @@ -25,6 +25,7 @@ /* * @test * + * @bug 8217432 * @summary converted from VM Testbase metaspace/shrink_grow/ShrinkGrowTest. * * @requires vm.opt.final.ClassUnloading