【问题标题】:How to restrict php function to use specific amount of RAM?如何限制 php 函数使用特定数量的 RAM?
【发布时间】:2020-10-12 07:42:46
【问题描述】:

有没有办法限制 php 函数使用特定数量的计算机 RAM。 本地和服务器上有不同的 RAM 内存。我想在本地测试它只有 1 GB Ram 这可能是函数

    public function handle()
    {
       

            $pdfMerger = new  PdfMerger();

            $bulk_pdf = public_path('bulk-pdf-consignment/manifest' . $this->manifest_id . '_.pdf');
            $allFilesPath = public_path('bulk-pdf-consignment/manifest_' . $this->manifest_id);

            if (File::exists($bulk_pdf)) {
                $pdfMerger->addPDF($bulk_pdf, 'all');
            }
            $filesInFolder = collect(File::files($allFilesPath))->sortBy(function ($file) {
                return $file->getCTime();
            });

            $arr = [];
            $filesCount = 0;
            $x = '';
            foreach ($filesInFolder as $path) {
                $file = pathinfo($path);
                $finalPath = $file['dirname'] . '/' . $file['basename'];

                $added =  $pdfMerger->addPDF($finalPath, 'all');
                if ($added) {
                    array_push($arr, $finalPath);
                }
                $filesCount++;
                echo "real: ".(memory_get_peak_usage(true)/1024/1024)." MiB\n\n";
                $x .= str_repeat(' ', 1024*25); //store 25kb more to string
            }

            $merged =  $pdfMerger->merge("file", $bulk_pdf);


            if ($merged) {
                $url = Storage::disk('s3')->url('bulk_pdf_consignments/manifest' . $this->manifest_id . '_.pdf', fopen($bulk_pdf, 'r+'));

           
                Notification_Helper::sendNotification($this->user_id, 'Download Bulk Print Label PDF'.$filesCount, $url,$this->user_type);
            
                if ($this->user_type == 'staff') {
                    $user = Staff::find($this->user_id);
                }
                    if ($this->user_type == 'customer') {
                        $user = Contact::find($this->user_id);
                    }
                    sendEmail($user->email, 'Manifest Consignment Labels PDF ', $url, $attachment = null);
                    // File::delete($bulk_pdf);
                    // File::deleteDirectory($allFilesPath);
                    // foreach ($arr as $file) {
                        File::delete($finalPath);
                    // }
                }
            }

我想在实时服务器上测试它

【问题讨论】:

    标签: php memory


    【解决方案1】:

    您应该能够设置 php memory_limit,将以下行添加到脚本的顶部。

    ini_set('memory_limit','1G');
    

    https://haydenjames.io/understanding-php-memory_limit/

    【讨论】:

    • 这个函数设置内存分配?
    • 我是这么理解的
    • 好的,如果函数需要 50 Mb ram 并且我们只为其分配 20 Mb Ram 会发生什么。会失败吗?
    • 我提供链接的文章解释了这一点
    猜你喜欢
    • 2014-02-15
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 2011-08-21
    • 1970-01-01
    相关资源
    最近更新 更多