Skip to content

Instantly share code, notes, and snippets.

@fcoury
Created July 2, 2025 15:47
Show Gist options
  • Save fcoury/167e608bf6c352865b0d516862963078 to your computer and use it in GitHub Desktop.
Save fcoury/167e608bf6c352865b0d516862963078 to your computer and use it in GitHub Desktop.

Here's JavaScript code that will definitely trigger OOM in your deno_core setup:

Memory Bomb - Large Array Allocation

// This will quickly consume memory by creating large arrays
const arrays = [];
let counter = 0;

while (true) {
    // Create 10MB arrays filled with random data
    const largeArray = new Array(1024 * 1024 * 10).fill(0).map(() => Math.random());
    arrays.push(largeArray);
    
    counter++;
    console.log(`Created array ${counter}, total arrays: ${arrays.length}`);
    
    // This will hit your heap limit very quickly
    if (counter > 200) break; // Safety break (won't be reached due to OOM)
}

return "This won't be returned due to OOM";

String Concatenation Bomb

// Exponential string growth
let str = "a";
let iteration = 0;

while (true) {
    str = str + str; // Doubles the string size each iteration
    iteration++;
    console.log(`Iteration ${iteration}, string length: ${str.length}`);
    
    // Will OOM before reaching iteration 30
    if (iteration > 30) break;
}

return str.length;

Object Creation Bomb

// Create millions of objects
const objects = [];
let count = 0;

while (true) {
    // Create objects with random properties
    const obj = {
        id: count,
        data: new Array(1000).fill(0).map(() => ({
            value: Math.random(),
            timestamp: Date.now(),
            nested: {
                more: "data".repeat(100),
                array: new Array(100).fill(Math.random())
            }
        }))
    };
    
    objects.push(obj);
    count++;
    
    if (count % 1000 === 0) {
        console.log(`Created ${count} objects`);
    }
    
    // Safety break (won't be reached)
    if (count > 1000000) break;
}

return objects.length;

Buffer/ArrayBuffer Bomb

// Create large ArrayBuffers
const buffers = [];
let totalSize = 0;

while (true) {
    // Create 50MB buffer
    const size = 50 * 1024 * 1024;
    const buffer = new ArrayBuffer(size);
    const view = new Uint8Array(buffer);
    
    // Fill with data to ensure allocation
    for (let i = 0; i < view.length; i += 1024) {
        view[i] = Math.floor(Math.random() * 256);
    }
    
    buffers.push(buffer);
    totalSize += size;
    
    console.log(`Total allocated: ${Math.round(totalSize / 1024 / 1024)} MB`);
    
    // This will definitely OOM with your limits
    if (totalSize > 2 * 1024 * 1024 * 1024) break; // 2GB safety
}

return totalSize;

Expected Behavior

With your current settings:

  • MAX_HEAP_SIZE: 50 MBHEAP_SIZE_INCREASE_LIMIT: 512 MB

Any of these scripts will:

  1. Trigger near_heap_limit_callback multiple times
  2. Log warnings about reaching heap limits
  3. Eventually call isolate.terminate_execution()
  4. Trigger the oom_handler
  5. Return a JavaScript execution error

Testing Recommendation

Start with the Array Allocation example as it's the most predictable and will hit your limits fastest. You should see your tracing logs showing heap limit warnings before the OOM occurs.

The exact point of failure will depend on your current heap settings, but with 50MB initial limit growing to 512MB max, these scripts will definitely cause OOM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment