PHP 的内存撑爆了,memory_get_usage()获取的内存为啥还是那么点?

2020-03-08 10:51:24 +08:00
 lysS
function test($url){
    $pid = pcntl_fork();
    if( 0 == $pid ) {
        $R = file_get_contents($url );
    } 
    elseif( $pid > 0 ){      
        for( $i = 1; $i <= 50; $i++ ){
            sleep( 1 );
            // posix_getppid()函数的作用就是获取当前进程的父进程进程 ID
            var_dump( memory_get_usage() );
        }       
        pcntl_wait($status); //, WNOHANG
        pcntl_waitpid(-1, $status);    //, WNOHANG
                           
    }  else {
        echo "fork error.";
    }
}
$url = 'http://23.252.96.201/100mb.bin';
test($url);

结果:

sh-4.2# php client.php
int(390248)
int(390280)
int(390280)
int(390280)
int(390280)
int(390280)
int(390280)
int(390280)
int(390280)
int(390280)
int(390280)
int(390280)
int(390280)
int(390280)
int(390280)
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 98566176 bytes) in /www/forgetword.yixuewenku.cn/client.php on line 223

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 98566176 bytes) in /www/forgetword.yixuewenku.cn/client.php on line 223
int(390280)
int(390280)
int(390280)
^Z
[5]+  Stopped(SIGTSTP)        php client.php
sh-4.2# 
1091 次点击
所在节点    问与答
3 条回复
surfire91
2020-03-08 14:12:57 +08:00
参数传 true 试试,memory_get_peak_usage() 也试试
wanguorui123
2020-03-09 08:32:42 +08:00
内存碎片
lysS
2020-03-09 10:48:51 +08:00
@wanguorui123
试了下还是一样的, 有空认真学习下这方面
@surfire91
感谢回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/650853

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX