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 #489

Closed
vastray opened this issue Nov 12, 2021 · 5 comments
Closed

Memory leak #489

vastray opened this issue Nov 12, 2021 · 5 comments

Comments

@vastray
Copy link
Contributor

vastray commented Nov 12, 2021

Removed elements are not deleted from memory.

Снимок экрана от 2021-11-12 13-47-05
^^^ Nodes and Listeners are not cleared

We create the "List" controller, in it we create elements for the "Item" controller. When the button is clicked, the item is cleared and a list is created. When you click on the "Reload" button, a memory leak occurs. The garbage collector doesn't help.

Example:

index.html

<div data-controller="list">
    <button data-action="list#reload">Reload</button>
    <div data-list-target="listContainer"></div>
</div>

script.js

const data = ['name1', 'name2', 'name3', 'name4', 'name5', 'name6', 'name7', 'name8']

Stimulus.register("list", class extends Controller {

    static targets = ['listContainer']

    connect() {
        this.load()
    }

    load() {
        data.forEach((name) => {
            const div = document.createElement('div')
            div.setAttribute('data-controller', 'item')
            div.setAttribute('data-action', 'click->item#log')
            div.innerText = name
            this.listContainerTarget.appendChild(div)
        })
    }

    reload() {
        this.listContainerTarget.innerHTML = ''
        this.load()
    }
})

Stimulus.register("item", class extends Controller {

    log() {
        console.log('click')
    }

})
@tleish
Copy link
Contributor

tleish commented Nov 12, 2021

I'm pretty sure this has nothing to do with stimulus. In this instance, stimulus is the event handling for executing the javascript, it the standard javascript that creating and clearing the nodes.

It's been discussed a lot in other javascript forums:

You can read through this post for possible alternatives to innerHTML:

@vastray
Copy link
Contributor Author

vastray commented Nov 13, 2021

@tleish I noticed an interesting feature. If I remove the line:

div.setAttribute('data-action', 'click->item#log')

Nodes are removed as expected. I don't understand how to explain it.

It seems to me that the event listener is not deleted or there is a link to it somewhere. Because of this, the nodes are also not deleted.

@intrip
Copy link
Contributor

intrip commented Nov 1, 2022

@vastray #592 might have fixed this, can you still reproduce using the latest StimulusJS version?

@vastray
Copy link
Contributor Author

vastray commented Nov 1, 2022

@intrip Yes, this fixes the current bug. The garbage collector removes all unused nodes. Good job.

Снимок экрана от 2022-11-01 14-46-34

@intrip
Copy link
Contributor

intrip commented Nov 1, 2022

Great! Let's close the issue now ?

@vastray vastray closed this as completed Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants