V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
lysS
V2EX  ›  问与答

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

  •  
  •   lysS · 2020-03-08 10:51:24 +08:00 · 1080 次点击
    这是一个创建于 1481 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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# 
    
    第 1 条附言  ·  2020-03-09 10:47:47 +08:00

    已解决 需求: 扒大图,需要ip代理,由于有的ip质量不行,有速度要求

    /**
     * Description: 分片下载并追加写入,在开始函数第三秒后检查文件大小,以判断对下载的速度(1.125Mb/3s, 可更改)
     * Note: 函数最小返回时间为3秒,不适合小文件下载; 本地已存在同名文件返回false
     * @param string $url      网络资源路径
     * @param string $newname  下载后重命名(带后缀)
     * @param bool   $isProxy  是否使用代理ip
     */
    function downResourc($url, $newname, $isProxy=true){
        $local_path = TEMPPATH.$newname; //下载临时路径
        if ( file_exists($local_path)== true ) { return false; } //由于file_put_contents追加,所以不能存在
        $pid = pcntl_fork();
        if( 0 == $pid ) { 
            $ch = fopen( $url, 'rb', false, creatHeader( $isProxy ) ); // ip代理
            if ( $ch==false) { return false; }
            $i = 0;
            while (!feof($ch)){
                $r = stream_get_contents($ch, 1179648, $i ); //每次读出1.125Mb
                file_put_contents( $local_path, $r, FILE_APPEND);
                $i = $i + 1179648;
                if ( $r==null || $r=='') { break; } //网络资源feof可能失效
            }
            fclose( $ch );
        }
        elseif( $pid > 0 ){ 
            $local_path = TEMPPATH.$newname; //下载临时路径
            sleep(3);
            // 子进程提前退出; 作用是为了小文件下载(在三秒内已经下载完成了小于步长(1.125Mb)的文件)
            if ( pcntl_wait($status_min , WNOHANG) == $pid ) {  return true; }
            //3秒内链接没建立
            if ( file_exists($local_path)==false ) { shell_exec("kill -9 $pid"); return false; } 
            // 在3秒检查文件大小
            $filesize = filesize($local_path);
            if ( $filesize<1179647 ) { 
                shell_exec("kill -9 $pid"); writeLog('速度慢1','xun.log');
                //删除未完成文件
                if ( file_exists($local_path)==true ) { unlink($local_path);  } 
                return false; 
            } 
            // 正常下载完成后回收子函数
            pcntl_wait($status); 
            pcntl_waitpid(-1, $status);           
        }  else {
            return false;
        }
    }
    
    3 条回复    2020-03-09 10:48:51 +08:00
    surfire91
        1
    surfire91  
       2020-03-08 14:12:57 +08:00
    参数传 true 试试,memory_get_peak_usage() 也试试
    wanguorui123
        2
    wanguorui123  
       2020-03-09 08:32:42 +08:00 via iPhone
    内存碎片
    lysS
        3
    lysS  
    OP
       2020-03-09 10:48:51 +08:00
    @wanguorui123
    试了下还是一样的, 有空认真学习下这方面
    @surfire91
    感谢回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4252 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:13 · PVG 18:13 · LAX 03:13 · JFK 06:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.