下载ZipArchive OutOfMemory

I've an issue when downloading a file on my heroku app. I've a basic PHP ini wich allow us to have 128MB, for memory limit.

My Error is as follow :

(1/1) OutOfMemoryException Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 77709312 bytes)

Ok I got it, but the weird thing is 77709312bytes are only 74MB.

So what's wrong ? This a the code of my download action :

/**
 * @Route("/templates/{id}/download", name="api_templates_download", requirements={"id"="\d+"})
 * @Method({"GET"})
 * @param \Symfony\Component\HttpFoundation\Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 * @throws \Exception
 */
public function downloadTemplate(Request $request)
{
    $id = $request->get('id');

    try {
        $template = $this->service->getTemplate($id, $this->helper->getUser());
        $archive = $this->service->downloadTemplate($template);
        $response = new Response(file_get_contents($archive));
        $response->headers->set('Content-Type', 'application/zip');
        $response->headers->set('Content-Disposition', 'attachment;filename="' . $archive . '"');
        $response->headers->set('Content-length', filesize($archive));

        return $response;
    } catch (\Exception $e) {
        $response = new Response(json_encode($e->getMessage()), 401);
        $response->headers->set('Content-Type', 'application/json');
        return $response;
    }
}

And the method downloadTemplate called in the controller :

/**
 * @tests Need to tests about performance - do this in september with real server
 * @param \App\Domain\Dollycast\Template\Entity\Template $template
 * @return string
 */
public function downloadTemplate(Template $template)
{
    $zip = new \ZipArchive();
    $zipName = self::ZIPDIR . $template->getName() . '.zip';

    $zip->open($zipName, \ZipArchive::CREATE);

    $finder = new Finder();
    $finder->files()->in(self::WORKDIR . $template->getName());

    $zip->addEmptyDir($template->getName());
    /** @var SplFileInfo $file */
    foreach ($finder as $file) {
        // Rename the full path to the relative Path
        $zip->addFile(
            self::WORKDIR . $template->getName() . DIRECTORY_SEPARATOR . $file->getRelativePathname(),
            $template->getName() . DIRECTORY_SEPARATOR . $file->getRelativePathname()
        );
    }

    $zip->close();

    return $zipName;
}

file_get_contents will load the file into memory, when I put this into the Response object, this will effectively copy all the content from memory to the output buffer. This is why an archive wich is around 74MB will result in Out of memory for 128M configuration (74*2) error due to double work.

At this time, I can't use readfile() to solve this problem because the behaviour is too much different for my needs. But readfile() will open and output into buffer without using twice memory.

EDIT : The best alternative I found is by using the BinaryFileResponse object from Symfony 3.2 wich handle effectively the response stream.

(1/1) OutOfMemoryException Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 77709312 bytes)

At the time, the php proccess tries to allocate 77709312 bytes it already has memory allocated, which can be checked with the function memory_get_usage(). In your case, this must be more than (134217728 - 77709312 =) 56508416 bytes.

If the exception is raised during the creation of the zip file, you can try to use an external tool to create the zip file. Something like

exec("tar -zcvf archive.tar.gz /folder/tozip");

Otherwise try to find out when exactly this exception is thrown an try to free all unnecessary used memory.

Did you try file_get_contents($zipName); unlink($zipName); so you can delete file from local