Skip to content

[GR-65896] Dynamic Heap Size Manager for Serial GC #11423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ protected SizeParameters computeSizeParameters(SizeParameters existing) {
UnsignedWord heapSizeLimit = UnsignedUtils.max(alignDown(getHeapSizeLimit()), minAllSpaces);

UnsignedWord maxHeap;
long optionMax = SubstrateGCOptions.MaxHeapSize.getValue();
long optionMax = getMaximumHeapSizeOptionValue();
if (optionMax > 0L) {
maxHeap = Word.unsigned(optionMax);
} else {
Expand Down Expand Up @@ -418,6 +418,10 @@ protected UnsignedWord getYoungSizeLimit(UnsignedWord maxHeap) {
return maxHeap.subtract(minSpaceSize());
}

protected long getMaximumHeapSizeOptionValue() {
return SubstrateGCOptions.MaxHeapSize.getValue();
}

protected UnsignedWord getInitialHeapSize() {
return AbstractCollectionPolicy.INITIAL_HEAP_SIZE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ static Class<? extends CollectionPolicy> getPolicyClass(String name) {
return BasicCollectionPolicies.OnlyIncrementally.class;
case "NeverCollect":
return BasicCollectionPolicies.NeverCollect.class;
case "Dynamic":
return DynamicCollectionPolicy.class;
}
throw UserError.abort("Policy %s does not exist.", name);
}
Expand Down Expand Up @@ -210,4 +212,9 @@ static boolean shouldCollectYoungGenSeparately(boolean defaultValue) {

/** Called before the end of a collection, in the safepoint operation. */
void onCollectionEnd(boolean completeCollection, GCCause cause);

/** Can be overridden to recover from OOM. */
default boolean isOutOfMemory(UnsignedWord usedBytes) {
return usedBytes.aboveThan(getMaximumHeapSize());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2025, 2025, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
package com.oracle.svm.core.genscavenge;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.word.UnsignedWord;

import com.oracle.svm.core.SubstrateGCOptions;
import com.oracle.svm.core.heap.DynamicHeapSizeManager;

/**
* This class provides the implementation of a dynamic collection policy that calls into an instance
* of {@link DynamicHeapSizeManager} if it exists, otherwise the implementation is equivalent to
* {@link AdaptiveCollectionPolicy}.
*/
class DynamicCollectionPolicy extends AdaptiveCollectionPolicy {
@Override
public String getName() {
return "dynamic";
}

@Override
protected long getMaximumHeapSizeOptionValue() {
if (ImageSingletons.contains(DynamicHeapSizeManager.class)) {
return ImageSingletons.lookup(DynamicHeapSizeManager.class).maxHeapSize().rawValue();
}

return SubstrateGCOptions.MaxHeapSize.getValue();
}

@Override
public boolean isOutOfMemory(UnsignedWord usedBytes) {
if (ImageSingletons.contains(DynamicHeapSizeManager.class)) {
return ImageSingletons.lookup(DynamicHeapSizeManager.class).outOfMemory(usedBytes);
}

return super.isOutOfMemory(usedBytes);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2025, 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
Expand Down Expand Up @@ -169,6 +169,9 @@ public void maybeCollectOnAllocation(UnsignedWord allocationSize) {
} else if (getPolicy().shouldCollectOnAllocation()) {
AllocationRequiringGCEvent.emit(getCollectionEpoch(), allocationSize);
outOfMemory = collectWithoutAllocating(GenScavengeGCCause.OnAllocation, false);
if (outOfMemory) {
outOfMemory = getPolicy().isOutOfMemory(HeapImpl.getAccounting().getUsedBytes());
}
}
if (outOfMemory) {
throw OutOfMemoryUtil.heapSizeExceeded();
Expand All @@ -186,7 +189,11 @@ private void collect(GCCause cause, boolean forceFullGC) {
if (!hasNeverCollectPolicy()) {
boolean outOfMemory = collectWithoutAllocating(cause, forceFullGC);
if (outOfMemory) {
throw OutOfMemoryUtil.heapSizeExceeded();
if (getPolicy().isOutOfMemory(HeapImpl.getAccounting().getUsedBytes())) {
throw OutOfMemoryUtil.heapSizeExceeded();
} else {
GCImpl.getPolicy().updateSizeParameters();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public static Object slowPathNewArrayLikeObject(Word objectHeader, int length, b
* object is allocated and survives.
*/
GCImpl.getPolicy().ensureSizeParametersInitialized();
if (size.aboveOrEqual(GCImpl.getPolicy().getMaximumHeapSize()) && !GCImpl.shouldIgnoreOutOfMemory()) {
if (GCImpl.getPolicy().isOutOfMemory(size) && !GCImpl.shouldIgnoreOutOfMemory()) {
OutOfMemoryError outOfMemoryError = new OutOfMemoryError("Array allocation too large.");
throw OutOfMemoryUtil.reportOutOfMemoryError(outOfMemoryError);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2025, 2025, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
package com.oracle.svm.core.heap;

import static com.oracle.svm.core.heap.RestrictHeapAccess.Access.NO_ALLOCATION;

import org.graalvm.word.UnsignedWord;

import com.oracle.svm.core.SubstrateGCOptions;

/**
* This interface defines the functions needed to implement a dynamic heap size manager, where the
* Java heap size can be changed at runtime.
*/
public interface DynamicHeapSizeManager {
/**
* Retrieves the current max heap size, overrides {@link SubstrateGCOptions#MaxHeapSize}.
*
* @return max heap size
*/
@RestrictHeapAccess(access = NO_ALLOCATION, reason = "Called from allocation-free code paths.")
UnsignedWord maxHeapSize();

/**
* In case of OOM, try to request additional memory.
*
* @param bytes memory required
* @return true if out of memory, false if max heap size was increased
*/
@RestrictHeapAccess(access = NO_ALLOCATION, reason = "Called from allocation-free code paths.")
boolean outOfMemory(UnsignedWord bytes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,16 @@ private class HeapDumpOperation extends NativeVMOperation {
}

@Override
@RestrictHeapAccess(access = NO_ALLOCATION, reason = "Heap dumping must not allocate.")
protected void operate(NativeVMOperationData d) {
HeapDumpVMOperationData data = (HeapDumpVMOperationData) d;
if (data.getGCBefore()) {
Heap.getHeap().getGC().collectCompletely(GCCause.HeapDump);
}
dumpHeap(data);
}

@RestrictHeapAccess(access = NO_ALLOCATION, reason = "Heap dumping must not allocate.")
private void dumpHeap(HeapDumpVMOperationData data) {
try {
boolean success = writer.dumpHeap(data.getRawFileDescriptor());
data.setSuccess(success);
Expand Down