Gergely Nemeth, RisingStack
@nthgergo
RisingStack
is now a member of the
Node.js Foundation
Then....
About Memory
int * array = malloc(10 * sizeof(int));
if (NULL == array) {
fprintf(stderr, "malloc failed\n");
return(-1);
}
free(array);Meet the Garbage Collector
Before collection
After collection
memory allocation is cheap
collection is expensive
is the portion of memory occupied by a process that is held in the RAM, this contains the code itself, the stack and the heap
contains primitive types and references to objects
stores reference types, like objects, strings or closures
the size of memory that is held by the object itself
the size of the memory that is freed up once the object is deleted along with its' dependent objects
new space
old space
and
New space
Old space
Scavenge Compact Runs
Mark Sweep Runs
npm install heapdumpvar heapdump = require('heapdump');
heapdump.writeSnapshot(function(err, filename) {
console.log('dump written to', filename);
});$ kill -USR2 <pid>or
const heapdump = require('heapdump')
const app = require('koa')()
const router = require('koa-router')()
const log = [];
router.get('/', function *(next) {
log.push(this.headers)
this.body = {
status: 'finding a leak'
}
})
app
.use(router.routes())
.use(router.allowedMethods())
app.listen(process.env.PORT || 3000)
extensible utility for:
generate a core dump of a running program with process ID pid.
# create core dump of the node process
gcore `pgrep node`
# search for the all the JS Objects on the heap
> ::findjsobjects
# analyze them using
object_id::jsprint
# find the retainer of the object
object_id::findjsobjects -r for the brave ones
node --v8-options# expose gc extension
--expose_gc
# print one trace line following each garbage collection
--trace_gc
# Enables optimizations which favor memory size over execution speed.
--optimize_for_size
# max size of the old space (in Mbytes)
--max_old_space_size
# grow the new space based on the percentage of survivors
--experimental_new_space_growth_heuristicLearn Node.js from Zero to Hero
http://bit.ly/nodehero
https://slides.com/gergelyke
Gergely Nemeth, RisingStack
@nthgergo
Thanks!