Skip to content
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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
!/tsconfig.json
!/tsconfig.src.json
!/entrypoint.sh
!/compute-heap.mjs

# Ignore unnecessary files inside allowed directories
# This should go after the allowed directories
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ COPY --from=builder --chown=docker:docker docs docs
COPY --from=builder node_modules node_modules
COPY --chown=docker:docker tsconfig.src.json tsconfig.json
COPY entrypoint.sh entrypoint.sh
COPY compute-heap.mjs compute-heap.mjs

RUN adduser -u 2004 -D docker &&\
chmod +x entrypoint.sh
Expand Down
21 changes: 21 additions & 0 deletions compute-heap.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os from "node:os";

const constrained =
typeof process.constrainedMemory === "function"
? process.constrainedMemory()
: 0;
const limitBytes = constrained || os.totalmem();
const limitMB = Math.floor(limitBytes / 1024 / 1024);

// Reserve 25% headroom for non-V8-heap usage (native buffers, stack, code space, RSS overhead).
const budgetMB = Math.floor(limitMB * 0.75);

// V8's new-space is TWO semi-spaces (from-space + to-space), so its worst-case
// footprint is 2x --max-semi-space-size. Size it as a fraction of the budget,
// clamped to V8's own sane range, then subtract it before sizing old-space so
// old-space + new-space never exceeds the budget.
const semiSpaceMB = Math.min(128, Math.max(16, Math.floor(budgetMB * 0.05)));
const newSpaceMB = semiSpaceMB * 2;
const oldSpaceMB = Math.max(1, budgetMB - newSpaceMB);
Comment on lines +17 to +19

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Ensure the configured V8 spaces remain within the 75% memory budget. Account for the actual young-generation footprint (including three semi-spaces where applicable), and handle budgets too small to satisfy the 16 MB minimum without exceeding the limit.


process.stdout.write(`${oldSpaceMB} ${semiSpaceMB}`);
8 changes: 6 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/bin/sh

read -r HEAP_MB SEMI_MB <<EOF
$(node /compute-heap.mjs)
EOF

exec node \
--max-old-space-size=4608 \
--max-semi-space-size=128 \
--max-old-space-size="${HEAP_MB}" \
--max-semi-space-size="${SEMI_MB}" \
/dist/src/index.js