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

Keys occupy double memory size after updating #473

Open
zhu-he opened this issue Dec 23, 2024 · 1 comment
Open

Keys occupy double memory size after updating #473

zhu-he opened this issue Dec 23, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@zhu-he
Copy link

zhu-he commented Dec 23, 2024

Running the following code will use 2GB of memory.

let cache = moka::future::Cache::new(10);
cache.insert(vec![1u8; 1 << 30], ()).await;
cache.insert(vec![1u8; 1 << 30], ()).await;

Testable:

let cache = moka::future::Cache::new(10);
let key = std::sync::Arc::new(());
cache.insert(key.clone(), ()).await;
cache.insert(key.clone(), ()).await;
assert_eq!(std::sync::Arc::strong_count(&key), 2);
@cfeitong
Copy link

The updated key is not dropped immediately; instead, its drop is deferred until a later point in time. This behavior is expected in concurrent data structures. To achieve optimal performance, such data structures often rely on some form of garbage collection. In the case of moka, it uses crossbeam-epoch as the underlying garbage collection mechanism.

To address your test case, you can perform additional random operations on the cache. This allows crossbeam-epoch to advance its internal epoch, which will then reclaim unused garbage, including the updated key.

Here is an updated version of your test:

    let key = std::sync::Arc::new(1);
    let cache = moka::future::Cache::new(10);
    cache.insert(key.clone(), ()).await;
    cache.insert(key.clone(), ()).await;

    // Perform some random operations to advance the epoch.
    for i in 0..100 {
        let key_i = std::sync::Arc::new(i);
        cache.insert(key_i.clone(), ()).await;
    }

    // Verify that the strong count is as expected.
    assert_eq!(std::sync::Arc::strong_count(&key), 2);

@tatsuya6502 tatsuya6502 added the documentation Improvements or additions to documentation label Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants