Skip to content

Commit b239207

Browse files
committed
Write a memory test
1 parent 3cd5329 commit b239207

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
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",

‎test/memory.test.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
})

0 commit comments

Comments
 (0)