如何在Perl中查找哈希占用的物理内存量? [英] How to find the amount of physical memory occupied by a hash in Perl?

查看:181
本文介绍了如何在Perl中查找哈希占用的物理内存量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Perl脚本,使用哈希表维护一个非常简单的缓存。我想在哈希占用超过n个字节时清除哈希值,以避免Perl(32位)耗尽内存并导致崩溃。



我可以检查键值对的数量:

 <$ c (标量键%cache> $ maxSize)
{
%cache =();
}

但是有可能检查哈希占用的实际内存吗?

解决方案

Devel :: Size 是你的问题的答案。 (请注意,Devel :: Size将在处理大型数据结构时临时分配大量内存,因此它不适合用于此目的。)

然而, Cache :: SizeAwareMemoryCache Tie :: Cache 已经实现了你正在寻找的东西(具有不同的接口),并且可以帮你避免重新发明轮子。 >

记事是一个模块,缓存函数的返回值。它没有实现基于大小的缓存限制,但应该可以使用Tie :: Cache作为Memoize的后端。


I have a Perl script where I maintain a very simple cache using a hash table. I would like to clear the hash once it occupies more than n bytes, to avoid Perl (32-bit) running out of memory and crashing.

I can do a check on the number of keys-value pairs:

if (scalar keys %cache > $maxSize)
 {
 %cache = ();
 }

But is it possible to check the actual memory occupied by the hash?

解决方案

Devel::Size is the answer to your question. (Note that Devel::Size will temporarily allocate a significant amount of memory when processing a large data structure, so it's not really well suited to this purpose.)

However, Cache::SizeAwareMemoryCache and Tie::Cache already implement what you're looking for (with somewhat different interfaces), and could save you from reinventing the wheel.

Memoize is a module that makes it simple to cache the return value from a function. It doesn't implement a size-based cache limit, but it should be possible to use Tie::Cache as a backend for Memoize.

这篇关于如何在Perl中查找哈希占用的物理内存量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