如何确定变量的内存占用(大小)?

时间:2022-12-31
本文介绍了如何确定变量的内存占用(大小)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

PHP(或 PHP 扩展)中是否有函数可以找出给定变量使用了多少内存?sizeof 只是告诉我元素/属性的数量.

memory_get_usage 的帮助在于它为我提供了 整个 脚本使用的内存大小.有没有办法对单个变量执行此操作?

请注意,这是在开发机器上,因此加载扩展或调试工具是可行的.

解决方案

您可能需要一个内存分析器.我已经收集了 SO 的信息,但我复制了一些可能对您也有帮助的重要内容.

您可能知道,从 2.* 版本开始,Xdebug 放弃了内存分析支持.请搜索已删除的功能"这里的字符串:.

黑火

Blackfire 是 Symfony2 的 SensioLabs 开发的 PHP 分析器https://blackfire.io/

如果您使用 puphpet 来设置您的虚拟机,您会很高兴知道它受支持;-)

Xdebug 和跟踪内存使用情况

XDEBUG2 是 PHP 的扩展.Xdebug 允许您记录所有函数调用,包括参数和以不同格式返回到文件的值.有三种输出格式.一个是人类可读的跟踪,另一个更适合计算机程序,因为它更容易解析,最后一个使用 HTML 来格式化跟踪.您可以通过设置在两种不同的格式之间切换.一个例子是此处提供

forp

forp 简单、非侵入性、面向生产的 PHP 分析器.一些功能是:

  • 为每个函数测量时间和分配的内存

  • CPU 使用率

  • 函数调用的文件和行号

  • 输出为 Google 的跟踪事件格式

  • 函数说明

  • 功能分组

  • 函数的别名(对匿名函数有用)

DBG

DBG 是一个全功能的 php 调试器,一个帮助您调试 php 脚本的交互式工具.它适用于生产和/或开发 WEB 服务器,允许您从 IDE 或控制台在本地或远程调试脚本,其功能是:

  • 远程和本地调试

  • 显式和隐式激活

  • 调用栈,包括函数调用、动态和静态方法调用及其参数

  • 在调用堆栈中导航,能够评估相应(嵌套)位置中的变量

  • 步进/步进/步进/运行到光标功能

  • 条件断点

  • 全局断点

  • 记录错误和警告

  • 用于并行调试的多个同时会话

  • 支持 GUI 和 CLI 前端

  • 支持 IPv6 和 IPv4 网络

  • 调试器传输的所有数据都可以选择使用 SSL 保护

Is there a function in PHP (or a PHP extension) to find out how much memory a given variable uses? sizeof just tells me the number of elements/properties.

memory_get_usage helps in that it gives me the memory size used by the whole script. Is there a way to do this for a single variable?

Note that this is on a development machine, so loading extensions or debug tools is feasible.

解决方案

You Probably need a Memory Profiler. I have gathered information fro SO but I have copied the some important thing which may help you also.

As you probably know, Xdebug dropped the memory profiling support since the 2.* version. Please search for the "removed functions" string here: http://www.xdebug.org/updates.php

Removed functions

Removed support for Memory profiling as that didn't work properly.

Other Profiler Options

php-memory-profiler

https://github.com/arnaud-lb/php-memory-profiler. This is what I've done on my Ubuntu server to enable it:

sudo apt-get install libjudy-dev libjudydebian1
sudo pecl install memprof
echo "extension=memprof.so" > /etc/php5/mods-available/memprof.ini
sudo php5enmod memprof
service apache2 restart

And then in my code:

<?php
memprof_enable();
// do your stuff
memprof_dump_callgrind(fopen("/tmp/callgrind.out", "w"));

Finally open the callgrind.out file with KCachegrind

Using Google gperftools (recommended!)

First of all install the Google gperftools by downloading the latest package here: https://code.google.com/p/gperftools/

Then as always:

sudo apt-get update
sudo apt-get install libunwind-dev -y
./configure
make
make install

Now in your code:

memprof_enable();

// do your magic

memprof_dump_pprof(fopen("/tmp/profile.heap", "w"));

Then open your terminal and launch:

pprof --web /tmp/profile.heap

pprof will create a new window in your existing browser session with something like shown below:

Xhprof + Xhgui (the best in my opinion to profile both cpu and memory)

With Xhprof and Xhgui you can profile the cpu usage as well or just the memory usage if that's your issue at the moment. It's a very complete solutions, it gives you full control and the logs can be written both on mongo or in the filesystem.

For more details see here.

Blackfire

Blackfire is a PHP profiler by SensioLabs, the Symfony2 guys https://blackfire.io/

If you use puphpet to set up your virtual machine you'll be happy to know it's supported ;-)

Xdebug and tracing memory usage

XDEBUG2 is a extension for PHP. Xdebug allows you to log all function calls, including parameters and return values to a file in different formats.There are three output formats. One is meant as a human readable trace, another one is more suited for computer programs as it is easier to parse, and the last one uses HTML for formatting the trace. You can switch between the two different formats with the setting. An example would be available here

forp

forp simple, non intrusive, production-oriented, PHP profiler. Some of features are:

  • measurement of time and allocated memory for each function

  • CPU usage

  • file and line number of the function call

  • output as Google's Trace Event format

  • caption of functions

  • grouping of functions

  • aliases of functions (useful for anonymous functions)

DBG

DBG is a a full-featured php debugger, an interactive tool that helps you debugging php scripts. It works on a production and/or development WEB server and allows you debug your scripts locally or remotely, from an IDE or console and its features are:

  • Remote and local debugging

  • Explicit and implicit activation

  • Call stack, including function calls, dynamic and static method calls, with their parameters

  • Navigation through the call stack with ability to evaluate variables in corresponding (nested) places

  • Step in/Step out/Step over/Run to cursor functionality

  • Conditional breakpoints

  • Global breakpoints

  • Logging for errors and warnings

  • Multiple simultaneous sessions for parallel debugging

  • Support for GUI and CLI front-ends

  • IPv6 and IPv4 networks supported

  • All data transferred by debugger can be optionally protected with SSL

这篇关于如何确定变量的内存占用(大小)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:PHP 警告:113 字节的 POST Content-Length 超出了 Unknown 中 -1988100096 下一篇:在 PHP 中跟踪内存使用情况

相关文章

最新文章