You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constfs=require('fs');constAjv=require('ajv');constaddFormats=require('ajv-formats');// Run me with `node --expose-gc ./leak.js`constpath=__dirname+'/out.tsv';fs.writeFileSync(path,'i\tHeapUsed\r\n','utf8');constajv=newAjv({allErrors: true,allowUnionTypes: true,allowMatchingProperties: true,strict: false,});addFormats(ajv);global.gc();for(leti=0;i<1000;i++){constvalidate=ajv.compile({type: 'string',format: 'something',maxLength: 50});constvalid=validate('test');if(i%10===0){constmem=process.memoryUsage();global.gc();fs.appendFileSync(path,`${i}\t${mem.heapUsed}\r\n`);}}console.log();constcontent=fs.readFileSync(path,'utf8');console.log(content);
It’s a duplicate of #1413 - compiling the “same” schema in the loop would create a new function every time because starting from v7 Ajv uses the schema itself as a key for Map, rather than it’s serialization.
A solution is to either make sure you are compiling schema once (that is outside of the loop), or at least the schema object itself is defined outside of the loop.
This is a stripped down version of stoplightio/prism#1881
What version of Ajv are you using?
8.6.3
Does the issue happen if you use the latest version?
Yes
Ajv options object
Your code
package.json
leak.js
This generates the following output:
Although, the garbage collector is regularly triggered, the amount of used heap keeps on growing.
What results did you expect?
I would expect the heap to stay more or less stable.
Are you going to resolve the issue?
Sorry, I don't have the required skills. :-(
The text was updated successfully, but these errors were encountered: