• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fatal error: Allowed memory size of xxxbytes exhausted

#1
HI,
I am getting this error:
Code:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in /home/mywhdms/public_html/am/system/database/drivers/pdo/pdo_result.php on line 195

I am using CodeIgniter V3 and DB Driver is PDO. Trying to retrieve 25000 records for calculation of values.

My DB Settings are as follows :
Code:
$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
       'dsn'=> 'mysql:host=XXXXX;dbname=XXXXXX',
'hostname' => 'localhost',
'username' => 'XXXXX',
'password' => 'XXXXX',
'database' => 'XXXXX',
'dbdriver' => 'pdo',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,//FALSE
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Reply

#2
You PHP settings has a maximum amount of memory set for each PHP request. In your case 128 MB. Your code/query need more memory to run completely then PHP has to offer. Basicly the script stops executing as soon as the limit is reached.

To resolve you can
1) alter the setting inside php.ini: memory_limit: 256M. Some hosts (not all) let you alter this setting through .htaccess or inside you PHP script. You can check google for the correct syntax.
2) You can optimize your own code and queries so it can run the whole script without hitting the current limit of 128 MB. For example, be selective in your query, don't use "SELECT * FROM X" if you only need 2 columns.
Reply

#3
... and if there's another way, don't fetch 25k rows in the first place.
Reply

#4
Sad 
(09-10-2015, 02:38 AM)Narf Wrote: ... and if there's another way, don't fetch 25k rows in the first place.

Actually its an activity table, so I need to calculate total number of activity happened, like xx Success, xx Pending and so on. Actually site traffic increased in just 2 days , now today number of records are almost 70000, can you suggest me any work around. Currently just in mess to figure-out how to handle that.
Reply

#5
http://www.codeigniter.com/user_guide/da...esult-rows
Read about unbuffered_row() method.

Edit: Or if you need only sums or counts, then if it is possible let the SQL server do them.
Reply

#6
in any circumstance you should prevent calculations like that in PHP

most of the time if you want to calculate something like that with this type of data you should think about triggers
MYSQL Trigger Doku

Basically you can set after and before triggers in order to react with your action. Allowed are, on update, on insert and on delete triggers.
Reply

#7
If you just want the counts, use $this->db->count_all() or $this->db->count_all_results().

If you need both, you should probably use one of the above functions for the count and page the actual data.
Reply

#8
If can't just make a "select count" and you really need the data for each rows to make your stats, select only the columns you need and set save_queries to FALSE in database configuration. Depending on how much query you make, it can use a lot of memory. I can't find it anywhere in the user guide, but it should be set to FALSE in production!

From the config/database.php file :

PHP Code:
|    ['save_queries'TRUE/FALSE Whether to "save" all executed queries.
|                 
NOTEDisabling this will also effectively disable both
|                 $this->db->last_query() and profiling of DB queries.
|                 
When you run a querywith this setting set to TRUE (default),
|                 
CodeIgniter will store the SQL statement for debugging purposes.
|                 
Howeverthis may cause high memory usageespecially if you run
|                 a lot of SQL queries ... disable this to avoid that problem
Test your translation files with Translation Tester
Reply

#9
Hi,
Thanks for the replies, but currently issue is the still same:
What I have done:
1-Changed memory limit to : 256M in php ini
2- Corrected maxm. queries to use unbuffered_row() method.
What I observed:
1-If I am changing DB drivers to mysqli its working fine.
2-if I am setting it to PDO same error is coming. i.e. Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 12288 bytes) in /home/xxxx/public_html/am/application/modules/clients/views/view.php on line 103

Is there any work around for PDO to work fine? If so, can you people guide me.
Reply

#10
(10-19-2015, 05:55 AM)chandrabhanu Wrote: 1-Changed memory limit to : 256M in php ini
...
2-if I am setting it to PDO same error is coming. i.e. Fatal error: Allowed memory size of 134217728 bytes exhausted

Then you're still limited to 128MB and not 256MB. You need to restart Apache for your change to take effect.

...and, did you set save_queries to false ?
Test your translation files with Translation Tester
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.