Skip to content

[BUG] Memory Leak from Large Canvas Objects Not Being Garbage Collected #122

@Karelaking

Description

@Karelaking

Bug Description

During full-page screenshot capture, multiple large canvas objects are created and stored in the chunks array. These canvases are not explicitly cleaned up, leading to high memory usage and potential browser crashes on very large pages.

Steps To Reproduce

  1. Navigate to a very long webpage (10,000+ px height)
  2. Capture full page screenshot
  3. Monitor memory usage in browser DevTools
  4. Observe high memory consumption
  5. Multiple captures can crash the browser tab

Expected Behavior

  • Canvas objects should be cleaned up after use
  • Memory should be released between chunks
  • Browser should remain stable even for very large pages

Actual Behavior

  • Memory usage grows significantly during capture
  • Canvas objects remain in memory
  • Can cause browser tab crashes on large pages

Affected Files

  • src/js/content.js (Lines 476-615)
// Line 476-555: chunks array accumulates large objects
const chunks = [];
for (let chunkIndex = 0; chunkIndex < numChunks; chunkIndex++) {
  const chunkCanvas = document.createElement('canvas');
  // ... canvas grows to thousands of pixels
  chunks.push(chunkCanvas); // No cleanup between pushes
}

Environment

  • OS: All platforms
  • Browser: Chrome, Firefox, Edge
  • Version: 1.2.0
  • Particularly affects: Large pages (>10,000px height)

Possible Solution

Explicitly cleanup resources and transfer data to blobs:

const chunks = [];
for (let chunkIndex = 0; chunkIndex < numChunks; chunkIndex++) {
  const chunkCanvas = document.createElement('canvas');
  // ... capture logic
  
  // Convert to blob instead of keeping canvas
  const blob = await new Promise(resolve => 
    chunkCanvas.toBlob(resolve, 'image/png')
  );
  chunks.push(blob);
  
  // Explicitly cleanup canvas
  chunkCanvas.width = 0;
  chunkCanvas.height = 0;
  
  // Force garbage collection opportunity
  if (chunkIndex % 3 === 0) {
    await sleep(200);
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions