Skip to content

Instantly share code, notes, and snippets.

@arunoda
Last active March 20, 2018 08:52
Show Gist options
  • Save arunoda/0fee721fd13111cd676c to your computer and use it in GitHub Desktop.
Save arunoda/0fee721fd13111cd676c to your computer and use it in GitHub Desktop.
V8 Memory Usage

V8 Mem Test

This is test about, how V8 caches internal objects and how it affects with the Memory. But there is no good way to findout the exact amount of memory used by given set of objects without profiling. So, we are using gc() with following steps to pick a decent value.

For that, you need to run following scripts with --expose gc NodeJS option.

gc();
var v8memoryUsage = process.memoryUsage().rss;
var arr = [];
var str = getString(5000);
var obj = {data: str};
for(var lc=0; lc<2000; lc++) {
// Simulating that we got data from some other process
var newData = JSON.parse(JSON.stringify(obj));
// adding some other value
newData.a = Math.random();
arr.push(newData);
}
// make sure, we removed all the garbage
gc();
var memoryForObjects = (process.memoryUsage().rss - v8memoryUsage)/1024/1024;
console.log('completed with: ', memoryForObjects.toFixed(2), "MB of Memory");
function getString(length) {
var str = "";
for(var lc=0; lc<length; lc++) {
str+= "" + Math.ceil(Math.random() * 9);
}
return str;
}
sh-3.2$
sh-3.2$
sh-3.2$ node --expose-gc 1-idential-strings.js
completed with: 25.91 MB of Memory
sh-3.2$
sh-3.2$
gc();
var v8memoryUsage = process.memoryUsage().rss;
var arr = [];
for(var lc=0; lc<2000; lc++) {
// a new object with a unique string
var newData = {data: getString(5000)};
// adding some other value
newData.a = Math.random();
arr.push(newData);
}
// make sure, we removed all the garbage
gc();
var memoryForObjects = (process.memoryUsage().rss - v8memoryUsage)/1024/1024;
console.log('completed with: ', memoryForObjects.toFixed(2), "MB of Memory");
function getString(length) {
var str = "";
for(var lc=0; lc<length; lc++) {
str+= "" + Math.ceil(Math.random() * 9);
}
return str;
}
sh-3.2$
sh-3.2$
sh-3.2$
sh-3.2$ node --expose-gc 3-different-strings.js
completed with: 419.12 MB of Memory
sh-3.2$
sh-3.2$
sh-3.2$

V8 is super genious and know about object better than you and me. So generally, we really don't need to worry too much if we are going to cache data on the server.

This test was done to simulate, it's okay for Meteor to cache all the client data on Memory.

For some apps, caching causes some issues. See following link for how to act accordingly.

Continue the discussion on Kadira Academy for more info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment