Skip to content

Commit

Permalink
Merge pull request #81 from agerasev/fix-gpu-res-leak
Browse files Browse the repository at this point in the history
Fix `StandardGpuResources` memory leak
  • Loading branch information
Enet4 authored Apr 1, 2024
2 parents 7a2843a + 76eb627 commit 97cdff8
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ impl StandardGpuResources {
}
}

impl Drop for StandardGpuResources {
fn drop(&mut self) {
unsafe {
faiss_StandardGpuResources_free(self.inner);
}
}
}

impl GpuResourcesProvider for StandardGpuResources {
fn inner_ptr(&self) -> *mut FaissGpuResourcesProvider {
self.inner as *mut _
Expand Down Expand Up @@ -169,4 +177,20 @@ mod tests {
fn smoke_detector() {
StandardGpuResources::new().unwrap();
}

// The test marked as ignored because it takes a significant amount of time.
#[ignore]
#[test]
fn resources_leak() {
use crate::{index_factory, MetricType};

// Try to allocate gpu resources multiple times in a loop.
// If resources are not properly deallocated this will cause out-of-memory error.
for _ in 0..50 {
let res = StandardGpuResources::new().unwrap();
// We need to create an index because `StandardGpuResources` constructor does not allocate actual resources.
let index = index_factory(32, "Flat", MetricType::InnerProduct).unwrap();
let _gpu_index = index.into_gpu(&res, 0).unwrap();
}
}
}

0 comments on commit 97cdff8

Please sign in to comment.