Hi folks,
I was analyzing some strange behavior of JavaScript concerning memory consumption, garbage collection. Following some questions, observations:
1. Inner Functions/Objects/Function Pointers:
function Hashtable() {
this.valueList = new Array();
var test = function() { return 1; }
this.putValue = function(name, value) {
this.valueList[name] = value;
}
}
for (var i = 0; i < 100000; i++) {
var hash = new Hashtable();
hash.putValue("Key" + i, "Value" + i);
delete hash;
hash = null;
}
Oberservation: Running the script with the inner function test, the memory will rise per loop, despite the use of delete and null per loop. Because of the memory consumption the execution time will rise exponential. Without the inner function, the memory won't rise and the script is much faster.
Question: What is so special on an inner function for the JavaScript engine? What kind of engine are you using, does it has a JIT compiler? Why can't I kill the object references with delete/setting null?
2. Usage of targetengine directive:
//Startup Scripts/script1.jsx
#targetengine "my_special_target"
function PersistentObject() {
this.getXYZ = function() {...}
}
...
MyPersistendObject = new PersistentObject();
...
//Scripts/script2.jsx
#targetengine "my_special_target"
var newObject = new Object();
MyPersistendObject.getXYZ();
Oberservation: Using this code from within a Startup Script, I can create peristend objects, I can use from a second script. The second script needs to "connect" to the same targetengine to call something like MyPersistendObject.getXYZ(). That's very usefull for caching etc. But all my code within the second script seems to be peristent as well. If I run the second script, all objects will be created twice and the references of the first run will reside too. This means the memory consumption will rise linear with the objects of second script per run. The execution time will rise exponential because of expensive memory allocation.
Question: Does targetengine means no run of garbage collection at all? How can I override reference instead of creating new ones. Can I influence the garbage collection at all (with $.gc())? Is their a difference between the garbage collection of InDesign CS4 vs. InDesign Server CS4?
Thanks for any information.
Tino