转储PHP中的所有变量 [英] Dump all variables in PHP

查看:75
本文介绍了转储PHP中的所有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本中存在内存泄漏,两天后我找不到.我发现了导致内存泄漏的循环.循环的每次迭代都会增加内存使用量.我将循环移到一个函数中以隔离变量.在函数的最后,我未设置该函数创建的每个变量,以便get_defined_vars()返回一个空数组.这就是我的意思:

There's a memory leak in my script and I couldn't find it after 2 days. I found the loop that is causing the memory leak; each iteration of the loop increases the memory usage. I moved the loop into a function to isolate the variables. At the end of the function, I unsetted every variable created by the function so that get_defined_vars() returns an empty array. Here's what I mean:

function the_loop(){
  $var="value";

  ... // processing, including using a library

  unset($var);
  print_r(get_defined_vars()); // prints empty array
}

while(true){
  the_loop();
  echo memory_get_usage()."\n"; // steadily increases until memory limit is reached
}

我猜想the_loop()中定义的某些变量仍在内存中.我尝试使用XDebug的跟踪工具,但没有帮助.它表明,从长远来看,内存使用量平均会增加.我正在寻找一种可以向我显示PHP内存中所有值的工具.我将能够基于值识别变量.什么工具可以转储PHP的内存?

I'm guessing that some variables defined in the_loop() are still in memory. I tried using XDebug's trace tool, but it didn't help. All it showed was that memory usage increases on average over the long run. I'm looking for a tool that can show me all the values in PHP's memory. I will be able to recognize the variable based on the value. What tool can dump PHP's memory?

推荐答案

正如Dragon所说,未设置不会立即释放内存.

As Dragon mentioned unset doesn't instantly free memory.

什么是使用PHP释放内存的更好方法:unset()或$ var = null

我会考虑重新评估您使用PHP的方式,它不是为长时间/恒定运行的脚本而设计的,垃圾处理程序根本不是那么好.

I'd consider re-evaluating the way you're using PHP, it's not designed for long/constant running scripts, the garbage handler simply isn't that great.

如果您想进一步研究执行脚本,我建议您检查一些工具,例如:

If you want to dig further into the executing script I'd suggest checking out some tools like:

https://github.com/jokkedk/webgrind

http://xhprof.io/

http://derickrethans.nl/xdebug-and-tracing-memory -usage.html

还值得一读:什么gc_collect_cycles函数有用? /a>

Also worth a read: What gc_collect_cycles function is useful for?

这篇关于转储PHP中的所有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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