Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Describe how to free memory in README and fix a memory leak in playground #314

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ This package also ships a pure WebAssembly artifact built with `wasm-bindgen` to
const resvgJS = new resvg.Resvg(svg, opts)
const pngData = resvgJS.render(svg, opts) // Output PNG data, Uint8Array
const pngBuffer = pngData.asPng()
pngData.free() // Free memory
resvgJS.free()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one doesn't seem to be needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mizdra Take a look here for other LGTM.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Removing resvgJS.free() will cause a memory leak in the external area.

Test Code
// wasm-memory-leak-test.mjs
import { readFile } from 'fs/promises'
import { join } from 'path'
import { setTimeout } from 'timers/promises'

import { Resvg, initWasm } from './wasm/index.mjs'

const wasmPath = join(import.meta.dirname, './wasm/index_bg.wasm')

if (!global.gc) {
  throw new Error('You must run this script with --expose-gc')
}

function logMemoryUsage(i, rss, heapTotal, heapUsed, external) {
  console.log(
    `${i.padStart(8, ' ')},` +
      `${rss.padStart(12, ' ')},` +
      `${heapTotal.padStart(12, ' ')},` +
      `${heapUsed.padStart(12, ' ')},` +
      `${external.padStart(12, ' ')},`,
  )
}

await initWasm(readFile(wasmPath))
const svg = await readFile(join(import.meta.dirname, './example/text.svg'))
const opts = {
  background: 'rgba(238, 235, 230, .9)',
  fitTo: {
    mode: 'width',
    value: 1200,
  },
  font: {
    fontFiles: ['./example/SourceHanSerifCN-Light-subset.ttf'],
    loadSystemFonts: false,
  },
}

logMemoryUsage('i', 'rss', 'heapTotal', 'heapUsed', 'external')

for (let i = 0; i < 1000000; i++) {
  const resvg = new Resvg(svg, opts)
  const pngData = resvg.render()
  const pngBuffer = pngData.asPng()
  pngData.free()
  resvg.free()
  await setTimeout(0)
  if (i % 10 === 0) {
    // NOTE: Do GC before measuring memory usage because the garbage is not measured.
    global.gc()
    const usage = process.memoryUsage()

    logMemoryUsage(
      i.toString(),
      usage.rss.toString(),
      usage.heapTotal.toString(),
      usage.heapUsed.toString(),
      usage.external.toString(),
    )
  }
}
Log (with `resvg.free()`)
$ node --expose-gc wasm-memory-leak-test.mjs
       i,         rss,   heapTotal,    heapUsed,    external,
       0,   138412032,    10420224,     4730568,    26217124,
      10,   147652608,    10158080,     4784248,    25229371,
      20,   157597696,    10158080,     4993736,    25229371,
      30,   157417472,    10158080,     4790728,    25229371,
      40,   157483008,     7012352,     4799736,    25229371,
      50,   156106752,     7012352,     4799832,    25229371,
      60,   155664384,     7012352,     4801072,    25229371,
      70,   155910144,     7012352,     4807656,    25229371,
      80,   155926528,     7012352,     4863272,    25229371,
      90,   155992064,     7012352,     4862560,    25229371,
     100,   164970496,     7012352,     4876304,    25229371,
     110,   164937728,     7012352,     4876528,    25229371,
     120,   165183488,     7012352,     4876896,    25229371,
     130,   165199872,     7012352,     4873816,    25229371,
     140,   165183488,     7012352,     4873816,    25229371,
     150,   165298176,     7012352,     4873816,    25229371,
     160,   165314560,     7012352,     4873816,    25229371,
     170,   165363712,     7012352,     4875656,    25229371,
     180,   165314560,     7012352,     4875656,    25229371,
     190,   165380096,     7012352,     4875656,    25229371,
     200,   165380096,     7012352,     4875656,    25229371,
     210,   165380096,     7012352,     4875656,    25229371,
     220,   165380096,     7012352,     4875656,    25229371,
     230,   165396480,     7012352,     4875656,    25229371,
     240,   165036032,     7012352,     4875656,    25229371,
     250,   165085184,     7012352,     4875656,    25229371,
     260,   165068800,     7012352,     4875656,    25229371,
     270,   165052416,     7012352,     4875656,    25229371,
     280,   165085184,     7012352,     4875656,    25229371,
     290,   165068800,     7012352,     4875656,    25229371,
     300,   165052416,     7012352,     4875656,    25229371,
     310,   165085184,     7012352,     4875656,    25229371,
     320,   165117952,     7012352,     4875656,    25229371,
     330,   165117952,     7012352,     4875656,    25229371,
     340,   165101568,     7012352,     4876648,    25229371,
     350,   165117952,     7012352,     4876648,    25229371,
     360,   165117952,     7012352,     4876648,    25229371,
     370,   165134336,     7012352,     4876648,    25229371,
     380,   165117952,     7012352,     4881432,    25229371,
     390,   165117952,     7012352,     4881432,    25229371,
     400,   168148992,     7012352,     4881432,    25229371,
     410,   168116224,     7012352,     4881432,    25229371,
     420,   168148992,     7012352,     4881432,    25229371,
     430,   168165376,     7012352,     4883792,    25229371,
     440,   168165376,     7012352,     4883792,    25229371,
     450,   168132608,     7012352,     4883792,    25229371,
     460,   168165376,     7012352,     4883792,    25229371,
     470,   168148992,     7012352,     4883792,    25229371,
     480,   168198144,     7012352,     4883792,    25229371,
     490,   168198144,     7012352,     4883792,    25229371,
     500,   168148992,     7012352,     4883792,    25229371,
     510,   168148992,     7012352,     4883792,    25229371,
     520,   168198144,     7012352,     4883792,    25229371,
     530,   168165376,     7012352,     4883792,    25229371,
     540,   168198144,     7012352,     4883792,    25229371,
     550,   168230912,     7012352,     4885112,    25229371,
     560,   168230912,     7012352,     4885112,    25229371,
     570,   168263680,     7012352,     4885112,    25229371,
     580,   168247296,     7012352,     4885112,    25229371,
     590,   168247296,     7012352,     4885112,    25229371,
     600,   168247296,     7012352,     4885112,    25229371,
     610,   168263680,     7012352,     4885112,    25229371,
     620,   168263680,     7012352,     4885112,    25229371,
     630,   168263680,     7012352,     4885112,    25229371,
     640,   168247296,     7012352,     4885112,    25229371,
     650,   168263680,     7012352,     4885112,    25229371,
     660,   168263680,     7012352,     4885112,    25229371,
     670,   168280064,     7012352,     4885112,    25229371,
     680,   168247296,     7012352,     4885112,    25229371,
     690,   168280064,     7012352,     4885112,    25229371,
     700,   168247296,     7012352,     4885112,    25229371,
     710,   168247296,     7012352,     4885112,    25229371,
     720,   168280064,     7012352,     4885112,    25229371,
     730,   168280064,     7012352,     4885112,    25229371,
     740,   168280064,     7012352,     4885112,    25229371,
     750,   168247296,     7012352,     4885712,    25229371,
     760,   168280064,     7012352,     4885712,    25229371,
     770,   168230912,     7012352,     4885712,    25229371,
     780,   168247296,     7012352,     4885712,    25229371,
     790,   168263680,     7012352,     4885712,    25229371,
     800,   168280064,     7012352,     4885712,    25229371,
     810,   168280064,     7012352,     4885712,    25229371,
     820,   168329216,     7012352,     4885712,    25229371,
     830,   168312832,     7012352,     4885712,    25229371,
     840,   168296448,     7012352,     4885712,    25229371,
     850,   168329216,     7012352,     4885712,    25229371,
     860,   168329216,     7012352,     4885712,    25229371,
     870,   168329216,     7012352,     4885712,    25229371,
     880,   168329216,     7012352,     4885712,    25229371,
     890,   168296448,     7012352,     4885712,    25229371,
     900,   168329216,     7012352,     4885712,    25229371,
     910,   168329216,     7012352,     4885712,    25229371,
     920,   168296448,     7012352,     4885712,    25229371,
     930,   168329216,     7012352,     4885712,    25229371,
     940,   168329216,     7012352,     4885712,    25229371,
     950,   168312832,     7012352,     4885712,    25229371,
     960,   168296448,     7012352,     4885712,    25229371,
     970,   168329216,     7012352,     4885712,    25229371,
     980,   168247296,     7012352,     4885712,    25229371,
     990,   168329216,     7012352,     4885712,    25229371,
    1000,   168312832,     7012352,     4887488,    25229371,
    1010,   168296448,     7012352,     4887488,    25229371,
    1020,   168312832,     7012352,     4887488,    25229371,
    1030,   168345600,     7012352,     4887488,    25229371,
    1040,   168296448,     7012352,     4887488,    25229371,
    1050,   168263680,     7012352,     4887488,    25229371,
    1060,   168361984,     7012352,     4887488,    25229371,
    1070,   168493056,     7012352,     4887488,    25229371,
    1080,   168460288,     7012352,     4887488,    25229371,
    1090,   168460288,     7012352,     4887488,    25229371,
    1100,   168525824,     7012352,     4887488,    25229371,
    1110,   168542208,     7012352,     4887488,    25229371,
    1120,   168542208,     7012352,     4887488,    25229371,
    1130,   168493056,     7012352,     4887488,    25229371,
    1140,   168542208,     7012352,     4887488,    25229371,
    1150,   168509440,     7012352,     4887488,    25229371,
    1160,   168525824,     7012352,     4887488,    25229371,
    1170,   168542208,     7012352,     4887488,    25229371,
    1180,   168787968,     7012352,     4887488,    25229371,
    1190,   168804352,     7012352,     4887488,    25229371,
    1200,   168771584,     7012352,     4887488,    25229371,
    1210,   168771584,     7012352,     4887488,    25229371,
    1220,   168787968,     7012352,     4887488,    25229371,
    1230,   168804352,     7012352,     4887488,    25229371,
    1240,   168787968,     7012352,     4887488,    25229371,
    1250,   168804352,     7012352,     4887488,    25229371,
    1260,   168787968,     7012352,     4887488,    25229371,
    1270,   168771584,     7012352,     4887488,    25229371,
    1280,   168804352,     7012352,     4887488,    25229371,
    1290,   168787968,     7012352,     4887488,    25229371,
    1300,   168804352,     7012352,     4887488,    25229371,
    1310,   168787968,     7012352,     4887488,    25229371,
    1320,   168787968,     7012352,     4887488,    25229371,
