用new分配并存储在LRU缓冲区中,不会发生删除?
我正在实现一个简单的无序地图,以 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.但是,地图本身不会被删除,并且内存使用量达到顶峰。我在做什么错?
注意:应在执行程序执行期间检索地图,以便可以从缓存访问它们。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论