【问题标题】:How fix Fatal error: Out of memory (allocated ) (tried to allocate bytes) in php如何修复致命错误:php中的内存不足(已分配)(试图分配字节)
【发布时间】:2013-01-18 07:08:43
【问题描述】:

我将文件 csv 的值读取为:

 //$mypath . '/' . $filename <=> ../abc.csv
 $val = file_get_contents($mypath . '/' . $filename);                                       

 $escaped = pg_escape_bytea($val);

 $model->addFileImport($tmp, $data['email'], $escaped);

我的文件大约 100MB。 在 php.ini 设置中: memory_limit = 128M

但它仍然显示 errort:Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 133120 bytes) in... at row: $val = file_get_contents($mypath . '/' . $filename);

我已通过添加 ini_set('memory_limit', '-1'); 修复:

 //$mypath . '/' . $filename <=> ../abc.csv
 ini_set('memory_limit', '-1');
 $val = file_get_contents($mypath . '/' . $filename);                                       

 $escaped = pg_escape_bytea($val);

 $model->addFileImport($tmp, $data['email'], $escaped);

但它显示错误:

致命错误:C:\wamp\www\joomlandk\components\com_servicemanager\views\i0701\view.html.php 第 112 行内存不足(已分配 230686720)(试图分配 657099991 字节)

在行$escaped = pg_escape_bytea($val);

为什么?如何修复该错误?

【问题讨论】:

  • 你的内存用完了,据我所知是物理内存。 657099991 字节是 626.659 兆字节。你最好的选择是use a bigger swap file

标签: php


【解决方案1】:

根据the doc

pg_escape_bytea() 为 bytea 数据类型转义字符串。它返回 转义字符串。

当你选择一个 bytea 类型时,PostgreSQL 返回八进制字节值 以 '\' 为前缀(例如 \032)。用户应该转换回 手动二进制格式。

表示单个输入字节给出 4 个字节,即初始大小的 4 倍

您需要大量 RAM 来处理您的文件(也许您的系统无法分配那么多内存 - 即使没有 PHP 限制)。一种解决方案是从 40MB 块中处理它,例如使用 fread()fwrite() 函数。

  $val = file_get_contents($mypath . '/' . $filename);

将占用 100MB - 因此下一行占用 400 MB,总共 500 MB。您需要从 *file_get_contents* 中读取更少的内容,例如一次只读取 20(或 40)MB

  • 使用 fread(而不是 file_get_contents)读取 20MB 的文件
  • 使用 *pg_escape_bytea* 处理这 20MB(总共 100MB)
  • 重复该过程,直到文件完全处理完毕

【讨论】:

  • 但它在行显示错误:$escaped = pg_escape_bytea($val);我必须将字符串转换为 bytea 插入数据库。
  • 是的,因为第一行file_get_contents占用100MB,然后pg_escape_bytea行占用4次,即400MB。总共 500MB... 这就是错误出现的原因 - 如果您只能 file_get_contents (比如说)25MB,那么总数将只有 125MB。查看编辑
  • 我可以为网络服务器设置内存吗?
【解决方案2】:

尝试添加:

ini_set('memory_limit', '700M');

【讨论】:

  • 它显示错误:致命错误:C:\wamp\www\joomlandk\components\com_servicemanager\views\i0701\view.html 中允许的内存大小为 734003200 字节已用尽(试图分配 657099991 字节) .php 第 114 行:$val = file_get_contents($mypath . '/' . $filename);
猜你喜欢
  • 1970-01-01
  • 2018-12-03
  • 2014-04-04
  • 2011-09-12
  • 1970-01-01
  • 2017-03-02
  • 2018-01-14
  • 1970-01-01
  • 2014-05-27
相关资源
最近更新 更多