Log (with `resvg.free()`)
$ node --expose-gc wasm-memory-leak-test.mjs
       i,         rss,   heapTotal,    heapUsed,    external,
       0,   133611520,    10158080,     4730784,    26217124,
      10,   149241856,    10158080,     4782904,    25229371,
      20,   172507136,    10158080,     4996904,    26867771,
      30,   172851200,    10158080,     4783984,    26867771,
      40,   173391872,     7012352,     4793296,    26867771,
      50,   172769280,     7012352,     4793392,    26867771,
      60,   172589056,     7012352,     4802528,    28506171,
      70,   173096960,     7012352,     4802992,    28506171,
      80,   173604864,     7012352,     4861872,    28506171,
      90,   174112768,     7012352,     4861160,    30144571,
     100,   184516608,     7012352,     4875528,    30144571,
     110,   184991744,     7012352,     4875752,    30144571,
     120,   185450496,     7012352,     4876120,    30144571,
     130,   185909248,     7012352,     4873040,    31782971,
     140,   186384384,     7012352,     4873040,    31782971,
     150,   186859520,     7012352,     4873040,    31782971,
     160,   187351040,     7012352,     4873040,    33421371,
     170,   187842560,     7012352,     4874920,    33421371,
     180,   188301312,     7012352,     4874920,    33421371,
     190,   188841984,     7012352,     4875048,    35059771,
     200,   189349888,     7012352,     4874920,    35059771,
     210,   189792256,     7012352,     4874920,    35059771,
     220,   190267392,     7012352,     4874920,    35059771,
     230,   191004672,     7012352,     4874920,    36698171,
     240,   191447040,     7012352,     4874920,    36698171,
     250,   191922176,     7012352,     4874920,    36698171,
     260,   192380928,     7012352,     4875048,    37119851,
     270,   192839680,     7012352,     4874920,    38336571,
     280,   193347584,     7012352,     4874920,    38336571,
     290,   193806336,     7012352,     4874920,    38336571,
     300,   193888256,     7012352,     4874920,    39214521,
     310,   194428928,     7012352,     4874920,    39974971,
     320,   194871296,     7012352,     4874920,    39974971,
     330,   195297280,     7012352,     4875048,    40396651,
     340,   195837952,     7012352,     4876392,    41613371,
     350,   196280320,     7012352,     4876392,    41613371,
     360,   196788224,     7012352,     4876392,    41613371,
     370,   197246976,     7012352,     4876392,    42491321,
     380,   197738496,     7012352,     4881176,    43251771,
     390,   198213632,     7012352,     4881176,    43251771,
     400,   198721536,     7012352,     4881304,    43673451,
     410,   199180288,     7012352,     4881176,    44890171,
     420,   199655424,     7012352,     4881176,    44890171,
     430,   200163328,     7012352,     4883536,    44890171,
     440,   200638464,     7012352,     4883536,    45768121,
     450,   201097216,     7012352,     4883536,    46528571,
     460,   201605120,     7012352,     4883536,    46528571,
     470,   202031104,     7012352,     4883664,    46950251,
     480,   202539008,     7012352,     4883536,    48166971,
     490,   203014144,     7012352,     4883536,    48166971,
     500,   203505664,     7012352,     4883536,    48166971,
     510,   203931648,     7012352,     4883536,    49044921,
     520,   204406784,     7012352,     4883536,    49805371,
     530,   204931072,     7012352,     4883536,    49805371,
     540,   205389824,     7012352,     4883664,    50227051,
     550,   205930496,     7012352,     4884856,    51443771,
     560,   206520320,     7012352,     4884856,    51443771,
     570,   207110144,     7012352,     4884856,    51443771,
     580,   207568896,     7012352,     4884856,    52473811,
     590,   208044032,     7012352,     4884856,    53082171,
     600,   208519168,     7012352,     4884856,    53082171,
     610,   209174528,     7012352,     4884856,    53655941,
     620,   209666048,     7012352,     4884856,    54720571,
     630,   210124800,     7012352,     4884856,    54720571,
     640,   210583552,     7012352,     4884856,    54720571,
     650,   211042304,     7012352,     4884856,    55750611,
     660,   211533824,     7012352,     4884856,    56358971,
     670,   211992576,     7012352,     4884856,    56358971,
     680,   212451328,     7012352,     4884856,    56932741,
     690,   212926464,     7012352,     4884856,    57997371,
     700,   213434368,     7012352,     4884856,    57997371,
     710,   213909504,     7012352,     4884856,    57997371,
     720,   214351872,     7012352,     4884856,    59179501,
     730,   214794240,     7012352,     4884856,    59635771,
     740,   215318528,     7012352,     4884856,    59635771,
     750,   215728128,     7012352,     4885456,    60209541,
     760,   216252416,     7012352,     4885456,    61274171,
     770,   216694784,     7012352,     4885456,    61274171,
     780,   217186304,     7012352,     4885456,    61274171,
     790,   217645056,     7012352,     4885456,    62456301,
     800,   218120192,     7012352,     4885456,    62912571,
     810,   218562560,     7012352,     4885456,    62912571,
     820,   219021312,     7012352,     4885456,    63334251,
     830,   219545600,     7012352,     4885456,    64550971,
     840,   219955200,     7012352,     4885456,    64550971,
     850,   220446720,     7012352,     4885456,    64550971,
     860,   220921856,     7012352,     4885456,    65581011,
     870,   221396992,     7012352,     4885456,    66189371,
     880,   221888512,     7012352,     4885456,    66189371,
     890,   222330880,     7012352,     4885456,    66763141,
     900,   222822400,     7012352,     4885456,    67827771,
     910,   223281152,     7012352,     4885456,    67827771,
     920,   223756288,     7012352,     4885456,    67827771,
     930,   224215040,     7012352,     4885456,    68857811,
     940,   224690176,     7012352,     4885456,    69466171,
     950,   225181696,     7012352,     4885456,    69466171,
     960,   225574912,     7012352,     4885456,    69887851,
     970,   226115584,     7012352,     4885456,    71104571,
     980,   226590720,     7012352,     4885456,    71104571,
     990,   227065856,     7012352,     4885456,    71104571,
    1000,   227491840,     7012352,     4887232,    72438791,
    1010,   227966976,     7012352,     4887232,    72742971,
    1020,   228458496,     7012352,     4887232,    72742971,
    1030,   228900864,     7012352,     4887232,    73164651,
    1040,   229376000,     7012352,     4887232,    74381371,
    1050,   229883904,     7012352,     4887232,    74381371,
    1060,   230342656,     7012352,     4887232,    74381371,
    1070,   230817792,     7012352,     4887232,    75563501,
    1080,   231309312,     7012352,     4887232,    76019771,
    1090,   231768064,     7012352,     4887232,    76019771,
    1100,   232194048,     7012352,     4887232,    76745631,
    1110,   232718336,     7012352,     4887232,    77658171,
    1120,   233160704,     7012352,     4887232,    77658171,
    1130,   233635840,     7012352,     4887232,    77658171,
    1140,   234127360,     7012352,     4887232,    79144481,
    1150,   234586112,     7012352,     4887232,    79296571,
    1160,   235044864,     7012352,     4887232,    79296571,
    1170,   235470848,     7012352,     4636200,    80022431,
    1180,   235962368,     7012352,     4636200,    80934971,
    1190,   236453888,     7012352,     4636200,    80934971,
    1200,   236945408,     7012352,     4636200,    80934971,
    1210,   237404160,     7012352,     4636200,    82269191,
    1220,   237928448,     7012352,     4636200,    82573371,
    1230,   238403584,     7012352,     4636200,    82573371,
    1240,   238845952,     7012352,     4636200,    83147141,
    1250,   239321088,     7012352,     4636200,    84211771,
    1260,   239828992,     7012352,     4636200,    84211771,
    1270,   240254976,     7012352,     4636200,    84211771,
    1280,   240746496,     7012352,     4636200,    85698081,
    1290,   241238016,     7012352,     4636200,    85850171,
    1300,   241680384,     7012352,     4636200,    85850171,
    1310,   242155520,     7012352,     4636200,    86576031,
    1320,   242647040,     7012352,     4636200,    87488571,

const svgURL = URL.createObjectURL(new Blob([pngData], { type: 'image/png' }))
document.getElementById('output').src = svgURL
})()
Expand Down
3 changes: 3 additions & 0 deletions wasm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@
const pngData = resvgJS.render()
const pngBuffer = pngData.asPng()

pngData.free()
resvgJS.free()

document.querySelector('#output img').src = URL.createObjectURL(
new Blob([pngBuffer], { type: 'image/png' })
)
Expand Down