2

I got Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 91 bytes) error while trying to backup database on online rest server using Codeigniter dbutil backup() function.

Here is my code:

    $this->load->dbutil();
    $backup =& $this->dbutil->backup(); 
    $this->load->helper('file');
    write_file('./uploads/db/mybackup.gz', $backup);

I can't find out where I am doing wrong.

Sazzad-Ul Islam
  • 33
  • 1
  • 1
  • 4
  • http://php.net/manual/en/ini.core.php#ini.memory-limit . You just used all the memory that was allowed by PHP setup. Increase value in php.ini. And next time read error as clearly states problem. – E_p Jan 12 '16 at 16:51
  • I can increase memory limit on php.ini from local server. How could I increase php.ini value in online server? – Sazzad-Ul Islam Jan 12 '16 at 16:55
  • If u dont have access on production than use ini_set function as I suggest – devpro Jan 12 '16 at 17:00
  • I would go with tools that go with DB without PHP. – E_p Jan 12 '16 at 17:03
  • 1
    You should look into native [mysqldump](http://dev.mysql.com/doc/refman/5.7/en/mysqldump-sql-format.html) rather than relying on CodeIgniter to basically fill up PHP's memory with a bunch of `$output.= 'insert into...';` and then writing the output to the disk. – MonkeyZeus Jan 12 '16 at 17:05
  • 1
    In CodeIgniter 2.1.4 at least, go to `/system/database/drivers/mysql/mysql_utility.php` and find the function `_backup()` to see exactly why your memory is being exhausted. The issue is that the utility is not streaming the data to a file but it is trying to load all of the bytes into memory. – MonkeyZeus Jan 12 '16 at 17:06
  • @MonkeyZeus Why bother if there are tools that can do it without PHP/CodeIgniter. And based on question I doubt that he has knowledge to fix it anyway. – E_p Jan 12 '16 at 17:10
  • @E_p I've outlined what I think are the two most correct and long-lasting solutions, anything else is just a band-aid solution. If OP wants to get out of beginner status then hopefully my two comments can point him in the right direction. My comments were hardly a "bother" to write so why so much negativity? – MonkeyZeus Jan 12 '16 at 17:15
  • I am not sure if your hosting provider is going to allow this but in theory you *could* [execute mysqldump through command-line using php](http://stackoverflow.com/a/6750595). This will avoid any memory issues you would otherwise encounter and will execute much faster than `$this->dbutil->backup();` – MonkeyZeus Jan 12 '16 at 17:19
  • @MonkeyZeus No negativity. Simple facts are: 1) He never used google. 2) He never used search on SO. If you look in too related questions (right hand side) first one is a copy of this question. Why encourage such a behavior? Why expect author of question to change? – E_p Jan 12 '16 at 17:22
  • Thanks all. Have learnt a lot of things from all of you genius mind people...:) – Sazzad-Ul Islam Jan 12 '16 at 17:25
  • @E_p If I am not mistaken, ALL of the questions on the right side point to `ini_set('memory_limit', 'xyz');` solutions besides the one (mine) under the **Linked** header. If I am not mistaken for a second time then increasing the memory limit is a band-aid solution. MVC frameworks are designed to mask many things to "speed" up development which in this case was not beneficial. – MonkeyZeus Jan 12 '16 at 17:27
  • @MonkeyZeus If you had a chance to read my comment (4th from the top) I agree with your answers. Just pointing out lack of effort from questions' author. – E_p Jan 12 '16 at 17:30
  • Possible duplicate of http://stackoverflow.com/questions/561066/fatal-error-allowed-memory-size-of-134217728-bytes-exhausted-codeigniter-xml – Olga Jan 12 '16 at 17:55

2 Answers2

5

You need to increase your script memory by using this:

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

You need to use this line at the top of file.

By increasing memory_limit your script will take unlimited memory usage of server.

If you have access on php.ini file than you can increase limit from php.ini file otherwise you need to add this line.

Side note:

If you think unlimited memory will be effected on other areas than you can set custom value as:

ini_set('memory_limit', '512M');  //increase size as you need
devpro
  • 16,184
  • 3
  • 27
  • 38
0

Database may be too large?

"Due to the limited execution time and memory available to PHP, backing up very large databases may not be possible. If your database is very large you might need to backup directly from your SQL server via the command line, or have your server admin do it for you if you do not have root privileges."

http://www.codeigniter.com/user_guide/database/utilities.html

Edit: As @E_p states, you're running out of memory, but my answer is probably the most likely cause of the error...

Edit 2:

As noted in another answer, I would strongly caution you to not use set your memory usage to unlimited ini_set('memory_limit', '-1');, especially if you're on a shared server.

Friderich Weber
  • 290
  • 2
  • 12