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

Excessive memory usage #672

Open
cathaysia opened this issue Feb 17, 2025 · 5 comments
Open

Excessive memory usage #672

cathaysia opened this issue Feb 17, 2025 · 5 comments

Comments

@cathaysia
Copy link

When I recursively watch a folder(37323 files), the memory usage is too large. 64MB of memory needs to be allocated at a time. In addition, the vec memory growth strategy is too conservative, causing vec to keep trying to allocate memory:

Image

Image

please notic the slider:

Image

Image

@davidhalter
Copy link

davidhalter commented Feb 18, 2025

If you are on Linux you should expect that a watch uses 1kB of memory (source https://unix.stackexchange.com/questions/13751/kernel-inotify-watch-limit-reached). I'm not sure this still applies to the latest kernel and you might be on MacOs, but I assume 64 MB of memory is not entirely wrong (it would be at least 32 MB), especially when you consider that inotify also uses some state internally per watch.

Other people should probably weigh in on the "vec memory growth strategy", but I doubt that this is an issue compared to the overhead of a sys call (or at least not making notify slower by a huge amount).

@cathaysia
Copy link
Author

cathaysia commented Feb 19, 2025

Actually the memory allocation happens here instead of inside inotify:

let mut queues_remaining = HashMap::with_capacity(self.queues.len());

I tried replacing it with BTreeMap, but the memory usage was only reduced to about 57MB, and the cost was that one memory allocation was replaced by tens of thousands of memory allocations. Is there any other way to reduce the memory usage? (such as use raidx tree, etc.)

vec memory growth strategy

The memory allocation of vec is quite conservative(https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#2417). I'm not sure how Rust makes such decisions in the first place, but can Vec::with_capacity be used here to reduce the number of memory allocations?

@dfaust
Copy link
Member

dfaust commented Feb 19, 2025

@cathaysia Can you please provide more information about how you are using notify. Maybe you could create a minimal test case, that watches a folder and generates FS events (like examples/debouncer_full.rs).
Also, the memory leak issue you raised in the linked Tauri issue seems far more severe. Can you provide some more details about that (maybe even a minimal test case).
It's also very important to know what OS you are using. Since you are using Tauri, you are also using the recommended file id cache. And if you are using something other than Linux, this means the path of every file is stored in memory.
And what tools are you using to profile the memory usage?

@cathaysia
Copy link
Author

I'm using notify in this repo: https://github.com/cathaysia/finalgit/tree/dev. If you are not in a hurry, I will try to give a minimal reproducible example

When you select a git repository, it will automatically recursively watch the entire git repository (yes, .gitignore is not followed here).

If you refresh the page a few times, you will see that the memory usage is growing.

I currently tested on Windows, Linux and MacOS. Windows performs best (the program does not lag too much), while Linux (Fedora) performs worst and is basically inoperable.

I mainly use MacOS instruments when analyzing memory. It is really convenient.

@cathaysia
Copy link
Author

Memory leaks may be beyond the scope of this issue. The main purpose of this issue is to reduce memory allocation times and memory usage when monitoring a large number of files.

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

No branches or pull requests

3 participants