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

[Bug] Memory leak when executing JavaScript from Rust #1489

Open
lanyeeee opened this issue Feb 17, 2025 · 0 comments
Open

[Bug] Memory leak when executing JavaScript from Rust #1489

lanyeeee opened this issue Feb 17, 2025 · 0 comments

Comments

@lanyeeee
Copy link

lanyeeee commented Feb 17, 2025

Describe the bug
There appears to be a memory leak when using webview.evaluate_script().
During testing, continuous calls to this method result in significant memory growth.
The webview process grows to approximately 870MB while the backend reaches 130MB after 2 million iterations.

Steps To Reproduce

  1. Create a new Rust project with the following dependencies:
[dependencies]
wry = { version = "0.48.1" }
tao = { version = "0.31.1" }
  1. Use the following code in main.rs:
use tao::{
    event::{Event, WindowEvent},
    event_loop::{ControlFlow, EventLoop},
    window::WindowBuilder,
};
use wry::WebViewBuilder;

fn main() -> wry::Result<()> {
    let event_loop = EventLoop::new();
    let window = WindowBuilder::new().build(&event_loop).unwrap();

    let builder = WebViewBuilder::new().with_url("https://tauri.app");

    let webview = builder.build(&window)?;
    let event_loop_proxy = event_loop.create_proxy();

    std::thread::spawn(move || {
        for i in 0..2000000 {
            // send event to the event loop
            if let Err(e) = event_loop_proxy.send_event(()) {
                eprintln!("{e}");
            }
            // sleep for a second every 10000 iterations
            if i % 10000 == 0 {
                std::thread::sleep(std::time::Duration::from_secs(1));
            }
        }
    });

    let mut i = 0;
    event_loop.run(move |event, _, control_flow| {
        *control_flow = ControlFlow::Wait;

        match event {
            Event::WindowEvent {
                event: WindowEvent::CloseRequested,
                ..
            } => {
                *control_flow = ControlFlow::Exit;
            }
            Event::UserEvent(()) => {
                i += 1;
                let _ = webview.evaluate_script(&format!(
                    r"document.querySelector('.tagline').textContent = '{i}'",
                ));
            }
            _ => (),
        }
    });
}
  1. Build and run the project:
cargo build --release
  1. Let it run until the counter approaches 2,000,000 and observe the memory usage in Task Manager.

Expected behavior
The memory usage should remain stable or have minimal growth over time when repeatedly calling evaluate_script. Instead, it continuously grows, indicating a memory leak.

Screenshots

  • Webview process memory usage 870MB
  • Backend memory usage 130MB
    Image

Platform and Versions (please complete the following information):
[✔] Environment
- OS: Windows 10.0.26100 x86_64 (X64)
✔ WebView2: 133.0.3065.69
✔ MSVC: Visual Studio Community 2022
✔ rustc: 1.84.1 (e71f9a9a9 2025-01-27)
✔ cargo: 1.84.1 (66221abde 2024-11-19)
✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)

Additional context
This issue may be the reason for tauri-apps/tauri#12724

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant