wailsapp / wails

Create beautiful applications using Go
https://wails.io
MIT License
25.35k stars 1.22k forks source link

[v2] Event Memory Leak Problem #1217

Closed mostend closed 2 years ago

mostend commented 2 years ago

this is simple clock program

Env :

System
------
OS:     MacOS
Version:    12.2.1
ID:     199506
Go Version: go1.17.8
Platform:   darwin
Architecture:   amd64

Dependency          Package Name    Status      Version
----------          ------------    ------      -------
xcode command line tools    N/A     Installed   2392
npm                 N/A     Installed   8.5.1
*upx                N/A     Installed   upx 3.96
*nsis               N/A     Available

html:

<p id="info"></p>

js:

let info = document.getElementById("info")

runtime.EventsOn("test",function (data){
    info.innerHTML = data
})

go:

func (b *App) domReady(ctx context.Context) {
    // Add your action here
    go func() {
        timeTickerChan := time.Tick(time.Millisecond)
        for {
            runtime.EventsEmit(b.ctx, "test", time.Now())
            <-timeTickerChan
        }
    }()

}

At this time, memory will rise very quickly, and continues to increase. from 24MB to 39MB and higher It's a lot like a memory leak issue.

image

leaanthony commented 2 years ago

What makes you think this is a problem and not just normal Go memory management? Did you look for where the problem is?

mostend commented 2 years ago

yes, I've tried many times, and when you keep calling events, memory keeps going up and doesn't go down. Although I changed timer to time.Second.

It even ended up with memory go to 1G and higher. This problem is not a memory rise quickly problem, it's very much like a memory leak problem.

Many thanks.

leaanthony commented 2 years ago

Yeah right. I'll have a look. Thanks for investigating 👍

leaanthony commented 2 years ago

Looks like it might be a ticker issue? https://developpaper.com/question/golang-time-newticker-memory-leak/

stffabi commented 2 years ago

I suspect it might really be a memory leak in the native code. Shouldn't we release the NSString of the script, after it has been executed on the main thread?

https://github.com/wailsapp/wails/blob/c5c0617c8924d580b02bfc659f35d0a669e5d15c/v2/internal/frontend/desktop/darwin/Application.m#L65-L71

leaanthony commented 2 years ago

Nice catch. It should be getting released by the ExecJS method on the main thread.

leaanthony commented 2 years ago

@waini1110 - Please test the PR. Thanks 👍