lysS
V2EX  ›  问与答

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

  •  
  •   lysS · Mar 8, 2020 · 1574 views
    This topic created in 2350 days ago, the information mentioned may be changed or developed.
    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# 
    
    Supplement 1  ·  Mar 9, 2020

    已解决 需求: 扒大图,需要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 replies    2020-03-09 10:48:51 +08:00
    surfire91
        1
    surfire91  
       Mar 8, 2020
    参数传 true 试试,memory_get_peak_usage() 也试试
    wanguorui123
        2
    wanguorui123  
       Mar 9, 2020 via iPhone
    内存碎片
    lysS
        3
    lysS  
    OP
       Mar 9, 2020
    @wanguorui123
    试了下还是一样的, 有空认真学习下这方面
    @surfire91
    感谢回复
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3050 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 02:43 · PVG 10:43 · LAX 19:43 · JFK 22:43
    ♥ Do have faith in what you're doing.