[memory]: test memory leaks in canvas operations
Technical Documentation: Memory Usage in Canvas operations (sp-color-area)
Overview
The ColorArea component is a memory-intensive component due to its canvas-based operations and color manipulation functionality. This document explains why certain memory usage is expected and cannot be fully eliminated, as well as why the current memory testing approach is appropriate.
Memory Usage Analysis
Expected Memory Footprint
The ColorArea component has a baseline memory footprint of approximately 12KB, which is primarily composed of:
-
Component Structure: ~2-3KB
- Class definition, properties, and methods
- Reactive controllers (
ColorController,LanguageResolutionController)
-
Canvas Operations: ~4-5KB
- Gradient pattern definitions
- Color transformation calculations
- HSL/HSV color model conversions
-
Event Handling: ~2-3KB
- Pointer event listeners
- Keyboard navigation handlers
- Streaming listener infrastructure
-
DOM References: ~1-2KB
- References to input elements
- Bounding client rect calculations
- ResizeObserver setup
Why Canvas Operations Are Memory-Intensive?
Canvas-based rendering requires additional memory because:
- Gradient Patterns: These define the color space and are stored in memory.
- Color Calculations: Every color change involves calculations across multiple color models.
- Event Processing: Pointer events update color selection dynamically.
- Resize Handling: The
ResizeObserverensures proper layout adjustments.
Memory Testing Approach used in SWC
Our testForMemoryLeaks function focuses on detecting memory leaks, not enforcing a strict memory limit. It:
- Creates and destroys the component 50 times.
- Measures heap usage before and after.
- Ensures memory usage stays stable.
Why Canvas Operations like sp-color-area Are Excluded from Memory Testing?
Canvas-based components naturally use more memory. 12KB is reasonable given that ColorArea performs:
- Visual rendering (gradients, colors)
- User interactions (event handling, state updates)
- Complex calculations (color conversions)
For context:
- A simple button: 1-2KB
- A form component: 5-10KB
- A data grid with filtering: 20-50KB
Is 12KB a Problem?
12KB of memory usage is not necessarily a problem, especially for a component that:
- Performs complex visual operations
- Handles user interactions
- Manages state and color transformations
For comparison:
- A simple HTML button might use 1-2KB
- A complex form component might use 5-10KB
- A data grid with sorting and filtering might use 20-50KB
Conclusion
The ColorArea component’s memory footprint is expected due to its complexity. While it cannot be fully reduced, our memory testing approach effectively prevents leaks by ensuring stable memory usage over time. Proper cleanup in disconnectedCallback guarantees that resources are released, maintaining performance and reliability.
⚠️ No Changeset found
Latest commit: 6f94784e1ab3f026dda7a849adde1bb6da597d80
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Branch preview
Review the following VRT differences
When a visual regression test fails (or has previously failed while working on this branch), its results can be found in the following URLs:
- Spectrum | Light | Medium | LTR
- Spectrum | Dark | Large | RTL
- Express | Light | Medium | LTR
- Express | Dark | Large | RTL
- Spectrum-two | Light | Medium | LTR
- Spectrum-two | Dark | Large | RTL
- High Contrast Mode | Medium | LTR
If the changes are expected, update the current_golden_images_cache hash in the circleci config to accept the new images. Instructions are included in that file.
If the changes are unexpected, you can investigate the cause of the differences and update the code accordingly.
Tachometer results
Currently, no packages are changed by this PR...
closed to be tackled as part of new initiative