Skip to content

Feature: Add compact() method to reclaim memory from deleted vectors (tombstones) #726

Closed as not planned

Description

@anaslimem

Describe what you are looking for

Currently, when a vector is removed from a usearch::Index using .remove(), the node is marked as a tombstone, but the underlying memory is never actually freed back to the OS.
In long-running, write-heavy applications (like vector databases or AI agent memory systems), this leads to severe memory bloat. For example, if millions of embeddings are added and subsequently removed over time,
usearch will hold gigabytes of unused RAM even if the active vector count is very low. I am looking for a native compact() or shrink_to_fit() method that drops these tombstones and reorganizes the internal graph to actually free up memory, avoiding the need to manually build a brand-new index and stream all the alive vectors into it just to reclaim RAM.

What i am looking for:
A method on the index (index.compact()) that performs in-place memory reclamation.

let mut index = usearch::new_index(&options).unwrap();

// Insert a large number of vectors
for i in 0..1_000_000 {
    index.add(i, &vec![0.0; 128]).unwrap();
}

// Remove 90% of them
for i in 0..900_000 {
    index.remove(i).unwrap();
}

// At this point, [usearch](cci:1://file:///Users/limemanas/Desktop/projects/CortexaDB/crates/cortexadb-core/src/index/hnsw.rs:30:4-35:5) still holds RAM for 1,000,000 vectors.

// Feature request: A way to reclaim the memory in-place
index.compact().unwrap(); 

// Now [usearch](cci:1://file:///Users/limemanas/Desktop/projects/CortexaDB/crates/cortexadb-core/src/index/hnsw.rs:30:4-35:5) should only actively hold RAM for the 100,000 remaining vectors.

If pure in-place compaction compromises search latency too much during the operation, even an officially supported internal rebuild/swap utility that safely drops the unreferenced memory would be highly beneficial.

Can you contribute to the implementation?

  • I can contribute

Is your feature request specific to a certain interface?

Other bindings

Contact Details

[email protected]

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added
    enhancement
    on Mar 6, 2026
  2. VividhDesign commented on Oct 5, 2026

    @VividhDesign
    Contributor

    A data point for this request, measured on main-dev (v2.26.3, Python, 200k × 128 f32 vectors):

    step size capacity memory_usage
    after add 200,000 262,144 178.0 MiB
    after removing 90% 20,000 262,144 178.0 MiB
    after compact() 20,000 262,144 178.0 MiB

    So compact() reorders the graph but doesn't give memory back. Freed slots are reused by later add() calls, and that reuse is now safe since #793 (before it, compact() left the key lookup and free-slot list stale).

    @ashvardanian, would a shrink_to_fit() that reallocates the node, vector and lookup storage down to the live count after compact() be welcome in v2 main-dev, or is this better left for v3? I'm happy to implement it either way.

  3. ashvardanian commented on Oct 5, 2026

    @ashvardanian
    Member

    If it's not supposed to break anything, lets add it to the v2. Thanks!

  4. ashvardanian commented on Nov 14, 2026

    @ashvardanian
    Member

    @anaslimem, thanks for the request, and @VividhDesign for the memory measurements! Rather than a separate compact(), removal in 3.0 reclaims memory itself: #846 proposes an exclusive call that removes a batch of keys, renumbers the survivors densely and frees what they leave behind, so I'm closing this in its favor.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions