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 leaks when using newInstance to create multiple hljs instances #4086

Closed
immccn123 opened this issue Aug 1, 2024 · 8 comments · Fixed by #4095
Closed

Memory leaks when using newInstance to create multiple hljs instances #4086

immccn123 opened this issue Aug 1, 2024 · 8 comments · Fixed by #4095
Labels
bug help welcome Could use help from community parser

Comments

@immccn123
Copy link
Contributor

Describe the issue/behavior that seems buggy

Calling newInstance multiple times poses a risk of memory leaks.

Sample Code or Instructions to Reproduce

see remarkjs/react-markdown#791 (comment)

Expected behavior

newInstance should not call window.addEventListener('DOMContentLoaded', boot, false).

Additional context

In function HLJS:

if (typeof window !== 'undefined' && window.addEventListener) {
window.addEventListener('DOMContentLoaded', boot, false);
}

Related:

remarkjs/react-markdown#791 rehypejs/rehype-highlight#31

@immccn123 immccn123 added bug help welcome Could use help from community parser labels Aug 1, 2024
@immccn123 immccn123 changed the title Memory leaks when using newInstance Memory leaks when using newInstance to create multiple hljs instances Aug 1, 2024
@joshgoebel
Copy link
Member

I can see the issue here, though newInstance is not really intended to be used more than a few times (for custom usage scenarios) - there is a high startup cost if you are using lots grammars. React should not be calling it for say, every render loop. Instead some parent component should likely hold onto a single instance and use that for most rendering.

@joshgoebel
Copy link
Member

How would anyone suggest we fix this, track some global state on window itself, or?

@immccn123
Copy link
Contributor Author

immccn123 commented Aug 3, 2024

I don't think a standalone hljs instance should have global side effects. Perhaps an additional configuration option would be better, but I'm not sure if this would break highlight.js and it might increase the bundle size.

It seems like that the operations on the window object are only these.

@joshgoebel
Copy link
Member

What if this code was moved inside the conditional portion of highlightAll() such that it's only called if one requests highlighting of everything?

@immccn123
Copy link
Contributor Author

What if this code was moved inside the conditional portion of highlightAll() such that it's only called if one requests highlighting of everything?

Seems like a good idea

@erha19
Copy link

erha19 commented Sep 18, 2024

@immccn123 is there any new version for this fix?

@callie-openai
Copy link

@joshgoebel @immccn123 Also curious about this, saw the updated CHANGES.md for 11.11.0 but no associated publish to npm yet.

@joshgoebel
Copy link
Member

Will try to get a few more of the open PRs resolved and then do a release after that. Wanting the next 11 release to be the last and then switch over to v12 betas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help welcome Could use help from community parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants