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

How to handle classes that are retained because they are extended by other classes? #35

Open
steveszc opened this issue Mar 9, 2021 · 6 comments

Comments

@steveszc
Copy link
Owner

steveszc commented Mar 9, 2021

In the case where an app/addon defines a custom base class and then extends it...

export default class Car {}
export default class Sedan extends Car {}

...

new Sedan();

The base class (Car in this example) will be retained in the heap by virtue of it being used as a prototype, even if the subclass (Sedan in this example) is not itself retained, and even if no instances of the base class itself are ever created.

@adc-mhaugen
Copy link

I too am running into this issue. I have an addon with a service. I'm extending that service in the dummy app, then testing the subclass. The super class service is retained in the heap because it's the prototype of the subclass.

@steveszc
Copy link
Owner Author

@adc-mhaugen I still haven't come up with a solution to this problem. You could add the super classes to the ignoredClasses array in config, although I'm sure you'd rather not exclude that service from memory leak detection. Maybe you could find another pattern for testing the service that doesn't require you to subclass it? I'm sorry I don't have a more ideal solution here 😞

@adc-mhaugen
Copy link

Thanks @steveszc, I'm using ignoredClasses right now. This is a great addon, thanks for developing it.

@snewcomer
Copy link

@steveszc Do you have any light reading on why this happens? Seems like a fundamental JS design problem with classes. I would think the garbage collector could identify the "unreachable objects"/not referenced object and mark + sweep them.

@kiwiupover
Copy link

kiwiupover commented Oct 26, 2021

@krisselden Mate I'm tagging you because you are super smart.

@steveszc
Copy link
Owner Author

@snewcomer I unfortunately haven't been able to find really any detailed information about why this is happening or how it could be avoided.
The ember-best-practices/memory-leaks-examples simply states the following as fact (emphasis mine):

We'll notice that the _data property is set to an object during extend. This means that it is being placed on the prototype of the class. Since protoypes are never destroyed, any changes to the _data property will stick around indefinitely.

I have not found any additional information about why prototypes are never destroyed or whether there is a way to create a destroyable prototype. That guide was originally written by Trent Willis but I'm not sure what reference material he used.

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

No branches or pull requests

4 participants