Using Unix Commands for System Metrics

The $.summary() builtin function yields an overview with absolute numbers of "leaked" objects. In order to translate that number to wasted memory, Mac users can use Unix commands. Windows users: sorry.

Usage

The following example determines how much memory is used by a single Object. Of course such metrics are still approximations, as they do not consider other things going on within InDesign, but with a newly started application and a sufficient large array we get close...

#include "/www.ixta.com/scripts/utilities/unix.jsx" // pre-initialize the array so we don't count the array slots. var big = new Array(); var count = 10000; for( i=0; i < count; i++ ) big[i] = null; // memory usage before our example $.gc();$.gc(); var mem_before = getVMSize(); // the actually measured code - repeat for accuracy. for( i=0; i < count; i++ ) big[i] = new Object(); // memory usage after our example $.gc();$.gc(); var mem_after = getVMSize(); // output is in ESTK console $.writeln($.summary()); $.writeln("Memory used: ",(mem_after-mem_before)*1024/count); app.quit();

Output

... 10006 Object ... Memory used: 209.7152 Execution finished.

The Script

// DESCRIPTION: invoke unix commands #target "InDesign" function getPID(aName) { if( aName==undefined ) aName = "Adobe InDesign CS4"; var script = 'tell application "System Events" to get unix id of process "'+aName+'"'; var result = app.doScript(script,ScriptLanguage.APPLESCRIPT_LANGUAGE); return Number(result); } function getVMSize() { var pid = getPID(); var script = 'do shell script ("ps -o vsz= -p ' + pid +'")'; var result = app.doScript(script,ScriptLanguage.APPLESCRIPT_LANGUAGE); return Number(result); }

See also

The $.summary.difference() function.