You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature: Add compact() method to reclaim memory from deleted vectors (tombstones) #726
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.
letmut index = usearch::new_index(&options).unwrap();// Insert a large number of vectorsfor i in0..1_000_000{
index.add(i,&vec![0.0;128]).unwrap();}// Remove 90% of themfor i in0..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?
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.
@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.
Activity
Fix: Bounded probe in `equal_iterator_gt::operator++`
Fix: Bounded probe in `equal_iterator_gt::operator++`
VividhDesign commentedon Oct 5, 2026
A data point for this request, measured on
main-dev(v2.26.3, Python, 200k × 128f32vectors):memory_usageaddcompact()So
compact()reorders the graph but doesn't give memory back. Freed slots are reused by lateradd()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 aftercompact()be welcome in v2main-dev, or is this better left for v3? I'm happy to implement it either way.ashvardanian commentedon Oct 5, 2026
If it's not supposed to break anything, lets add it to the v2. Thanks!
ashvardanian commentedon Nov 14, 2026
@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.