Skip to content

Commit

Permalink
feat(core): Collect our own garbage
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypfer committed Apr 6, 2021
1 parent 371ceea commit 3c3df9a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
36 changes: 23 additions & 13 deletions lib/Valetudo.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,40 @@ class Valetudo {

/**
* Surprisingly, allocated buffers are not part of the v8 heap.
* Furthermore even though that they could be garbage collected, for some reason
* that doesn't happen immediately (at least on node v14.16.0)
* Furthermore even though that they could be garbage collected,
* for some reason that doesn't happen immediately (at least on node v14.16.0) which leads to
* memory issues on machines like the roborock s5 max
*
* Therefore we'll manually force a gc if the memory usage seems odd
*
* This is only active if the --expose-gc flag is provided which should only be the case
* for either testing or the lowmem build
*
* This could use some more testing and will probably require tweaking with new hw as well as sw versions
*/
//@ts-ignore
if (typeof gc === "function") {
if (typeof global.gc === "function") {
const heapLimit = v8.getHeapStatistics().heap_size_limit;
const overLimit = heapLimit + (8*1024*1024); //8mb of buffers and other stuff sounds somewhat reasonable

setInterval(() => {
const rss = process.memoryUsage().rss;

if (rss > overLimit) {
Logger.debug("Garbage collection forced. rss: " + rss);
//@ts-ignore
//eslint-disable-next-line no-undef
gc();
/*
* os.freemem() is quite a lot cheaper than process.memoryUsage(), because process.memoryUsage()
* iterates over all pages
*
* With node v15, we could use process.memoryUsage.rss() which doesn't do that,
* but we don't have that version available yet
*
* Therefore, we'll only act when the overall memory usage gets somewhat critical
*/
if (os.freemem() <= 52*1024*1024) {
const rss = process.memoryUsage().rss;

if (rss > overLimit) {
Logger.debug("Garbage collection forced. rss: " + rss);
//@ts-ignore
//eslint-disable-next-line no-undef
global.gc();
}
}

}, 100);
}
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
"ts-check": "tsc -p jsconfig.json",
"test": "mocha \"test/**/*_spec.js\"",
"build_all": "npm run build && npm run build_aarch64 && npm run build_armv7_lowmem",
"build": "cross-env PKG_CACHE_PATH=./build_dependencies/pkg pkg --targets node14-linux-armv7 --no-bytecode --options max-heap-size=34 . --output ./build/armv7/valetudo",
"build_aarch64": "cross-env PKG_CACHE_PATH=./build_dependencies/pkg pkg --targets node14-linux-arm64 --no-bytecode --options max-heap-size=64 . --output ./build/aarch64/valetudo",
"build_armv7_lowmem": "cross-env PKG_CACHE_PATH=./build_dependencies/pkg pkg --targets node14-linux-armv7 --no-bytecode --options expose-gc,max-heap-size=30,optimize-for-size,always-compact,lite-mode . --output ./build/armv7/valetudo_lowmem"
"build": "cross-env PKG_CACHE_PATH=./build_dependencies/pkg pkg --targets node14-linux-armv7 --no-bytecode --options expose-gc,max-heap-size=34 . --output ./build/armv7/valetudo",
"build_aarch64": "cross-env PKG_CACHE_PATH=./build_dependencies/pkg pkg --targets node14-linux-arm64 --no-bytecode --options expose-gc,max-heap-size=64 . --output ./build/aarch64/valetudo",
"build_armv7_lowmem": "cross-env PKG_CACHE_PATH=./build_dependencies/pkg pkg --targets node14-linux-armv7 --no-bytecode --options expose-gc,max-heap-size=30,optimize-for-size,lite-mode . --output ./build/armv7/valetudo_lowmem"
},
"pre-commit": [
"ts-check",
Expand Down

0 comments on commit 3c3df9a

Please sign in to comment.