Last active
April 28, 2021 21:44
-
-
Save vladar/046bae076d8d42f802fb520e7ff8b226 to your computer and use it in GitHub Desktop.
Testing garbage collection with `lmdb-store` using `cache` option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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