# PsychoJS platform version 2020.1 - Memory Leak in Visual Stimulus Setters

**URL:** <https://discourse.psychopy.org/t/psychojs-platform-version-2020-1-memory-leak-in-visual-stimulus-setters/14571>\
**Category:** Online experiments\
**Created:** [June 26, 2020, 8:49am UTC](https://discourse.psychopy.org/t/psychojs-platform-version-2020-1-memory-leak-in-visual-stimulus-setters/14571 "2020-06-26T08:49:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![frank.papenmeier](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@frank.papenmeier](https://discourse.psychopy.org/u/frank.papenmeier)\
**Post date:** [June 26, 2020, 8:49am UTC](https://discourse.psychopy.org/t/psychojs-platform-version-2020-1-memory-leak-in-visual-stimulus-setters/14571/1 "2020-06-26T08:49:09Z")

</div>

There is a memory leak in PsychoJS platform version 2020.1 caused by the stimulus setters and the subsequent call to the \_updateIfNeeded() not releasing the resources of the attached \_pixi object. I confirmed and fixed this issue for both TextStim and ImageStim but the same issue is very likely also present for other visual components (I did not check this).

I created two public demo experiments containing only a text stimulus that updates its text content every frame (the actual content stays the same but the update calls the setText function in turn triggering the \_updateIfNeeded function). This is to demonstrate that this memory leak is particularily problematic when using “set every frame”.

URL of experiment using the current PsychoJS platform version 2020.1:  
[https://pavlovia.org/run/frank.papenmeier/memory-leak-visual-stimulus-setters/html/](https://pavlovia.org/run/frank.papenmeier/memory-leak-visual-stimulus-setters/html/)

URL of experiment with my bugfix applied to the current PsychoJS platform version 2020.1:  
[https://pavlovia.org/run/frank.papenmeier/memory-leak-visual-stimulus-setters-with-bugfix/html/](https://pavlovia.org/run/frank.papenmeier/memory-leak-visual-stimulus-setters-with-bugfix/html/)

This is how to fix the bug in PsychoJS platform version 2020.1:

The PIXI objects created by the PsychoJS visual library need to be released before the creation of a new object. This can be done by calling the .destroy(true) function on respective objects within the \_updateIfNeeded() function.

For TextStim.js, for example, I add the following code

```auto
if (typeof this._pixi !== 'undefined')
    this._pixi.destroy(true);

```

just before the creation of the new PIXI stimulus  
`this._pixi = new PIXI.Text(this._text, { ... and so on`

The same is true for ImageStim (see also the html/lib\_bugfix/visual-2020.1.js within the second project linked above).

---

<div class="post-metadata">

**Author:** ![unagi\_pie](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/unagi_pie/32/3111_2.png) [@unagi\_pie](https://discourse.psychopy.org/u/unagi_pie)\
**Post date:** [June 27, 2020, 10:05pm UTC](https://discourse.psychopy.org/t/psychojs-platform-version-2020-1-memory-leak-in-visual-stimulus-setters/14571/2 "2020-06-27T22:05:11Z")

</div>

Thanks so much! I was wondering what was causing these crashes in my typing experiments. Glad someone with more skill was able to solve the issue.

This should be included in the next update.

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [June 28, 2020, 5:03am UTC](https://discourse.psychopy.org/t/psychojs-platform-version-2020-1-memory-leak-in-visual-stimulus-setters/14571/3 "2020-06-28T05:03:09Z")

</div>

Hi Frank, well done to chase this down.

Have you reported it as an issue (along with the suggested fix) at the PsychoJS Github repository? It make it much more likely to get actioned compared to posting here, where things can easily get lost or not come to the right person’s attention.

> **[psychopy/psychojs](https://github.com/psychopy/psychojs/issues)**
>
> PsychoJS is the online counterpart of the PsychoPy Python library. - psychopy/psychojs

---

<div class="post-metadata">

**Author:** ![frank.papenmeier](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@frank.papenmeier](https://discourse.psychopy.org/u/frank.papenmeier)\
**Post date:** [June 28, 2020, 8:44am UTC](https://discourse.psychopy.org/t/psychojs-platform-version-2020-1-memory-leak-in-visual-stimulus-setters/14571/4 "2020-06-28T08:44:37Z")

</div>

Hi Michael,

good point! Following your suggestion, I just opened an issue at Github:

> <https://github.com/psychopy/psychojs/issues/97>
>
> This is a copy of my PsychoPy Discourse entry to increase the likelihood that this issue gets attention and that it...

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [June 28, 2020, 9:07am UTC](https://discourse.psychopy.org/t/psychojs-platform-version-2020-1-memory-leak-in-visual-stimulus-setters/14571/5 "2020-06-28T09:07:42Z")

</div>

Thanks Frank. The cause/fix matches what I expected but great that you tracked down a particular fix. We’ll make sure this is fixed in the 2020.2 series.

---

<div class="post-metadata">

**Author:** ![ilharris](https://avatars.discourse-cdn.com/v4/letter/i/e79b87/32.png) [@ilharris](https://discourse.psychopy.org/u/ilharris)\
**Post date:** [July 1, 2020, 5:40pm UTC](https://discourse.psychopy.org/t/psychojs-platform-version-2020-1-memory-leak-in-visual-stimulus-setters/14571/6 "2020-07-01T17:40:07Z")

</div>

Hey Frank,

Thanks a lot for providing this fix! I’ve updated my experiment with your bug\_fix lib and updated the import statements in my experiment’s main .js file; this has minimized crashing/freezing resulting from this memory leak for most participants. That said, correct me if I’m wrong, but the fix doesn’t seem to apply to the .js file used for older browsers. Do you happen to know of an alternative solution for older browsers? Memory leakage via text components is the main issue in my experiment for participants with older operating systems and browsers.

Thanks a lot,  
Ilana

---

<div class="post-metadata">

**Author:** ![frank.papenmeier](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@frank.papenmeier](https://discourse.psychopy.org/u/frank.papenmeier)\
**Post date:** [July 2, 2020, 8:02am UTC](https://discourse.psychopy.org/t/psychojs-platform-version-2020-1-memory-leak-in-visual-stimulus-setters/14571/7 "2020-07-02T08:02:42Z")

</div>

Hi Ilana,

yes, this is correct. This should fix the issue for recent browsers only. For our experiment, this was sufficient because we only allow users using either Firefox or Chrome to participate and basically all of our participants use recent enough versions of both browsers.

Thus, I did not look into the legacy version. However, according to Jon (see Github thread), version 2020.2 is around the corner and will include a fix. Thus, my best solution would be to allow only recent browsers for now until 2020.2 is released.

All the best,

Frank.

---

<div class="post-metadata">

**Author:** ![ilharris](https://avatars.discourse-cdn.com/v4/letter/i/e79b87/32.png) [@ilharris](https://discourse.psychopy.org/u/ilharris)\
**Post date:** [July 2, 2020, 5:19pm UTC](https://discourse.psychopy.org/t/psychojs-platform-version-2020-1-memory-leak-in-visual-stimulus-setters/14571/8 "2020-07-02T17:19:59Z")

</div>

Ok, good to know. On the edge of my seat for 2020.2! Thanks for your help thus far, and best of luck with your own experiments.

Ilana

---

<div class="post-metadata">

**Author:** ![benwang](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/benwang/32/9733_2.png) [@benwang](https://discourse.psychopy.org/u/benwang)\
**Post date:** [July 19, 2020, 4:03pm UTC](https://discourse.psychopy.org/t/psychojs-platform-version-2020-1-memory-leak-in-visual-stimulus-setters/14571/9 "2020-07-19T16:03:12Z")

</div>

Thank you so much! This worked wonders. We were experiencing an issue where after 40 minutes of a basic task involving a simple recall and response (the user had to type and we would update the text every frame), memory usage went from 170 KB up to 1.5 GB.

Using Frank’s lib\_bugfix, I’ve created a Python script that automates the update process, so all you have to do is place this in the root of your experiment (the folder containing the html/ folder) and then run it after each HTML export and before you sync. Note that on Windows, if your environment PATH isn’t setup right, you may have to run the script from a command prompt instead of just double clicking.

[ApplyMemoryLeakBugfix.py](https://discourse.psychopy.org/uploads/short-url/xNxaUFqfSnf07HU8w40Bmnok1Qt.py) (179.8 KB)

Also, here’s the results:  
 ![Memory_Footprint](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/1/7/17a27a560a0b7285d991a855fd2ab15f107c4609.png) ![Leak_Rate](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/8/0/80552312cda336b9ad1c0c2b6f53ea1afddbc2db.png)
