简体   繁体   English

PHP中的内存管理和ZipArchive类

[英]Memory Management and ZipArchive class in PHP

I have a PHP script that uses adds a lot of small files into the same zip arhchive file using PHP's ZipArchive class. 我有一个PHP脚本,该脚本使用PHP的ZipArchive类将许多小文件添加到同一zip存档文件中。

Recently script started to run out of memory , although the archive is large, but I add only small files one by one, safely re-opening archive each time I need to add a file. 最近,脚本开始出现内存不足的情况,尽管档案很大,但是我只能一个一个地添加小文件,每次需要添加文件时都可以安全地重新打开档案。

The initial archive file grew little by little to 50 mb so I assume adding little files is not a problem, the real problem might be that whenever ZipArchive class adds a file, it unpacks the whole archive into memory. 最初的存档文件长大一点一点地50 MB所以我想加入少许文件不是问题,真正的问题可能是,每当ZipArchive类增加了一个文件,然后解压缩整个压缩文件到内存中。 Is this correct assumption, can it be so? 这是正确的假设吗?

Memory management isn't one of PHP's strong points. 内存管理不是PHP的强项之一。 I don't see anything in the manual to confirm or dispel the idea that the entire archive is unpacked into memory, but I'd guess that it is. 我在手册中看不到任何内容可以确认或消除整个存档都已解压缩到内存中的想法,但我想是的。

Try comparing the return value of $zip->open() to ZIPARCHIVE::ER_MEMORY - if they're equal, that should confirm that PHP is opening the entire archive in memory. 尝试将$zip->open()的返回值与ZIPARCHIVE::ER_MEMORY -如果它们相等,则应确认PHP正在打开内存中的整个存档。

Another way to confirm it would be to compare the setting of memory_limit ( http://us2.php.net/manual/en/ini.core.php#ini.memory-limit ) to the size of the zip file. 确认它的另一种方法是将memory_limit的设置( http://us2.php.net/manual/en/ini.core.php#ini.memory-limit )与zip文件的大小进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM