Skip to content

Instantly share code, notes, and snippets.

@vladar
Last active April 28, 2021 21:44
Show Gist options
  • Save vladar/046bae076d8d42f802fb520e7ff8b226 to your computer and use it in GitHub Desktop.
Save vladar/046bae076d8d42f802fb520e7ff8b226 to your computer and use it in GitHub Desktop.
Testing garbage collection with `lmdb-store` using `cache` option
// node --expose-gc gc-test.js
// CACHE=1 node --expose-gc gc-test.js
const { open } = require("lmdb-store")
const ptop = require("process-top")()
let rootDb
function getRootDb() {
if (!rootDb) {
rootDb = open({
name: `root`,
path: process.cwd() + `/.data`,
cache: Boolean(process.env.CACHE),
})
}
return rootDb
}
async function fillDb(db, len) {
let promise
for (let i = 0; i < len; i++) {
promise = db.put(i, { foo: new Array(1000).join(String(i)), i })
if (i % 100 === 0) {
await new Promise(resolve => setTimeout(resolve, 5))
}
}
return promise
}
function reportMemory() {
const used = ptop.memory().heapUsed / (1024 * 1024)
const total = ptop.memory().heapTotal / (1024 * 1024)
console.log(`heap: ${used.toFixed(2)} MB / ${total.toFixed(2)} MB`)
}
async function run() {
const db = getRootDb()
await fillDb(db, 10000)
db.getKeys().forEach((key) => {
const value = db.get(key)
if (value.i % 1000 === 0) {
reportMemory()
}
})
console.log(``)
console.log(`after loop:`)
reportMemory()
global.gc()
console.log(`after forced gc:`)
reportMemory()
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment