【问题标题】:Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 373 bytes) in [mypath] on line 65致命错误:第 65 行 [mypath] 中允许的内存大小为 134217728 字节已用尽(尝试分配 373 字节)
【发布时间】:2014-01-04 13:23:15
【问题描述】:

我正在尝试实现Converting single sheet in an XLS file to CSV with PHPExcel - Memory exhausted,但我仍然遇到内存问题。

想想@BT643 代码 - 我使用的 - 包含了@Mark Ba​​ker 在他的答案 (phpexcel Allowed memory size of 134217728 bytes exhausted) 编译的各种帖子中解释的大部分黄金法则。

我的 excel 文件有 61 列和大约 8000 行,所以大约有 50 万个单元格。大小 5561KB。我不希望它需要阅读“块”。

我错过了什么?

这是我正在使用的代码:

include 'Classes/PHPExcel.php';
include 'Classes/PHPExcel/IOFactory.php';

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = array( 'memoryCacheSize' => '2GB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

$reader = PHPExcel_IOFactory::createReader('Excel5');
$reader->setReadDataOnly(true);
$reader->setLoadSheetsOnly('mytab');

$filename = 'C:/xampp/htdocs/mycode/myfile.xls';

$excel = $reader->load($filename);

$filename_fixed='myfile.csv';
$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->save($filename_fixed);

【问题讨论】:

  • 您只允许此文档使用 128MB 的 RAM。你可能需要更多。
  • @Brad。 ini_set("memory_limit","256M"); + ini_set('max_execution_time', 300); 解决了这个问题!一堆!

标签: php memory csv memory-management phpexcel


【解决方案1】:

memoryCacheSize 设置为 2GB 不是一个好主意。这意味着您告诉 PHPExcel 在考虑使用 phptemp 磁盘缓存数据之前使用最多 2GB 的服务器内存(可能高于允许的 php 内存的限制)。如果你有 2GB 的可用内存,你可能不需要缓存:memoryCacheSize 的值应该设置为 PHPExcel 在切换到使用 phptemp 磁盘缓存之前应该允许使用你的可用 php 内存的最大值

【讨论】:

  • tks!事实上,如果我评论这三个 cashMethod 行,我的新代码仍然有效。但是我仍然需要大约 40 秒来运行它 (ini_set('max_execution_time', 40);)。我想知道是否有更快的方法...
  • 任何缓存的使用都会对速度产生不利影响,并且内存/性能比会根据您的电子表格而有所不同;尝试其他一些缓存方法,看看哪种方法可以在您的可用内存范围内提供最佳速度
猜你喜欢
  • 1970-01-01
  • 2011-09-27
  • 2015-03-08
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 2011-04-10
  • 1970-01-01
  • 2018-08-01
相关资源
最近更新 更多