Skip to content

Add memory_used api and related tools#78

Merged
arthurprs merged 1 commit into
arthurprs:masterfrom
virusdefender:feat/memory-used
Oct 6, 2025
Merged

Add memory_used api and related tools#78
arthurprs merged 1 commit into
arthurprs:masterfrom
virusdefender:feat/memory-used

Conversation

@virusdefender

@virusdefender virusdefender commented Aug 20, 2025

Copy link
Copy Markdown
Contributor

In practice, it is important to be able to estimate the memory usage of a cache in advance and to monitor its memory consumption in real time. Therefore, the memory_used API has been added.

However, it currently has a few issues:

  • If the cache's key or value is a data structure like Vec<T> that is actually allocated on the heap, this calculation will be inaccurate.
  • MemoryUsed exposes some internal implementation details, which may change in the future.

To verify the accuracy of the calculation, I've added examples/memory_used_plot.rs. This program plots our estimated memory usage alongside the memory usage of the process as observed at the operating system level. As shown in the figure below, our estimated values are highly accurate.

image

The cache's maximum capacity is 20,000,000, and the ghost queue by default allocates half of that capacity, resulting in a total of 30,000,000.

@arthurprs arthurprs left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Do you think exposing the split between map and entries is helpful?

Comment thread Cargo.toml Outdated
Comment thread src/lib.rs
@virusdefender

Copy link
Copy Markdown
Contributor Author

Thank you! Do you think exposing the split between map and entries is helpful?

I'm also a bit concerned about this issue. Perhaps another approach would be to add a separate feature, something like "unstable", which by default only allows the use of total(), and then enable this feature in memory_used_plot.

@arthurprs

Copy link
Copy Markdown
Owner

It might be best to return a single usize. Which I expect to be sufficient for most, if not all use cases.

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

Successfully merging this pull request may close these issues.

2 participants