File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2727 "prebenchmark" : " node-gyp build" ,
2828 "benchmark" : " node benchmarks/run.js" ,
2929 "lint" : " standard examples/*.js test/server.js test/public/*.js benchmarks/run.js lib/context2d.js util/has_lib.js browser.js index.js" ,
30- "test" : " mocha test/*.test.js" ,
30+ "test" : " mocha --node-option expose-gc test/*.test.js" ,
3131 "pretest-server" : " node-gyp build" ,
3232 "test-server" : " node test/server.js" ,
3333 "generate-wpt" : " node ./test/wpt/generate.js" ,
Original file line number Diff line number Diff line change 1+ /* eslint-env mocha */
2+
3+ 'use strict'
4+
5+ // These tests require --expose-gc. Skip gracefully if not available.
6+ const gcAvailable = typeof gc === 'function'
7+
8+ const assert = require ( 'assert' )
9+ const { createCanvas } = require ( '../' )
10+
11+ describe ( 'Memory management' , function ( ) {
12+ before ( function ( ) {
13+ if ( ! gcAvailable ) this . skip ( )
14+ } )
15+
16+ it ( 'Canvas objects are freed by GC' , function ( ) {
17+ const ITERATIONS = 100
18+ const SIZE = 1024 // 1024x1024 ARGB = 4 MiB per canvas
19+
20+ for ( let i = 0 ; i < ITERATIONS ; i ++ ) {
21+ const canvas = createCanvas ( SIZE , SIZE )
22+ const ctx = canvas . getContext ( '2d' )
23+ ctx . fillStyle = 'red'
24+ ctx . fillRect ( 0 , 0 , SIZE , SIZE )
25+ gc ( )
26+ }
27+
28+ gc ( )
29+ gc ( )
30+
31+ // 100 canvases x 4 MiB = 400 MiB if leaked.
32+ // With proper GC, RSS should stay well under 150 MiB.
33+ const rssMiB = process . memoryUsage ( ) . rss / 1024 / 1024
34+ assert ( rssMiB < 150 , `RSS is ${ rssMiB . toFixed ( 0 ) } MiB, expected < 150 MiB` )
35+ } )
36+ } )
You can’t perform that action at this time.
0 commit comments