-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
High memory usage when invoking commands [bug] #4026
Comments
Oh didn't see you closed it. When we talked about it on discord i was able to reproduce this on windows too, although it felt less severe. I'm gonna test it again later today with this specific repro. Edit: Thought about this issue again because of #4038 btw |
From what @wusyong said all platform webviews have some kind of memory leak issue (webview2 has it even in the custom protocol stuff). |
Does it leak "in" the rust process or one of the webview ones, because on windows it was the rust process that had increasing memory usage (well, the webview did too but idc about that). Buuut it wasn't that severe iirc, like i couldn't get it to 12gigs like in the screenshot. Will re-test later or tomorrow anyway just to be sure. Oh and now that i think about it, opening a file picker dialog increases the rust's process memory usage by 100mb and for whatever reason this won't get cleaned up completely until you exit the app. (Subsequent dialogs don't increase them that much so i guess this is just how it works, memory is meant to be used after all, so i guess it's windows itself not cleaning up if it doesn't see it as necessary?) |
The Linux leak was in the webkit process itself. I didn't test the macOS one yet. |
In my test for the HTTP memory leak the Rust process didn't increase its memory usage, but the webview2 one did (a lot), in less than a minute it was using an extra 300MB. |
Do y'all have one or more upstream issues to track? Memory leaks are nasty, and can cover up more leaks being added. Rust is not immune to it either, it's fairly easy to accidentally create ref cycles. I've had some experience tracking those down so let me know if you're interested in techniques. Also, workarounds can help. For instance in MicrosoftEdge/WebView2Feedback#1633 it appears to only accumulate when the webview is attached, so if closing + creating new window is better than hide + show, it's worth knowing as a Tauri user. |
@lucasfernog are you sure this is not like #852 ? When looking at dangling references to leaked http response objects it looked like they were from some closures originating in cmd invoke wrappers but I haven't looked at not minimized code. |
The one i'm currently tracking is MicrosoftEdge/WebView2Feedback#2171 not sure about webkit issues yet. |
I could reproduce the issue with only the eval function:
seems like the webview doesn't handle it very well. |
It also memory leak on Windows. |
@lucasfernog I tried your suggestion in #3921 (comment) to use http server as bypass invoke system. It does NOT help, memory for http response objects is still leaking. If I understand correctly this completely bypasses the rust->webview messaging and should not have the same root cause as the webview bug linked above. Am I missing something or are there 2 separate issues here? Minimal repro attached: |
Also, a memory leak happens when you emit an event to a window in a loop. During my investigation, I found out that if you emit an event to the window but on the window, there is no listener registered there will be no memory leak. But if a listener is registered, a memory leak occurs. |
Ok, I'm fairly certain now that I have proof of memory leak in tauri code, not webview. I modified the repro I linked in comment above. Now it has 2 http fetch variants: they both go through localhost server but one is via tauri invoke system and another is directly proxying requests. First one leaks memory while the second one doesn't. |
The leak happening in the tauri code doesn't mean it is not a webview leak, the invoke system use webview APIs (we already traced the issue to the |
Does |
Since we know we have a memory leak when calling eval, I will suggest that we should update the invoke system to use WebSockets to create some kind of RPC system. Thinking out loud. |
tauri-invoke-http doesn't use eval - it uses a localhost server. |
Can we upgrade this to use WebSocket instead of HTTP? Maybe I fork the repo and see what I can do. |
Hey just copy it and create |
And it leaks memory. So, like I said, 2 independent memory leaks. See my last attachment in #4026 (comment) |
If you're using Windows it's the MicrosoftEdge/WebView2Feedback#2171 issue. webkit might have a similar problem too, i didn't check it yet. |
@qu1ck use https://github.com/ahkohd/tauri-awesome-rpc Works like charm. Also avoid using tauri APIs that uses eval under the hood such as window.emit(). Use AwesomeEmit and AwesomeEvent instead. |
@ahkohd your invoke implementation does not help. Lucas is claiming that it's yet another issue not related to eval leak. It's not obvious to me how c++/rust land streams have effect on javascript objects that were created entirely in javascript but here we are. |
#4274 doesn't fix this issue, but it helps a little bit at least with a problem on our side. |
I I think I am experiencing the same bug as well on windows. In my case the rust part sends events at a very high rate 20K events/second. Then these events are rendered using chartjs. The memory consumption grows at about 5Mb per second and the application cannot stay up for more then few minutes. |
I think, there is another problem in wry (wkwebview mod). Rust calls OC using objc crate, an NSString (which is the invoke response data that will be posted to webview via eval method) allocs in rust and never be released in the correct way so that memory usage keeps increasing in the Tauri process. |
Good day @lucasfernog, Since we see that the discussions in several threads are not really promising, varying from blaming webview2 to poking in the dark. Best |
MicrosoftEdge/WebView2Feedback#2171 (comment) |
Hello @lzdyes, |
The issue 2171 was only about the |
Hello! I understand that the Tauri team is saying this is a leak related to the webview2 process (the issue different from the asset: protocol), but does there exist a github issue / ticket open in webview2feedback repo regarding this, focused on the invoke/eval system? I see that you guys keep mentioning that webview2#2171 is unrelated, but then where exactly is the relevant issue reported then? |
Indeed, i mean everything is possible but if there is a bug in webview2 where is the ticket and where is de precise explanation why this bug is causing the memory in a tauri app to explode every time you try to send something big? I know stupid annoying user but if anybody would have mercy with me and gives an explanation, i can create a test application to show it also |
That would be nice because i'm currently looking into it to double check if it's really not us leaking memory, and well, i can't get it to leak memory at all anymore. The only leak-like behavior shows up when you have the devtools open until you close them again. Well, that's not even "leak-like", but i don't find a better word to describe it. Edit: Talking about Windows only right now. Edit2: I do see an increase in memory while the invoke/eval/events stuff running, but it doesn't endlessly increase, just the normal GC increase/delay. |
@FabianLars I added a repro in #4026 (comment) |
@MaximilianLiebsch What is the amount of data in bytes you are trying to send via I cannot seem to reproduce this issue that is being mentioned here when trying to send ~500 KB of string data from Rust, every second. What I've tested: Have a button with On Windows after compiling the app for release, memory for the whole application starts out at ~70 MB, and peaks at 160 MB where the GC brings it down to 120. I've been running this for 30 minutes straight. |
Can you guys also include the windows webview version you are using? @qu1ck @MaximilianLiebsch |
Webview 104.0.1293.47. |
The command system uses eval internally FYI. |
Your post earlier:
|
Ahh sorry I didn't see you're using the HTTP invoke. |
Is this Windows only now? We fixed a few issues on macOS IIRC. |
Sorry for the drlays guys. I will find time this week to make a test app for Windows. |
@MaximilianLiebsch , did you manage to get a minimum example test app created? |
I have same issue on macOS. |
I worked around this bug by routing all data heavy requests through a localhost server instead of going through tauri command api. |
The issue still remains unresolved. Is there any progress on this? |
I can confirm we are having the same issue, any updates? |
Is there an update to this question? |
Describe the bug
High memory usage when sending lots of commands for rust from js.
Reproduction
Invoke a command in an interval:
Return large amount of data from rust to js:
After several thousand invocations, watch memory usage creep up:
Eventually, when memory pressure is sufficient, the usage plateaus. It appears that garbage collection runs regularly, reducing the memory usage of both the processes that hog memory.
Expected behavior
I would expect the app to run without hogging memory resources.
Platform and versions
Stack trace
Additional context
The
get_data
command only needs 5ms on my machine. That means the interval is not running too fast, since it has > 40ms of rest time between invocations.The text was updated successfully, but these errors were encountered: