Faiss: how to release an index in C++/CPU

Created on 28 May 2020  路  4Comments  路  Source: facebookresearch/faiss

Summary

I have a faiss cpu C++ application on server, and I need to update the index everyday, so I encapsulated the index and search vector into a class and call the destructor every time I update the class member. From the log I print from the server , Im sure the destructor was called every time ,however ,the memory occupied by the service has been growing. Is there any problem about the destructor?

Platform

centos 7

Running on:

  • [x] CPU
  • [ ] GPU

Interface:

  • [x] C++
  • [ ] Python

Reproduction instructions

    class FaissTool
    {   
    public:
        faiss::Index* index;
        std::vector<Item> storage_item_vec;
        FaissTool(){}
        ~FaissTool()
        {   
            index->reset();
            index=NULL;
            printf("release index");
        }                                                                                  
    };  
    typedef std::tr1::shared_ptr<FaissTool> FaissToolPtr;

I use a hashmap to store the FaissToolPtr, the key is a constant. everytime I set the new FaissToolPtr in the hashmap, the destructor has been called, but memory did not released

help wanted

Most helpful comment

In case it wasn't clear, with index=NULL you are deliberately dropping the pointer to the index object, not releasing the actual index. If the object is not owned anywhere else, you need to delete it instead (although this means you should also rethink your class's other member functions and constructors, see the rule of three). After that, it's up to the allocator to release the memory back to the system.

All 4 comments

basic C++ error

basic C++ error

I find that reset() function in cpu version can only clear data but can not release memory, Am I right?

In case it wasn't clear, with index=NULL you are deliberately dropping the pointer to the index object, not releasing the actual index. If the object is not owned anywhere else, you need to delete it instead (although this means you should also rethink your class's other member functions and constructors, see the rule of three). After that, it's up to the allocator to release the memory back to the system.

//index=NULL;
delete index;

it works,thanks

Was this page helpful?
0 / 5 - 0 ratings