在 PHP 中获取对象的内存大小?
有一种方法可以获取 PHP 使用的总内存(memory_get_usage()
),但是如何获取单个对象的内存大小呢?
显然我不是在谈论 count()
因为我想要潜在复杂的数据结构中的字节数。
There is a way to get the total memory PHP is using (memory_get_usage()
) but how does one get the size in memory of an individual object?
I'm obviously not talking about count()
as I want the number of bytes in a potentially complex data structure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在分配类之前和之后调用 memory_get_usage(),如 this 中所示来自 IBM 的示例。您甚至可以创建一个包装器来执行此操作,可能将结果存储在复杂类本身的成员变量上。
编辑:
为了澄清有关存储分配的内存大小的部分,您可以执行以下操作:
在将来的任何时候,您可以检查 allocateSize 以查看该对象在分配时有多大。但是,如果在分配后添加它,altedSize 将不再准确。
You can call memory_get_usage() before and after allocating your class as illustrated in this example from IBM. You could even create a wrapper to do this, possibly storing the result on a member variable of the complex class itself.
EDIT:
To clarify the part about storing the allocated memory size, you can do something like this:
At any point in the future, you could check allocatedSize to see how big that object was at time of allocation. If you add to it after allocating it, though, allocatedSize would no longer be accurate.
尝试序列化对象并读取字符串长度是否没有意义?显然这会少几个字节,因为序列化字符串会有 s:'string' 因此 s:'' 是额外的字节...除非序列化可以与 PHP 存储对象的方式相同???
例如
只是一个想法?
另一个混乱但可能准确的想法:
假设一个类实例变量自实例化以来已经被操作了几次:
$mem 可能是分配给 $DB 的确切内存量;
Would it not make sense to try serializing the object and reading the string length? Obviously it will be several bytes off because serialized string would have s:'string' therefore s:'' being extra bytes... unless serialize could be the same way that PHP stores objects???
so for example
Just a thought?
Another messy but possibly accurate thought:
Assuming a class instance variable that has been manipulated a few times since instantiation:
$mem could be the exact amount of memory allocated to $DB;
我认为这不太可能;我从未见过任何可以让您获取内存中对象的大小的东西。
获得一个相当粗略的想法的解决方案可能是序列化您的数据,并对其使用 strlen...但这实际上是一种估计...实际上,我不太依赖这样的东西。 ..
即使
debug_zval_dump
也不不要这样做:它输出变量和引用计数中的数据,但不输出使用的内存:只会让你:
I don't think this is quite possible ; I've never seen anything that would allow you to get the size of an object in memory.
A solution to get a pretty rough idea might be to kind of serialize your data, and use strlen on that... But that will really be something of an estimation... I wouldn't quite rely on anything like that, actually...
Even
debug_zval_dump
doesn't do that : it ouput the data in a variable and the refcount, but not the memory used :will only get you :
由于克隆(来自Prof83的答案)对我不起作用,我尝试序列化并反序列化我想要测量其大小的变量:
我认为它报告了更好的结果,至少对我而言。
Since clone (from Prof83's answer) didn't work for me, I tried to serialize and unserialize the variable whose size I want to measure:
I think it reports better results, at least for me.
如果您只想知道对象的大小,而不是为了您的下一个代码,请将其
返回
给浏览器,您可以看到网络传输了多少物体。
If you just want to know the size of object, and not for your next code,
return
it to the browser, and you can see how much the network transmitthe object.