Automattic / Automattic/node-canvas
All memory allocated by context not being freed
- Dominant language
- JavaScript
- Stars
- 10.7k
- Forks
- 1.2k
- Avg merge
- 4d 8h
- Merged PRs (30d)
- 1
Description
## Issue or Feature
When I allocate a context, there is seemingly nothing I can do to free the memory used. It seems to keep the memory without being freed. Also... If I clear the canvas and allocate a second one, it seems to re-use the buffer of the first canvas under certain conditions.
* Creating context from a 10kx10k canvas allocated 400mb
* Deleting context and canvas does not free memory
* There are very strange allocation behaviour depending on the order you create the canvas
Other oddities are that reallocating multiple canvas and context does not use more memory seems to re-use memory pool if you allocate the canvas next to each other...
```
let canvas1 = createCanvas(10000, 10000)
let canvas2 = createCanvas(10000, 10000)
let context1 = canvas1.getContext('2d')
let context2 = canvas2.getContext('2d')
// rss / heap / ext
// 413 MiB / 2 MiB / 1 MiB
```
but doubles memory when allocated interleaved
```
let canvas1 = createCanvas(10000, 10000)
let context1 = canvas1.getContext('2d')
let canvas2 = createCanvas(10000, 10000)
let context2 = canvas2.getContext('2d')
// rss / heap / ext
//793 MiB / 2 MiB / 1 MiB / context created
```
## Steps to Reproduce
```js
const { createCanvas, Canvas, loadImage, Image } = require('canvas')
async function printMem(name) {
await new Promise((r) => setTimeout(r, 1000))
global.gc()
const mem = process.memoryUsage()
const rss = (mem.rss/1024/1024).toFixed(0).padStart(3)
const heap = (mem.heapUsed/1024/1024).toFixed(0).padStart(3)
const external = (mem.external/1024/1024).toFixed(0).padStart(3)
console.log(`${rss} MiB / ${heap} MiB / ${external} MiB / ${name}`)
}
async function run() {
console.log(' rss / heap / ext / name')
await printMem('start')
let canvas = createCanvas(10000, 10000)
await printMem('canvas created')
let context = canvas.getContext('2d')
await printMem('context created')
context = null
await printMem('nulled context')
canvas = null
await printMem('nulled canvas all')
}
run()
// rss / heap / ext / name
// 29 MiB / 2 MiB / 1 MiB / start
// 29 MiB / 2 MiB / 1 MiB / canvas created
// 413 MiB / 2 MiB / 1 MiB / context created
// 412 MiB / 2 MiB / 1 MiB / nulled context
// 412 MiB / 2 MiB / 1 MiB / nulled canvas all
```
## Your Environment
* canvas@2.6.1
* v12.20.1 on Mac OS X 10.15.7
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.