Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak #153

Closed
Tracked by #1074 ...
pixelzoom opened this issue Apr 8, 2024 · 11 comments
Closed
Tracked by #1074 ...

Memory leak #153

pixelzoom opened this issue Apr 8, 2024 · 11 comments

Comments

@pixelzoom
Copy link
Contributor

pixelzoom commented Apr 8, 2024

Reported in phetsims/qa#1066 (comment) for graphing-lines by @KatieWoe:

Two snapshots in and it already looks like there's a pretty big memory leak.

bigleak

@pixelzoom pixelzoom self-assigned this Apr 8, 2024
@pixelzoom
Copy link
Contributor Author

... and in phetsims/qa#1067 (comment) for graphing-slope-intercept by @KatieWoe:

Similar to phetsims/qa#1066, this sim has a large memory leak
morelarge

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Apr 8, 2024

I've identified a couple of common-code leaks -- NumberPicker is probably the largest contributor in this sim. But I suspect that most of the leaks are due to disposed elements that are not unlinking from LocalizedStringProperty and ProfileColorProperty.

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Apr 8, 2024

In the above commits, I fixed a couple of common-code leaks, and many sim-specific leaks related to LocalizedStringProperty and ProfileColorProperty. (These will need to be cherry picked.)

But... the heap size did not change much (see below) so there's more to do here.

Next step will be to add a listener limit to LocalizedStringProperty and ProfileColorProperty, then fuzz test to identify what is linked to them.

screenshot_3196

@pixelzoom
Copy link
Contributor Author

I added this to ProfileColorProperty:

  public override link( listener: PropertyLinkListener<Color>, options?: LinkOptions ): void {
    if ( this.tinyProperty.listeners.size > 25 ) {
      debugger;
    }
    super.link( listener, options );
  }

And I added this to LocalizedStringProperty:

  public override link( listener: PropertyLinkListener<string>, options?: LinkOptions ): void {
    if ( this.tinyProperty.listeners.size > 100 ) {
      debugger;
    }
    super.link( listener, options );
  }

Fuzz tested, and discovered a leak in AccessibleNumberSpinner (see phetsims/sun#879) and a few more sim-specific leaks (fixed in e6a8bbf).

I tested with this uncommitted patch to AccessibleNumberSpinner:

Subject: [PATCH] various memory leaks related to LocalizedStringProperty and ProfileColorProperty, https://github.com/phetsims/graphing-lines/issues/153
---
Index: js/accessibility/AccessibleNumberSpinner.ts
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/js/accessibility/AccessibleNumberSpinner.ts b/js/accessibility/AccessibleNumberSpinner.ts
--- a/js/accessibility/AccessibleNumberSpinner.ts	(revision 75e22745c05481d1d6b3e80c8d7d2138563df27a)
+++ b/js/accessibility/AccessibleNumberSpinner.ts	(date 1712621584033)
@@ -180,6 +180,7 @@
         this.pdomDecrementDownEmitter.dispose();
 
         this.removeInputListener( accessibleInputListener );
+        this.removePDOMAttribute( 'aria-roledescription' );
       };
     }

Heap size is much better, but it does not yet stabilize or go down:

screenshot_3198

@pixelzoom
Copy link
Contributor Author

I identified a few more things that were observing Line.color, which is TColor and may therefore be a Property.

Heap size now stabilizes at ~102 MB for graphing-lines:

screenshot_3199




It's less clear when graphing-slope-intercept stabilizes, but heap size did decrease twice, so I think this is OK:

screenshot_3201




So after the leak in AccessNumberSpinner (or ParallelDOM) is fixed in phetsims/sun#879, we should be good to go.

Next is to cherry pick the sim-specific and common-code commits added above.

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Apr 9, 2024

Here's what needs to be cherry picked so far, from above commits.

scenery-phet

sun

graphing-lines

