Skip to content

Instantly share code, notes, and snippets.

@inimino
Forked from anonymous/leak.js
Created April 21, 2010 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inimino/374051 to your computer and use it in GitHub Desktop.
Save inimino/374051 to your computer and use it in GitHub Desktop.
# bash one-liner to run it and watch memory usage
( ./node test.js & PID=$! && trap 'kill $PID' 0; while sleep 1; do ps -o rss= -p $PID; done )
var sys = require('sys')
function processAll (items, callback) {
next()
function next(){
if(!items.length) callback()
else processOne(items.pop(), cb)}
function cb(){
if(items.length%1000 == 0) sys.puts('Remaining: '+items.length)
if(items.length%10000 == 0) setTimeout(next, 10000) // wait for gc to happen
else next()}}
function processOne (item, callback) {
// Simulate some work
setTimeout(callback, 2)
}
var items = []
for (i = 0; i < 400000; i++)
items.push(i)
processAll(items, function() {
sys.puts("done")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment