The $.summary.difference() Function

The builtin $.summary() function produces a table of allocated objects, counted by type. This is a valuable information for developers who have to optimize long running scripts for lower resource usage, or also to find memory leaks in permanent script engines.

The absolute values of $.summary() give a good starting point, but the more relevant part of the information is change. The new function $.summary.difference() takes a snapshot of the summary values and reports only the differences comparing to the previous snapshot.

Note that initialization must be done explicitly, so you can continue from the snapshot of the previous run when using a permanent ExtendScript engine - all but InDesign's "main" which is reset between runs.


#include "/" // We're looking for leaks missed by the garbage collector. $.gc();$.gc(); // optionally, initialize the snapshot $.summary.difference(); /* your script here */ // Let the garbage collector again do its work. // Double is better. Believe me! $.gc();$.gc(); // Output is displayed as result, in the ESTK console panel. $.summary.difference();

The Script

// DESCRIPTION: Install the $.summary.difference() function. // Copyright ©2010 Dirk Becker, // This software is provided "as is", with absolutely no warranty. // Permission to install and use the software is hereby granted without fee. // Unauthorized redistribution by any means is expressly prohibited. $.summary.difference = function() { return $.summary().replace( / *([0-9]+) ([^ ]+)(\n?)/g, $.summary.updateSnapshot ); } $.summary.updateSnapshot = function (full,count,name,lf) { var snapshot = $.summary.snapshot; count = Number(count); var prev = snapshot[name] ? snapshot[name] : 0; snapshot[name] = count; var diff = count-prev; if( diff==0 ) return ""; return " ".substring(String(diff).length)+diff+" "+name+lf; } if( !$.summary.snapshot ) { $.summary.snapshot = []; $.summary.difference(); } $.gc(); $.gc(); $.summary.difference();