I have created a PHP script which scrapes some particular data from a website, looks for the relevant information and store this in a database. The script works fine on the local machine but when i run it on a live server using cron job, it stops after doing 10 database inserts saying
我创建了一个PHP脚本,它从网站上抓取一些特定数据,查找相关信息并将其存储在数据库中。该脚本在本地计算机上运行正常,但是当我使用cron作业在实时服务器上运行时,它会在执行10个数据库插入后停止
"Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 35 bytes) in /home/content/36/78632936/html/scripts/simple_html_dom.php on line 809
"
“致命错误:在809行的/home/content/36/78632936/html/scripts/simple_html_dom.php中,允许的内存大小为67108864字节耗尽(试图分配35个字节)”
I checked the same script on my local using get_memory_peak_usage() and the maximum usage comes around 8 MB .
我使用get_memory_peak_usage()在本地检查了相同的脚本,最大使用量大约为8 MB。
I am puzzled as to why the memory usage is spectacularly high on the live machine.. Any help will be appreciated..
我很困惑为什么在现场机器上的内存使用率非常高..任何帮助将不胜感激..
Glimpse of my code:
一瞥我的代码:
DB_table1: contains list of 60000 rows of data
DB_table1:包含60000行数据的列表
Main code starts with calling the DB1 and get the data and then use each row of data to form an URL. Each of these URLs will be scraped using simple_html_dom() function and the script will look for some particular information on each page and store this data in another table DB_table2.
主代码从调用DB1开始并获取数据,然后使用每行数据形成一个URL。这些URL中的每一个都将使用simple_html_dom()函数进行抓取,脚本将在每个页面上查找某些特定信息,并将此数据存储在另一个表DB_table2中。
Please let me know if you need to know anything else . Thanks :)
如果您还需要了解其他信息,请与我们联系。谢谢 :)
2 个解决方案
#1
0
Don't forget to use unset()
after while/foreach to cleanup no longer required (DB) results.
不要忘记在while / foreach之后使用unset()来清除不再需要(DB)的结果。
#2
0
Update:
Looking at the php change log there are a lot of memory issues that are address. If you are using part of PHP that has a leak in it in 5.2 it is possible that could be a cause. If you want to know for sure try wrapping memory intensive part of your code with differences in memory_get_usage and see if there are any discrepancies between the memory usage.
更新:查看php更改日志有很多内存问题需要解决。如果您正在使用5.2中有泄漏的PHP部分,则可能是原因。如果您想确切地知道,请尝试使用memory_get_usage中的差异来包装内存密集型代码,并查看内存使用情况之间是否存在任何差异。
Question/Answer:
提问/回答:
afuzzyllama: Is your production environment the same as your local environment? It is possible your production db has a lot more data than your sandbox.
afuzzyllama:您的生产环境是否与您当地的环境相同?您的生产数据库可能比沙箱拥有更多数据。
user743914: Unfortunately I didn't notice the difference previously, My production server is running php version 5.2 while my localhost is on 5.3.1 ... Production DB has same amount of data as that of localhost...
user743914:不幸的是我之前没有注意到差异,我的生产服务器运行的是php 5.2版,而我的localhost是在5.3.1 ...生产数据库的数据量与localhost的数据量相同...
Simple solution:
What is the maximum memory limit your server allows?
简单的解决方案:服务器允许的最大内存限制是多少?
Search for 'memory_limit' on this page [php.net].
在此页[php.net]上搜索'memory_limit'。
As you see in the docs, depending on your version of PHP your limit could be as low as 8mb. I will guess that your limit is set to ~64M right now since your script is failing at ~67mb.
正如您在文档中看到的,根据您的PHP版本,您的限制可能低至8mb。我猜你的限制现在设置为~64M,因为你的脚本失败了~67mb。
If you need to raise it temporarily you can use ini_set() or you can set it in your php.ini file and restart your server.
如果你需要临时提升它,你可以使用ini_set(),或者你可以在php.ini文件中设置它并重启你的服务器。
#1
0
Don't forget to use unset()
after while/foreach to cleanup no longer required (DB) results.
不要忘记在while / foreach之后使用unset()来清除不再需要(DB)的结果。
#2
0
Update:
Looking at the php change log there are a lot of memory issues that are address. If you are using part of PHP that has a leak in it in 5.2 it is possible that could be a cause. If you want to know for sure try wrapping memory intensive part of your code with differences in memory_get_usage and see if there are any discrepancies between the memory usage.
更新:查看php更改日志有很多内存问题需要解决。如果您正在使用5.2中有泄漏的PHP部分,则可能是原因。如果您想确切地知道,请尝试使用memory_get_usage中的差异来包装内存密集型代码,并查看内存使用情况之间是否存在任何差异。
Question/Answer:
提问/回答:
afuzzyllama: Is your production environment the same as your local environment? It is possible your production db has a lot more data than your sandbox.
afuzzyllama:您的生产环境是否与您当地的环境相同?您的生产数据库可能比沙箱拥有更多数据。
user743914: Unfortunately I didn't notice the difference previously, My production server is running php version 5.2 while my localhost is on 5.3.1 ... Production DB has same amount of data as that of localhost...
user743914:不幸的是我之前没有注意到差异,我的生产服务器运行的是php 5.2版,而我的localhost是在5.3.1 ...生产数据库的数据量与localhost的数据量相同...
Simple solution:
What is the maximum memory limit your server allows?
简单的解决方案:服务器允许的最大内存限制是多少?
Search for 'memory_limit' on this page [php.net].
在此页[php.net]上搜索'memory_limit'。
As you see in the docs, depending on your version of PHP your limit could be as low as 8mb. I will guess that your limit is set to ~64M right now since your script is failing at ~67mb.
正如您在文档中看到的,根据您的PHP版本,您的限制可能低至8mb。我猜你的限制现在设置为~64M,因为你的脚本失败了~67mb。
If you need to raise it temporarily you can use ini_set() or you can set it in your php.ini file and restart your server.
如果你需要临时提升它,你可以使用ini_set(),或者你可以在php.ini文件中设置它并重启你的服务器。