4

Possible Duplicate:
Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php

I've seen similar problems here, but mine is quite different. I am reading from database and writing to an xml file. I get this error

<b>Fatal error</b>:  Allowed memory size of 67108864 bytes exhausted (tried to allocate 4459414 bytes) in <b>.../public/home/..</b> on line <b>32</b><br />.

What should be the solution? Do I increase the memory size in code? Any help?

Community
  • 1
  • 1
karto
  • 3,163
  • 8
  • 40
  • 63
  • 1
    Increasing memory is one solution (until you hit the next time you hit the same problem) or trying to identify why your script is using so much memory in the first place... what are you using to write your xml? are you loading all your database retrieved rows into an array? – Mark Baker Apr 29 '11 at 15:16
  • 2
    please point out why your issue is different. cause and solution to that error are usually always the same: your script ran out of memory for some reason. you either fix the script to consume less memory or increase the memory limit. – Gordon Apr 29 '11 at 15:16
  • Also see [Best XML parser for PHP](http://stackoverflow.com/questions/188414/best-xml-parser-for-php/3616044#3616044) – Gordon Apr 29 '11 at 15:17
  • @Mark Baker: Yes, i'm loading database retrieved rows into an array. – karto Apr 29 '11 at 15:22
  • 1
    @karto - In that case, reduce memory if you can by writing to xml each time you fetch a row... otherwise you have the entire array in memory – Mark Baker Apr 29 '11 at 15:23
  • 1
    @Mark Baker: Tried mysql_unbuffered_query() suggested by @PVM and it works quite fine. – karto Apr 29 '11 at 15:36

2 Answers2

13

Try to increase or remove memory_limit in php.ini.

Find something like this and change value of memory_limit

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 128M

(this is part of my php.ini)

Restart server after saving changes.

Also, you can try to remove limit from code.

Example:

ini_set('memory_limit', '-1');

That should work fine.

Another way is setting memory limit from .htaccess file by adding line:

php_value memory_limit 128M

(128M is just an example)

Note: For shared hosting I highly doubt you can change memory limit.

Wh1T3h4Ck5
  • 8,045
  • 9
  • 52
  • 74
3

Do a mysql_unbuffered_query() rather than mysql_query(). Your query is probably returning too much data as is for your php server to handle.

Pratik Butani
  • 51,868
  • 51
  • 228
  • 375
Parris Varney
  • 10,663
  • 12
  • 44
  • 71
  • please explain what happens with mysql_unbuffered_query() – karto Apr 29 '11 at 15:21
  • mysql_unbuffered_query() only fetches one record from your result set from the database at a time, so php only has to store a much smaller amount of data into memory. mysql_query() fetches the entire results set into memory. Documentation at: http://php.net/manual/en/function.mysql-unbuffered-query.php – Parris Varney Apr 29 '11 at 17:10