pixelzoom added a commit that referenced this issue Apr 9, 2024
pixelzoom added a commit that referenced this issue Apr 9, 2024
pixelzoom added a commit that referenced this issue Apr 9, 2024
pixelzoom added a commit to phetsims/scenery-phet that referenced this issue Apr 9, 2024
pixelzoom added a commit to phetsims/sun that referenced this issue Apr 9, 2024
pixelzoom added a commit to phetsims/scenery-phet that referenced this issue Apr 9, 2024
pixelzoom added a commit to phetsims/sun that referenced this issue Apr 9, 2024
pixelzoom added a commit that referenced this issue Apr 9, 2024
pixelzoom added a commit that referenced this issue Apr 9, 2024
pixelzoom added a commit that referenced this issue Apr 9, 2024
pixelzoom added a commit to phetsims/graphing-slope-intercept that referenced this issue Apr 9, 2024
@pixelzoom
Copy link
Contributor Author

pixelzoom commented Apr 9, 2024

In phetsims/sun#879 (comment), @jessegreenberg noted:

For graphing-lines, it does look like RichDragListeners can be created and not disposed for the 'reward' views. So I probably started to see the leak after a while because the fuzzer happened to win a game.

Ugh. I see that RichDragListener needs to be disposed because it owns 2 SoundClips that are added to soundManager. And this sim has 6 subclasses of RichDragListener.

The game reward (GLRewardNode) also looks like its leaking, because some of the rewards are linked to LocalizedStringProperty (eg "x" and "y" in the equations).

I'll tackle those next.

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Apr 9, 2024

Over in phetsims/sun#879, I confirmed that there's nothing wrong with disposal of AccessibleNumberSpinner, so no changes forthcoming for that.

After the above disposal of RichDragListener and GLRewardNode, there's more improvement. I tested with unbuilt, heap snapshots at 1-minute intervals, 10 minutes between snapshots 10 & 11.

http://localhost:8080/graphing-lines/graphing-lines_en.html?brand=phet&ea&fuzz&debugger
screenshot_3204

http://localhost:8080/graphing-slope-intercept/graphing-slope-intercept_en.html?brand=phet&ea&fuzz&debugger
screenshot_3205

pixelzoom added a commit that referenced this issue Apr 9, 2024
pixelzoom added a commit that referenced this issue Apr 9, 2024
(cherry picked from commit 6abe67c)
pixelzoom added a commit that referenced this issue Apr 9, 2024
pixelzoom added a commit that referenced this issue Apr 9, 2024
pixelzoom added a commit that referenced this issue Apr 9, 2024
(cherry picked from commit 6abe67c)
pixelzoom added a commit that referenced this issue Apr 9, 2024
pixelzoom added a commit to phetsims/graphing-slope-intercept that referenced this issue Apr 9, 2024
@pixelzoom
Copy link
Contributor Author

Cherry picks for RichDragListener and GLRewardNode leaks were done above.

This is ready for testing in the next RC.

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Apr 22, 2024

Ready for review in GL 1.4.0-rc2 and GSI 1.2.0-rc2. Please close this issue if it tests OK.

Below are my tests for the RC2 built versions. I took a heap snapshot every 1 minute, until the heap sized stabilized.

https://phet-dev.colorado.edu/html/graphing-lines/1.4.0-rc.2/phet/graphing-lines_en_phet.html?fuzz
screenshot_3241

https://phet-dev.colorado.edu/html/graphing-slope-intercept/1.2.0-rc.2/phet/graphing-slope-intercept_en_phet.html?fuzz
screenshot_3240

@Nancy-Salpepi
Copy link

Memory tests look good. Snapshots were taken at 1 minute intervals, with 10 minutes between snapshots 10 and 11.

Graphing Lines 1.4.0-rc.2:
Screenshot 2024-04-23 at 3 32 19 PM

Graphing Slope Intercept 1.2.0-rc.2:
Screenshot 2024-04-23 at 3 55 16 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants