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?
当我使用file_get_contents并将其作为参数传递给另一个函数而不将其分配给变量时,是否在脚本执行完成之前释放了该内存?
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?
file_get_contents使用的内存是否会在脚本完成之前释放?
4 个解决方案
#1
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:
这通过使用报告其自身消亡的类来证明生命周期:
class lifetime
{
public function __construct()
{
echo "construct\n";
}
public function __destruct()
{
echo "destruct\n";
}
}
function getTestObject()
{
return new lifetime();
}
function foo($obj)
{
echo "inside foo\n";
}
echo "Calling foo\n";
foo(getTestObject());
echo "foo complete\n";
This outputs
Calling foo
construct
inside foo
destruct
foo complete
Which indicates that the implied temporary variable is destroyed right after the foo function call.
这表示隐含的临时变量在foo函数调用之后被销毁。
Method 2: measure memory usage
Here's another method which offers further confirmation using memory_get_usage to measure how much we've consumed.
这是另一种方法,它使用memory_get_usage来测量我们消耗了多少进一步的确认。
function foo($str)
{
$length=strlen($str);
echo "in foo: data is $length, memory usage=".memory_get_usage()."\n";
}
echo "start: ".memory_get_usage()."\n";
foo(file_get_contents('/tmp/three_megabyte_file'));
echo "end: ".memory_get_usage()."\n";
This outputs
start: 50672
in foo: data is 2999384, memory usage=3050884
end: 51544
#2
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
在您的示例中,当$ matches超出范围时,将释放内存。如果您没有存储匹配结果,则会立即释放内存
#3
In following code memory usage = 6493720
在以下代码内存使用= 6493720
start: 1050504
end: 6492344
echo "start: ".memory_get_usage()."\n";
$data = file_get_contents("/six_megabyte_file");
echo "end: ".memory_get_usage()."\n";
but memory usage in following code = 1049680
但以下代码中的内存使用情况= 1049680
start = 1050504
start = 1050504
end = 1050976
结束= 1050976
echo "start: ".memory_get_usage()."\n";
file_get_contents("/six_megabyte_file");
echo "end: ".memory_get_usage()."\n";
Note: in first code file stores in a variable.
注意:在第一个代码文件中存储一个变量。
#4
If you think this will help in avoiding insufficient memory errors you are wrong. Your code (bytes_format):
如果您认为这有助于避免内存错误不足,那么您就错了。你的代码(bytes_format):
<?php
$url = 'http://speedtest.netcologne.de/test_10mb.bin';
echo 'Before: ' . bytes_format(memory_get_usage()) . PHP_EOL;
preg_match('~~', file_get_contents($url), $matches);
echo 'After: ' . bytes_format(memory_get_usage()) . PHP_EOL;
echo 'Peak: ' . bytes_format(memory_get_peak_usage(true)) . PHP_EOL;
?>
uses 10.5 MB:
使用10.5 MB:
Before: 215.41 KB
After: 218.41 KB
Peak: 10.5 MB
and this code:
而这段代码:
<?php
$url = 'http://speedtest.netcologne.de/test_10mb.bin';
echo 'Before: ' . bytes_format(memory_get_usage()) . PHP_EOL;
$contents = file_get_contents($url);
preg_match('~~', $contents, $matches);
unset($contents);
echo 'After: ' . bytes_format(memory_get_usage()) . PHP_EOL;
echo 'Peak: ' . bytes_format(memory_get_peak_usage(true)) . PHP_EOL;
?>
uses 10.5 MB as well:
使用10.5 MB:
Before: 215.13 KB
After: 217.64 KB
Peak: 10.5 MB
If you like to guard your script you need to use the $length
parameter or read the file in chunks.
如果您想保护脚本,则需要使用$ length参数或以块的形式读取文件。
#1
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:
这通过使用报告其自身消亡的类来证明生命周期:
class lifetime
{
public function __construct()
{
echo "construct\n";
}
public function __destruct()
{
echo "destruct\n";
}
}
function getTestObject()
{
return new lifetime();
}
function foo($obj)
{
echo "inside foo\n";
}
echo "Calling foo\n";
foo(getTestObject());
echo "foo complete\n";
This outputs
Calling foo
construct
inside foo
destruct
foo complete
Which indicates that the implied temporary variable is destroyed right after the foo function call.
这表示隐含的临时变量在foo函数调用之后被销毁。
Method 2: measure memory usage
Here's another method which offers further confirmation using memory_get_usage to measure how much we've consumed.
这是另一种方法,它使用memory_get_usage来测量我们消耗了多少进一步的确认。
function foo($str)
{
$length=strlen($str);
echo "in foo: data is $length, memory usage=".memory_get_usage()."\n";
}
echo "start: ".memory_get_usage()."\n";
foo(file_get_contents('/tmp/three_megabyte_file'));
echo "end: ".memory_get_usage()."\n";
This outputs
start: 50672
in foo: data is 2999384, memory usage=3050884
end: 51544
#2
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
在您的示例中,当$ matches超出范围时,将释放内存。如果您没有存储匹配结果,则会立即释放内存
#3
In following code memory usage = 6493720
在以下代码内存使用= 6493720
start: 1050504
end: 6492344
echo "start: ".memory_get_usage()."\n";
$data = file_get_contents("/six_megabyte_file");
echo "end: ".memory_get_usage()."\n";
but memory usage in following code = 1049680
但以下代码中的内存使用情况= 1049680
start = 1050504
start = 1050504
end = 1050976
结束= 1050976
echo "start: ".memory_get_usage()."\n";
file_get_contents("/six_megabyte_file");
echo "end: ".memory_get_usage()."\n";
Note: in first code file stores in a variable.
注意:在第一个代码文件中存储一个变量。
#4
If you think this will help in avoiding insufficient memory errors you are wrong. Your code (bytes_format):
如果您认为这有助于避免内存错误不足,那么您就错了。你的代码(bytes_format):
<?php
$url = 'http://speedtest.netcologne.de/test_10mb.bin';
echo 'Before: ' . bytes_format(memory_get_usage()) . PHP_EOL;
preg_match('~~', file_get_contents($url), $matches);
echo 'After: ' . bytes_format(memory_get_usage()) . PHP_EOL;
echo 'Peak: ' . bytes_format(memory_get_peak_usage(true)) . PHP_EOL;
?>
uses 10.5 MB:
使用10.5 MB:
Before: 215.41 KB
After: 218.41 KB
Peak: 10.5 MB
and this code:
而这段代码:
<?php
$url = 'http://speedtest.netcologne.de/test_10mb.bin';
echo 'Before: ' . bytes_format(memory_get_usage()) . PHP_EOL;
$contents = file_get_contents($url);
preg_match('~~', $contents, $matches);
unset($contents);
echo 'After: ' . bytes_format(memory_get_usage()) . PHP_EOL;
echo 'Peak: ' . bytes_format(memory_get_peak_usage(true)) . PHP_EOL;
?>
uses 10.5 MB as well:
使用10.5 MB:
Before: 215.13 KB
After: 217.64 KB
Peak: 10.5 MB
If you like to guard your script you need to use the $length
parameter or read the file in chunks.
如果您想保护脚本,则需要使用$ length参数或以块的形式读取文件。