Skip to main content
September 28, 2017
Answered

Memory leak in an Illustrator script

  • September 28, 2017
  • 2 replies
  • 3476 views

Hi there,

Open Illustrator, create a path with two points and run the jsx-script:

function test() {

  for(var i=0, x; i<20000; i++)

    x = app.activeDocument.pathItems[0].pathPoints[0].anchor[0];

}

test();

alert($.summary());

You will get "... PathPoint: 20000 ..." and lose 100Mb of memory just for reading of data. The memory will be freed when the jsx-script will be stopped.

When I try to call the function from HTML Extension the memory is never freed. PathPoint: 20000, 40000, 60000 and so on.

Do you have any suggestions how to solve the problem?

Thanks

This topic has been closed for replies.
Correct answer Trevor:

With Ai you need to use selections

Run through the CSTK (link on main page)

app.executeMenuCommand('deselectall');

(function() {

    function test() {

        var x, i, s, len;

        app.activeDocument.pathItems[0].selected = true;

        s = app.selection[0].pathPoints;

        len = s.length;

        for (i = 0; i < 20000; i++) {

            x = s[i % len].anchor[0];

        }

    }

    test();

})();

$.summary();

No leek ;-)

I run extensions on 200,000+ objects and without using selections it takes about one hour to populate an array of data, with selection it takes about  3 minutes

You should make sure the app nap if off if using mac otherwise it can add another minute

HTH

Trevor

2 replies

Vamitul
Legend
September 29, 2017

The HTML extension will create a separate jsx engine instance, and it will  stays in memory until quitting. So that answers the second part of your question.

However, the jsx should not give you those results from the beginning. My guess is that it's doing some caching of the reads behind the scenes. Try using $.gc() to force the garbage collector to run (maybe call it twice).

September 29, 2017

Vamitul  wrote

The HTML extension will create a separate jsx engine instance, and it will  stays in memory until quitting.

I read it before in SDK. But I think that's not always true. If it would be true memory should be freed after the jsx-script completed its work.

Vamitul  wrote

Try using $.gc() to force the garbage collector to run (maybe call it twice).

I tried $.gc() but that didn't not help.

Trevor:
Trevor:Correct answer
Legend
September 29, 2017

With Ai you need to use selections

Run through the CSTK (link on main page)

app.executeMenuCommand('deselectall');

(function() {

    function test() {

        var x, i, s, len;

        app.activeDocument.pathItems[0].selected = true;

        s = app.selection[0].pathPoints;

        len = s.length;

        for (i = 0; i < 20000; i++) {

            x = s[i % len].anchor[0];

        }

    }

    test();

})();

$.summary();

No leek ;-)

I run extensions on 200,000+ objects and without using selections it takes about one hour to populate an array of data, with selection it takes about  3 minutes

You should make sure the app nap if off if using mac otherwise it can add another minute

HTH

Trevor

Silly-V
Legend
September 28, 2017

have you done this with var x; declared prior to your loop?

September 28, 2017

I declared x in the loop - for(var i=0, x; but you can miss it at all like this

function test() {

  for(var i=0; i<20000; i++)

    app.activeDocument.pathItems[0].pathPoints[0].anchor[0];

}

that doesn't matter.

Silly-V
Legend
September 28, 2017

I see. This looks like a good question for the new CEP forum - maybe someone may know the answer over there. Would you like me to move your question to the new CEP forum?