From 5ad002effa849cb8358b57bbfd71c714542fed75 Mon Sep 17 00:00:00 2001 From: "shengjun.li" Date: Mon, 1 Mar 2021 19:57:53 +0800 Subject: [PATCH] fix minor memory leak when querying by IVF_SQ8H (#4758) * fix minor memory leak when querying by IVF_SQ8H Signed-off-by: shengjun.li * fix mismatch malloc/free new/delete Signed-off-by: shengjun.li --- CHANGELOG.md | 1 + .../vector_index/gpu/IndexIVFSQHybrid.cpp | 22 +++++++++---------- core/src/index/unittest/test_gpuresource.cpp | 8 +++---- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd29f8d992..0d20748ef9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Please mark all change in change log and use the issue from GitHub ## Bug - \#4739 Fix mishards probe test problem - \#4749 Fix minor memory leak when building IVF_SQ8 on GPU +- \#4757 Fix minor memory leak when querying by IVF_SQ8H ## Feature - \#3977 Support logging to stdout diff --git a/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexIVFSQHybrid.cpp b/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexIVFSQHybrid.cpp index df9d0a42bd..878653c804 100644 --- a/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexIVFSQHybrid.cpp +++ b/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexIVFSQHybrid.cpp @@ -138,12 +138,12 @@ IVFSQHybrid::LoadData(const FaissIVFQuantizerPtr& quantizer_ptr, const Config& c if (ivf_quantizer == nullptr) KNOWHERE_THROW_MSG("quantizer type not faissivfquantizer"); - auto index_composition = new faiss::IndexComposition; - index_composition->index = index_.get(); - index_composition->quantizer = ivf_quantizer->quantizer; - index_composition->mode = 2; // only 2 + faiss::IndexComposition index_composition; + index_composition.index = index_.get(); + index_composition.quantizer = ivf_quantizer->quantizer; + index_composition.mode = 2; // only copy data - auto gpu_index = faiss::gpu::index_cpu_to_gpu(res->faiss_res.get(), gpu_id, index_composition, &option); + auto gpu_index = faiss::gpu::index_cpu_to_gpu(res->faiss_res.get(), gpu_id, &index_composition, &option); std::shared_ptr new_idx; new_idx.reset(gpu_index); auto sq_idx = std::make_shared(new_idx, gpu_id, res); @@ -162,17 +162,17 @@ IVFSQHybrid::LoadQuantizer(const Config& config) { faiss::gpu::GpuClonerOptions option; option.allInGpu = true; - auto index_composition = new faiss::IndexComposition; - index_composition->index = index_.get(); - index_composition->quantizer = nullptr; - index_composition->mode = 1; // only 1 + faiss::IndexComposition index_composition; + index_composition.index = index_.get(); + index_composition.quantizer = nullptr; + index_composition.mode = 1; // only copy quantizer - auto gpu_index = faiss::gpu::index_cpu_to_gpu(res->faiss_res.get(), gpu_id, index_composition, &option); + auto gpu_index = faiss::gpu::index_cpu_to_gpu(res->faiss_res.get(), gpu_id, &index_composition, &option); delete gpu_index; auto q = std::make_shared(); - auto& q_ptr = index_composition->quantizer; + auto q_ptr = index_composition.quantizer; q->size = q_ptr->d * q_ptr->getNumVecs() * sizeof(float); q->quantizer = q_ptr; q->gpu_id = gpu_id; diff --git a/core/src/index/unittest/test_gpuresource.cpp b/core/src/index/unittest/test_gpuresource.cpp index 8d36c49a11..b1fa31cb29 100644 --- a/core/src/index/unittest/test_gpuresource.cpp +++ b/core/src/index/unittest/test_gpuresource.cpp @@ -42,14 +42,14 @@ class GPURESTEST : public DataGen, public TestGpuIndexBase { k = K; elems = nq * k; - ids = (int64_t*)malloc(sizeof(int64_t) * elems); - dis = (float*)malloc(sizeof(float) * elems); + ids = new int64_t[elems]; + dis = new float[elems]; } void TearDown() override { - delete ids; - delete dis; + delete[] ids; + delete[] dis; TestGpuIndexBase::TearDown(); }