Skip to content

Commit 7baec66

Browse files
committed
8328168: Epsilon: Premature OOM when allocating object larger than uncommitted heap size
Reviewed-by: shade, tschatzl
1 parent c342188 commit 7baec66

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/hotspot/share/gc/epsilon/epsilonHeap.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,21 @@ HeapWord* EpsilonHeap::allocate_work(size_t size, bool verbose) {
125125

126126
// Expand and loop back if space is available
127127
size_t size_in_bytes = size * HeapWordSize;
128-
size_t space_left = max_capacity() - capacity();
128+
size_t uncommitted_space = max_capacity() - capacity();
129+
size_t unused_space = max_capacity() - used();
129130
size_t want_space = MAX2(size_in_bytes, EpsilonMinHeapExpand);
131+
assert(unused_space >= uncommitted_space,
132+
"Unused (" SIZE_FORMAT ") >= uncommitted (" SIZE_FORMAT ")",
133+
unused_space, uncommitted_space);
130134

131-
if (want_space < space_left) {
135+
if (want_space < uncommitted_space) {
132136
// Enough space to expand in bulk:
133137
bool expand = _virtual_space.expand_by(want_space);
134138
assert(expand, "Should be able to expand");
135-
} else if (size_in_bytes < space_left) {
139+
} else if (size_in_bytes < unused_space) {
136140
// No space to expand in bulk, and this allocation is still possible,
137141
// take all the remaining space:
138-
bool expand = _virtual_space.expand_by(space_left);
142+
bool expand = _virtual_space.expand_by(uncommitted_space);
139143
assert(expand, "Should be able to expand");
140144
} else {
141145
// No space left:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
package gc.epsilon;
26+
27+
/**
28+
* @test TestEnoughUnusedSpace
29+
* @requires vm.gc.Epsilon
30+
* @summary Epsilon should allocates object successfully if it has enough space.
31+
* @run main/othervm -Xms64M -Xmx128M -XX:+UnlockExperimentalVMOptions
32+
* -XX:+UseEpsilonGC gc.epsilon.TestEnoughUnusedSpace
33+
*/
34+
35+
public class TestEnoughUnusedSpace {
36+
static volatile Object arr;
37+
38+
public static void main(String[] args) {
39+
// Create an array about 90M. It should be created successfully
40+
// instead of throwing OOME, because 90M is smaller than 128M.
41+
arr = new byte[90 * 1024 * 1024];
42+
}
43+
}

0 commit comments

Comments
 (0)