PHP内存调试

发布于 2024-12-29 12:16:25 字数 2149 浏览 3 评论 0原文

对于我的项目之一,我需要导入一个非常大的文本文件(~ 950MB)。我正在使用 Symfony2 &我的项目的原则 2。

我的问题是,我收到如下错误:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 24 bytes)

如果我将内存限制增加到 1GB,甚至会出现该错误。

我尝试使用 XDebug 和 KCacheGrind (作为 PHPEdit 的一部分)来分析问题,但我并不真正理解这些值:(

我正在寻找一个工具或方法(快速和简单,因为我没有太多时间)来找出为什么内存被分配并且没有再次释放

编辑

要清除这里的一些内容是我的代码:

$handle = fopen($geonameBasePath . 'allCountries.txt','r');

        $i = 0;
        $batchSize = 100;

        if($handle) {
            while (($buffer = fgets($handle,16384)) !== false) {

                if( $buffer[0] == '#') //skip comments
                    continue;
                //split parts
                $parts = explode("\t",$buffer);


                if( $parts[6] != 'P')
                    continue;

                if( $i%$batchSize == 0 )    {
                    echo 'Flush & Clear' . PHP_EOL;
                    $em->flush();
                    $em->clear();
                }

                $entity = $em->getRepository('MyApplicationBundle:City')->findOneByGeonameId( $parts[0] );
                if( $entity !== null)   {
                    $i++;
                    continue;
                }

                //create city object
                $city = new City();

                $city->setGeonameId( $parts[0] );
                $city->setName( $parts[1] );
                $city->setInternationalName( $parts[2] );
                $city->setLatitude($parts[4] );
                $city->setLongitude( $parts[5] );
                $city->setCountry( $em->getRepository('MyApplicationBundle:Country')->findOneByIsoCode( $parts[8] ) );

                $em->persist($city);

                unset($city);
                unset($entity);
                unset($parts);
                unset($buffer);

                echo $i . PHP_EOL;


                $i++;
            }
        }

        fclose($handle);

我已经尝试过,但没有任何帮助:

  1. 添加第二个参数。 fgets
  2. 增加内存限制
  3. 取消设置变量

For one off my projects I need to import a very huge text file ( ~ 950MB ). I'm using Symfony2 & Doctrine 2 for my project.

My problem is that I get errors like:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 24 bytes)

The error even occurs if I increase the memory limit to 1GB.

I tried to analyze the problem by using XDebug and KCacheGrind ( as part of PHPEdit ), but I don't really understand the values :(

I'am looking for a tool or a method (Quick & Simple due to the fact that I don't have much time) to find out why memory is allocated and not freed again.

Edit

To clear some things up here is my code:

$handle = fopen($geonameBasePath . 'allCountries.txt','r');

        $i = 0;
        $batchSize = 100;

        if($handle) {
            while (($buffer = fgets($handle,16384)) !== false) {

                if( $buffer[0] == '#') //skip comments
                    continue;
                //split parts
                $parts = explode("\t",$buffer);


                if( $parts[6] != 'P')
                    continue;

                if( $i%$batchSize == 0 )    {
                    echo 'Flush & Clear' . PHP_EOL;
                    $em->flush();
                    $em->clear();
                }

                $entity = $em->getRepository('MyApplicationBundle:City')->findOneByGeonameId( $parts[0] );
                if( $entity !== null)   {
                    $i++;
                    continue;
                }

                //create city object
                $city = new City();

                $city->setGeonameId( $parts[0] );
                $city->setName( $parts[1] );
                $city->setInternationalName( $parts[2] );
                $city->setLatitude($parts[4] );
                $city->setLongitude( $parts[5] );
                $city->setCountry( $em->getRepository('MyApplicationBundle:Country')->findOneByIsoCode( $parts[8] ) );

                $em->persist($city);

                unset($city);
                unset($entity);
                unset($parts);
                unset($buffer);

                echo $i . PHP_EOL;


                $i++;
            }
        }

        fclose($handle);

Things I have tried, but nothing helped:

  1. Adding second parameter to fgets
  2. Increasing memory_limit
  3. Unsetting vars

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉月流沐 2025-01-05 12:16:25

增加内存限制是不够的。当导入这样的文件时,您可以缓冲读取。

$f = fopen('yourfile');
while ($data = fread($f, '4096') != 0) {
    // Do your stuff using the read $data
}
fclose($f);

更新:

使用 ORM 时,您必须了解在刷新调用之前实际上没有任何内容插入到数据库中。这意味着所有这些对象都由标记为“要插入”的 ORM 存储。只有当进行flush调用时,ORM才会检查集合并开始插入。

解决方案 1:经常冲洗。并且清晰。

解决方案 2:不要使用 ORM。使用简单的 SQL 命令。它们占用的内存比对象 + ORM 解决方案要少得多。

Increasing memory limit is not going to be enough. When importing files like that, you buffer the reading.

$f = fopen('yourfile');
while ($data = fread($f, '4096') != 0) {
    // Do your stuff using the read $data
}
fclose($f);

Update :

When working with an ORM, you have to understand that nothing is actually inserted in the database until the flush call. Meaning all those objects are stored by the ORM tagged as "to be inserted". Only when the flush call is made, the ORM will check the collection and start inserting.

Solution 1 : Flush often. And clear.

Solution 2 : Don't use the ORM. Go for plain SQL command. They will take up far less memory than the object + ORM solution.

鸠魁 2025-01-05 12:16:25

33554432 是 32MB

更改 php.ini 中的内存限制,例如 75MB

memory_limit = 75M

并重新启动服务器

33554432 are 32MB

change memory limit in php.ini for example 75MB

memory_limit = 75M

and restart server

兰花执着 2025-01-05 12:16:25

您应该逐行读取文件,而不是简单地读取文件。每次您阅读一行时,您都应该处理您的数据。不要试图将所有内容都放入内存中。你会失败的。原因是,虽然您可以将 TEXT 文件放入 ram 中,但您将无法同时将数据作为 php 对象/变量/whathaveyou,因为 php 本身需要大量的内存来存储每个文件。其中。

我建议的是
a) 读取新行,
b) 解析该行中的数据
c) 创建新对象以存储在数据库中
d) 转到步骤 a,首先取消设置(ting)旧对象或重用它的内存

Instead of simply reading the file, you should read the file line by line. Every time you do read the one line you should process your data. Do NOT try to fit EVERYTHING in memory. You will fail. The reason for that is that while you can put the TEXT file in ram, you will not be able to also have the data as php objects/variables/whathaveyou at the same time, since php by itself needs much larger amounts of memory for each of them.

What I instead suggest is
a) read a new line,
b) parse the data in the line
c) create the new object to store in the database
d) goto step a, by unset(ting) the old object first or reusing it's memory

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文