file_get_contents() 使用的内存在未分配给变量时是否会被释放?
当我使用 file_get_contents 并将其作为参数传递给另一个函数而不将其分配给变量时,该内存是否会在脚本执行完成之前被释放?
例如:
preg_match($pattern, file_get_contents('http://domain.tld/path/to/file.ext'), $matches);
file_get_contents 使用的内存会在脚本完成之前释放吗?
When I use file_get_contents and pass it as a parameter to another function, without assigning it to a variable, does that memory get released before the script execution finishes?
For Example:
preg_match($pattern, file_get_contents('http://domain.tld/path/to/file.ext'), $matches);
Will the memory used by file_get_contents be released before the script finishes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为保存文件内容而创建的临时字符串将被销毁。 无需深入研究来源进行确认,您可以通过以下几种方法来测试作为函数参数创建的临时值是否被销毁:
方法 1:报告其销毁的类
这通过使用报告其自身消亡的类来演示生命周期:
此输出
表明隐含临时变量在 foo 函数调用后立即被销毁。
方法 2:测量内存使用情况
这是另一种方法,它使用 memory_get_usage 来进一步确认我们消耗了多少内存。
这输出
The temporary string created to hold the file contents will be destroyed. Without delving into the sources to confirm, here's a couple of ways you can test that a temporary value created as a function parameter gets destroyed:
Method 1: a class which reports its destruction
This demonstrates lifetime by using a class which reports on its own demise:
This outputs
Which indicates that the implied temporary variable is destroyed right after the foo function call.
Method 2: measure memory usage
Here's another method which offers further confirmation using memory_get_usage to measure how much we've consumed.
This outputs
在您的示例中,当
$matches
超出范围时,内存将被释放。如果您没有存储比赛结果,内存将立即被释放
In your example the memory will be released when
$matches
goes out of scope.If you weren't storing the result of the match the memory would be released immediately
在以下代码中,内存使用量 = 6493720
start: 1050504
end: 6492344
但以下代码中的内存使用量 = 1049680
start = 1050504
end = 1050976
注意:在第一个代码文件中存储在变量中。
In following code memory usage = 6493720
start: 1050504
end: 6492344
but memory usage in following code = 1049680
start = 1050504
end = 1050976
Note: in first code file stores in a variable.
如果您认为这将有助于避免内存不足错误,那么您就错了。 您的代码(bytes_format):
使用 10.5 MB:
并且此代码:
也使用 10.5 MB:
如果您想保护您的脚本,您需要使用
$length
参数或分块读取文件。If you think this will help in avoiding insufficient memory errors you are wrong. Your code (bytes_format):
uses 10.5 MB:
and this code:
uses 10.5 MB as well:
If you like to guard your script you need to use the
$length
parameter or read the file in chunks.