experimental GC

This commit is contained in:
Mark Nadal 2018-05-15 22:31:42 -07:00
parent 74699f64f8
commit 2015a01b65
4 changed files with 36 additions and 6 deletions

View File

@ -7,14 +7,17 @@
global.DEBUG = 1;
setInterval(function(){
var mem = process.memoryUsage();
var used = mem.heapUsed / 1024 / 1024;
var used = mem.heapTotal / 1024 / 1024;
var print = '';
used = used.toFixed(1);
print += used +' MB. '
if(db.root){
db.concurrency = Object.keys(db.peers||{}).length;
print += db.concurrency +' peers. ';
db.nodes = Object.keys(db.root.graph||{}).length;
print += db.nodes + ' nodes. ';
}
if(db.count){ print += db.count + ' msgs. '}
console.log(print);
}, 2500);
@ -24,6 +27,13 @@
if(root.once){ return }
db.root = root;
db.peers = root.opt.peers;
db.count = 0;
root.on('in', function(msg){
this.to.next(msg);
db.last = msg;
db.count++;
})
})
}());

View File

@ -1,13 +1,32 @@
;(function(){
var Gun = (typeof window !== 'undefined')? window.Gun : require('../gun');
var LRU = 1, empty = {}, u;
var ev = {}, empty = {}, u;
Gun.on('opt', function(root){
this.to.next(root);
if(root.once){ return }
root.on('get', function(msg){
})
if(typeof process == 'undefined'){ return }
var util = process.memoryUsage;
if(!util){ return }
ev.max = parseFloat(root.opt.memory || process.env.WEB_MEMORY || 512) * 0.8;
setInterval(check, 250);
function check(){
var used = ev.used = util().heapTotal / 1024 / 1024;
if(used < ev.max){ return }
setTimeout(GC, 1);
}
function GC(){
var souls = Object.keys(root.graph||empty);
var toss = Math.ceil(souls.length * 0.1);
//var start = Gun.state(), i = toss;
Gun.list.map(souls, function(soul){
if(--toss < 0){ return }
root.gun(soul).off();
});
//console.log("evicted", i, 'nodes in', ((Gun.state() - start)/1000).toFixed(2), 'sec.');
}
/*
root.on('in', function(msg){
this.to.next(msg);

View File

@ -9,6 +9,7 @@
require('./verify');
require('./file');
require('./bye');
require('./evict');
if('debug' === process.env.GUN_ENV){ require('./debug') }
module.exports = Gun;
}());

View File

@ -10,7 +10,7 @@ Gun.on('opt', function(ctx){
if(ctx.once){ return }
if(false !== opt.localStorage && !process.env.AWS_S3_BUCKET){ return } // TODO: Remove this after migration.
if(false === opt.radisk){ return }
console.log("BUG WARNING: Radix Storage Engine (RSE) has a known rare edge case, if data gets split between file chunks, a GET may only return the first chunk!!!");
console.log("BUG WARNING: Radix Storage Engine (RAD) has a known rare edge case, if data gets split between file chunks, a GET may only return the first chunk!!!");
opt.store = opt.store || Store(opt);
var rad = Radisk(opt);