tauri: High memory usage when invoking commands [bug]

Describe the bug

High memory usage when sending lots of commands for rust from js.

Reproduction

Invoke a command in an interval:

setInterval(() => {
  invoke<number[][]>('get_data', args).then(drawOnHtmlCanvasElement)
}, 50)

Return large amount of data from rust to js:

#[tauri::command]
fn get_data(path: &str, start: u64, length: usize, filters: Vec<FilterConfig>) -> Vec<Vec<f32>> {
    vec![vec![0.0; 2560]; 21]
    // data::get(path, start, length, filters)
}

After several thousand invocations, watch memory usage creep up: image

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

> tauri-app@0.1.0 tauri
> tauri "info"


Environment
  › OS: Mac OS 12.2.1 X64
  › Node.js: 16.13.1
  › npm: 8.1.2
  › pnpm: Not installed!
  › yarn: Not installed!
  › rustup: 1.24.3
  › rustc: 1.57.0
  › cargo: 1.57.0
  › Rust toolchain: stable-aarch64-apple-darwin 

Packages
  › @tauri-apps/cli [NPM]: 1.0.0-rc.9
  › @tauri-apps/api [NPM]: 1.0.0-rc.4
  › tauri [RUST]: 1.0.0-rc.8,
  › tauri-build [RUST]: 1.0.0-rc.7,
  › tao [RUST]: 0.8.3,
  › wry [RUST]: 0.15.1,

App
  › build-type: bundle
  › CSP: unset
  › distDir: ../build
  › devPath: http://localhost:3000/
  › framework: React

App directory structure
  ├─ node_modules
  ├─ public
  ├─ src-tauri
  ├─ build
  ├─ .git
  └─ src

Stack trace

N/A

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.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 52 (27 by maintainers)

Most upvoted comments

I can confirm we are having the same issue, any updates?

@ahkohd your invoke implementation does not help.

image

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.

Is there an update to this question?

Hey just copy it and create tauri-invoke-ws 😂 should be easy

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 eval function, which the invoke system uses).

@lucasfernog I tried your suggestion in https://github.com/tauri-apps/tauri/issues/3921#issuecomment-1115089647 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: httpleakrepro.zip