Repository navigation
Make -Xmx cap the whole heap under Node #50
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,6 +161,7 @@ export class JObject { | |
| constructor(cls: JClass, f: any[]) { | ||
| this.cls = cls; | ||
| this.f = f; | ||
| if (allocationCheck?.enabled) allocated(32); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -182,6 +183,7 @@ export class JArray extends JObject { | |
| constructor(cls: JClass, a: ArrayData) { | ||
| super(cls, EMPTY); | ||
| this.a = a; | ||
| if (allocationCheck?.enabled) allocated(ArrayBuffer.isView(a) ? a.byteLength : a.length * 8); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -372,11 +374,36 @@ export function primName(c: string) { | |
| throw new Error(`not a primitive: ${c}`); | ||
| } | ||
|
|
||
| // Reported by Runtime.maxMemory; no array may be bigger. -Xmx lowers it; the | ||
| // total of live objects is not counted. | ||
| // Reported by Runtime.maxMemory and the per-array limit. Node adds a sampled | ||
| // live-heap check when -Xmx is set; browser allocations keep the per-array cap. | ||
| export let MAX_HEAP = 1 << 30; | ||
| let explicitMaxHeap = false; | ||
|
project516-review-bot[bot] marked this conversation as resolved.
Resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| export function setMaxHeap(bytes: number) { | ||
| MAX_HEAP = Math.min(bytes, 1 << 30); | ||
| explicitMaxHeap = true; | ||
| } | ||
| export function hasMaxHeap() { return explicitMaxHeap; } | ||
| let allocationCheck: { cap: number; step: number; allocated: number; baseline: number; enabled: boolean; usage: () => number; gc?: () => void } | null = null; | ||
| export function setAllocationCheck(cap: number, usage: () => number, gc?: () => void) { | ||
| allocationCheck = { cap, step: Math.min(cap, Math.max(4 << 20, cap / 16)), allocated: 0, baseline: 0, enabled: false, usage, gc }; | ||
| } | ||
| export function clearAllocationCheck() { allocationCheck = null; } | ||
| export function resetAllocationCheck() { | ||
| if (!allocationCheck) return; | ||
| allocationCheck.baseline = allocationCheck.usage(); | ||
| allocationCheck.allocated = 0; | ||
| allocationCheck.enabled = true; | ||
| } | ||
| function allocated(bytes: number) { | ||
| const check = allocationCheck; | ||
| if (!check?.enabled || bytes <= 0) return; | ||
| check.allocated += bytes; | ||
| if (check.gc ? check.allocated < check.step : check.allocated <= check.cap) return; | ||
|
project516-review-bot[bot] marked this conversation as resolved.
Resolved
|
||
| check.allocated = 0; | ||
| if (check.usage() - check.baseline > check.cap) { | ||
| check.gc?.(); | ||
| if (check.usage() - check.baseline > check.cap) throw new PendingThrow("java/lang/OutOfMemoryError", "Java heap space"); | ||
| } | ||
| } | ||
| const ELEM_SIZE: Record<string, number> = { Z: 1, B: 1, C: 2, S: 2, I: 4, F: 4, J: 8, D: 8, L: 4 }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // java: -Xmx32m | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class HeapCap { | ||
| public static void main(String[] args) { | ||
| List<byte[]> keep = new ArrayList<>(); | ||
| try { | ||
| while (true) keep.add(new byte[256 * 1024]); | ||
| } catch (OutOfMemoryError e) { | ||
| System.out.println("OutOfMemoryError: " + e.getMessage()); | ||
| keep.clear(); | ||
| System.gc(); | ||
| } | ||
| for (int i = 0; i < 16; i++) keep.add(new byte[256 * 1024]); | ||
| System.out.println("continued"); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
hasMaxHeap()relies on the one-wayexplicitMaxHeapflag, a VM started without-Xmxafter one with-Xmxincorrectly enters this branch and installs an allocation check using the staleMAX_HEAP. Theelse clearAllocationCheck()branch is never reached for subsequent VMs. This should be fixed together with theexplicitMaxHeapreset.