-
Notifications
You must be signed in to change notification settings - Fork 339
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
task::spawn cause memory leak #702
Comments
Can you try the following? task::spawn(async {});
for _ in 0..100 {
task::block_on(async {
let url = "https://***.mp3";
download_music(url, "***.mp3").await.ok();
});
} And also this? for _ in 0..100 {
task::spawn(async {
let url = "https://***.mp3";
download_music(url, "***.mp3").await.ok();
})
.await;
} I wonder if those two pieces of code also exhibit the memory leak? |
Can you quantify "significant"?
Do you see a leak per spawn? |
@stjepang Have tested your code, the former did not find a memory leak, the latter still has a memory leak! |
@skade Hello, not every time spawn is called. File 2 According to observations, not every spawn call will increase the fixed memory. |
Can you perhaps try running with a different memory allocator? Two suggestions: I wonder if the memory use is simply due to the allocator lazily returning memory back to the OS? |
@stjepang mimallocator |
I found that the following code also causes a memory leak task::spawn(async {
for _ in 0..100 {
let url = "https://***.mp3";
download_music(url, "***.mp3").await.ok();
}
}); |
Okay, I think I see what is happening. The allocator has a separate memory pool for each thread. At first, memory usage is low because the task is running on just one thread. However, then another thread steals the task and keeps running it. Every time a new thread steals the task, the allocator will initialize its thread-local memory pool, and thus the total memory usage keeps growing. So this is not really a memory leak, it's just how allocators work. I think the new scheduler would exhibit lower total memory usage: #631 |
@stjepang Thanks i think i understand. |
I have a download function:
Executing with task :: spawn will cause a significant increase in memory usage and will not be released after the download is complete.
With task :: block_on there is no memory leak.
The text was updated successfully, but these errors were encountered: