简体   繁体   English

有关在php脚本中释放内存的问题

[英]Question about freeing memory in a php script

I just saw this in a script 我只是在脚本中看到了

mysql_free_result($rawdb);

I saw it on a script somewhere, it ran a mysql query and saved the results to an array, right after adding to the array, it ran that which I assume free's the memory used from the mysql query. 我在某个地方的脚本上看到了它,它运行了一个mysql查询并将结果保存到一个数组中,然后将其添加到该数组中,然后运行了它,假设我释放了mysql查询中使用的内存。

I am just asking to verify, is it a good idea to do this? 我只是想验证一下,这样做是个好主意吗? I have a couple mysql queries which return a large result which is saved to an array, should I run that code after it? 我有几个mysql查询,它们返回一个很大的结果,并保存到数组中,我应该在它之后运行该代码吗?

Also in a performance conscious environment is it good practice to unset large sessions and variables when finished with them? 同样在注重性能的环境中,在完成大型会话和变量后是否取消设置大型会话和变量是一种好习惯?

I wouldn't worry about it. 我不会担心。 All the result resources are freed at the end of the script, and unless you're having problems with memory when doing queries with large result sets, the difference will be negligible. 所有结果资源都在脚本末尾释放,并且除非对大型结果集进行查询时内存出现问题,否则差异可以忽略不计。

That said, if I were reading your code and saw mysql_free_result i'd know for sure that you weren't going to be using that resource anywhere further on in the code, so it could add a little readability... 就是说,如果我正在阅读您的代码并看到mysql_free_result我肯定会确定您不会在代码中的任何其他地方使用该资源,因此它可以增加一点可读性...

I would think it good practice to put into your data abstraction layer, as it makes it a good citizen with memory handling. 我认为将其放入数据抽象层是一种好习惯,因为它使它成为具有内存处理能力的良好公民。 Especially as a lot of PHP code is not. 特别是由于很多PHP代码不是。 :-/ :-/

The issue of memory usage was a whole lot worse in PHP 4 than in 5, but it can still be an issue. PHP 4中的内存使用问题比5中的要严重得多,但仍然是一个问题。 If you have to keep raising the maximum memory allowed to your PHP script (the default is 8Mb, but most environments I use have this up at 64Mb), then you probably should be thinking about how your script is using and over-using memory. 如果您必须继续提高PHP脚本允许的最大内存(默认值为8Mb,但我使用的大多数环境的最大内存为64Mb),那么您可能应该考虑脚本的使用和过度使用情况。 Using mysql_free_result() is one piece in this arsenal, but on its own is rather pointless. 在这个工具库中,使用mysql_free_result()是一件简单的事情,但mysql_free_result()它是毫无意义的。 Processing huge datasets can be done much more memory efficiently if you use mysql_unbuffered_query() and process each row as you retrieve it with mysql_fetch() . 如果使用mysql_unbuffered_query()并在使用mysql_fetch()检索每一行时对其进行处理,则可以有效地处理大量数据集,从而有效地处理了更多的内存。 I've seen the capacity of scripts to process data go up enormously when re-written to use this approach. 我已经看到使用这种方法重新编写脚本时,处理数据的能力会大大提高。

There's some discussion of this in the PHP API documentation . PHP API文档中对此进行了一些讨论。 Depending on your use case (ie, the size of the results you're retrieving), mysql_free_result() may improve or decrease performance. 根据您的用例(即,您要检索的结果的大小), mysql_free_result()可能会提高或降低性能。 As the commenter at the bottom of that link says, call memory_get_usage() to get an idea of whether you should free the result or not. 正如该链接底部的评论者所说,请调用memory_get_usage()来了解是否应释放结果。 Nothing beats actually examining your resource usage. 实际检查您的资源使用情况无济于事。

Also in a performance conscious environment is it good practice to unset large sessions and variables when finished with them? 同样在注重性能的环境中,在完成大型会话和变量后是否取消设置大型会话和变量是一种好习惯?

Note that if you unset variables from $_SESSION , you will effectivly delete them from the session : they won't be there the next time your user calls a page of your website. 请注意,如果您从$_SESSION取消设置变量,则将有效地从会话中删除它们:下次用户访问您的网站页面时,它们将不存在。

So, you might want to be careful with that idea : only delete data from $_SESSION when you don't need it anymore. 因此,您可能要谨慎使用该想法:仅在不再需要$_SESSION时才删除它。

About unset ting data and/or using functions like mysql_free_result : considering your scripts are (I suppose, as PHP is for web-development, and users won't wait hours for pages to load) running only for a few hundred milliseconds, freeing memory that way is probably a bit overkill : as long as you don't get memory_limit -related errors, you probably shouldn't care. 关于unset数据和/或使用mysql_free_result功能:考虑到您的脚本仅运行几百毫秒(我想,因为PHP用于Web开发,用户不会等待数小时的页面加载时间)仅运行几百毫秒,从而释放了内存这样可能有点矫kill过正:只要您没有遇到与memory_limit相关的错误,就可能不在乎。

From the manual : 手册

mysql_free_result() only needs to be called if you are concerned about how much memory is being used for queries that return large result sets. 仅当您担心返回大结果集的查询使用多少内存时,才需要调用mysql_free_result() All associated result memory is automatically freed at the end of the script's execution. 在脚本执行结束时,将自动释放所有关联的结果存储器。

The PHP manual page for the function answers most of your questions: 该函数的PHP手册页回答了您的大多数问题:

http://us.php.net/manual/en/function.mysql-free-result.php http://us.php.net/manual/zh/function.mysql-free-result.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM