用new分配并存储在LRU缓冲区中,不会发生删除?

发布于 2025-01-18 06:38:44 字数 2654 浏览 1 评论 0 原文

我正在实现一个简单的无序地图,以 new 关键字分配它们,并且我希望将对象从缓存中驱逐时被删除。作为参考,这是我正在使用的LRU缓存实现:https://github.com/facebook/hhvm/blob/master/hphp/util/concurrent-lru-cache.h

I am testing my program on Linux, 1 thread. The code is the following:

HPHP::ConcurrentLRUCache<std::string, std::unordered_map<int, int>*> my_cache(10);
for (int i = 0; i < 1000000; i++) {
    auto map = new std::unordered_map<int, int>(1000);
    for (int j = 0; j < 1000; j++)
        map->insert(std::make_pair(j, j));
    my_cache.insert(std::to_string(i), map);
}

template <class TKey, class TValue, class THash>
    bool ConcurrentLRUCache<TKey, TValue, THash>::
    insert(const TKey& key, const TValue& value) {
        // Insert into the CHM
        ListNode* node = new ListNode(key);
        HashMapAccessor hashAccessor;
        HashMapValuePair hashMapValue(key, HashMapValue(value, node));
        if (!m_map.insert(hashAccessor, hashMapValue)) {
            delete node;
            return false;
        }

        // Evict if necessary, now that we know the hashmap insertion was successful.
        size_t size = m_size.load();
        bool evictionDone = false;
        if (size >= m_maxSize) {
            evict();
            evictionDone = true;
        }

        std::unique_lock<ListMutex> lock(m_listMutex);
        pushFront(node);
        lock.unlock();
        if (!evictionDone) {
            size = m_size++;
        }
        if (size > m_maxSize) {
            if (m_size.compare_exchange_strong(size, size - 1)) {
                evict();
            }
        }
        return true;
    }

template <class TKey, class TValue, class THash>
    void ConcurrentLRUCache<TKey, TValue, THash>::
    evict() {
        std::unique_lock<ListMutex> lock(m_listMutex);
        ListNode* moribund = m_tail.m_prev;
        if (moribund == &m_head) {
            // List is empty, can't evict
            return;
        }
        delink(moribund);
        lock.unlock();

        HashMapAccessor hashAccessor;
        if (!m_map.find(hashAccessor, moribund->m_key)) {
            // Presumably unreachable
            return;
        }
        m_map.erase(hashAccessor);
        delete moribund;
    }

When inserting a map, if the cache is full, the last recently used element should get evicted.但是,地图本身不会被删除,并且内存使用量达到顶峰。我在做什么错?

注意:应在执行程序执行期间检索地图,以便可以从缓存访问它们。

I am implementing a simple cache of unordered maps allocating them with the new keyword, and I would like for the objects to be deleted when they are evicted from the cache. For reference, this is the LRU cache implementation I am using: https://github.com/facebook/hhvm/blob/master/hphp/util/concurrent-lru-cache.h

I am testing my program on Linux, 1 thread. The code is the following:

HPHP::ConcurrentLRUCache<std::string, std::unordered_map<int, int>*> my_cache(10);
for (int i = 0; i < 1000000; i++) {
    auto map = new std::unordered_map<int, int>(1000);
    for (int j = 0; j < 1000; j++)
        map->insert(std::make_pair(j, j));
    my_cache.insert(std::to_string(i), map);
}

template <class TKey, class TValue, class THash>
    bool ConcurrentLRUCache<TKey, TValue, THash>::
    insert(const TKey& key, const TValue& value) {
        // Insert into the CHM
        ListNode* node = new ListNode(key);
        HashMapAccessor hashAccessor;
        HashMapValuePair hashMapValue(key, HashMapValue(value, node));
        if (!m_map.insert(hashAccessor, hashMapValue)) {
            delete node;
            return false;
        }

        // Evict if necessary, now that we know the hashmap insertion was successful.
        size_t size = m_size.load();
        bool evictionDone = false;
        if (size >= m_maxSize) {
            evict();
            evictionDone = true;
        }

        std::unique_lock<ListMutex> lock(m_listMutex);
        pushFront(node);
        lock.unlock();
        if (!evictionDone) {
            size = m_size++;
        }
        if (size > m_maxSize) {
            if (m_size.compare_exchange_strong(size, size - 1)) {
                evict();
            }
        }
        return true;
    }

template <class TKey, class TValue, class THash>
    void ConcurrentLRUCache<TKey, TValue, THash>::
    evict() {
        std::unique_lock<ListMutex> lock(m_listMutex);
        ListNode* moribund = m_tail.m_prev;
        if (moribund == &m_head) {
            // List is empty, can't evict
            return;
        }
        delink(moribund);
        lock.unlock();

        HashMapAccessor hashAccessor;
        if (!m_map.find(hashAccessor, moribund->m_key)) {
            // Presumably unreachable
            return;
        }
        m_map.erase(hashAccessor);
        delete moribund;
    }

When inserting a map, if the cache is full, the last recently used element should get evicted. However, the map itself does not get deleted and the memory usage peaks. What am I doing wrong?

Note: the maps should be retrieved during the execution of my program, so they should be accessible from the cache.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文