Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
2

app.doScript memory leak

Community Beginner ,
Jan 22, 2020 Jan 22, 2020

in my scripts i use the app.doScript method which allows me to have a single undo for the entire script.

In a persistent engine i noticed (using the $.summary) that every time a script is running,  the functions count increase.

Even after a double $.gc() the memory is not released.

 

 

#targetengine 'test'

function test(v) {
    return v + 1;
}

app.doScript(test);

$.gc();
$.gc();

alert($.summary());

 

 

there's a way to prevent this sort of memory leak? 

TOPICS
Performance , Scripting
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Jan 22, 2020 Jan 22, 2020

Something like this seems to avoid the problem:

#targetengine test
var test;
!test && test = function(v) {
    return v + 1;
}

app.doScript(test);

$.gc();
$.gc();

alert($.summary());
Translate
Community Expert ,
Jan 22, 2020 Jan 22, 2020

Hi Roberto,

did you test different undo modes?

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#UndoModes.html

 

What's your version of InDesign and operating system?

 

Regards,
Uwe Laubender

( ACP )

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 22, 2020 Jan 22, 2020

Hi Uwe,

 

I'm on Macbook Pro, with High Sierra.

I tried all the 4 undo modes, in InDesign CC2019 and CC2020.

Same results. Functions count increase every time, garbage collector seems to fail to release the memory used, even with simple function that does not involve the InDesign DOM.

 

thank you, Roberto

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 22, 2020 Jan 22, 2020

I notice that if you do not use a custom target engine, and use Main instead, the function count doesn't increase.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 22, 2020 Jan 22, 2020

i know.

But I have to use a persistent engine, loading tons of other functions, create menu, menuaction, etc.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 22, 2020 Jan 22, 2020

Something like this seems to avoid the problem:

#targetengine test
var test;
!test && test = function(v) {
    return v + 1;
}

app.doScript(test);

$.gc();
$.gc();

alert($.summary());
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 22, 2020 Jan 22, 2020
LATEST

yes!! .... and (maybe) no..

I thought the problem was the app.doScript() but is the redeclaration of the test function.

 

But leaving the redeclaration and calling test() (without the app.doScript) the functions count does not increase. Very strange.

 

thank you, Roberto

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jan 22, 2020 Jan 22, 2020

When you run the script, a new function with the same name is defined.

With your call of doScript, the ExtendScript engine passes a reference to that function to InDesign.

Apparently InDesign never releases that reference, so the persistent target engine has to keep it around. For example other places where such references are passed are event handlers / listeners, and there you want to stick them around for long.

 

There are multiple solutions:

invoke the function by name, passing a script code instead of the function object.

app.doScript("test()");

Define the function once, and in later invokations refer to the existing function.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines