php - Fatal error: Allowed memory size of 134217728 bytes exhausted
There is an infinite loop because you forgot to increment $i.
$i = 0;
while ($i < $rows) {
array_push($columnNames, toCamelCase($firstRowNames[$i]));
}
better:
$i = 0;
while ($i < $rows) {
array_push($columnNames, toCamelCase($firstRowNames[$i]));
++$i;
}
Memory available bigger than tried error in PHP
The error message tells you that a limit of 134217728 bytes (128 MB) was defined. This has already been used by your script, but it requested more. Allocating the next bytes (in your example: 20480) failed, that's why the script died.
To resolve this, you could either check why your script needs that memory (reducing the memory footprint can always be a good idea), or otherwise set a higher memory limit
Why is CodeIgniter exhausing allowed memory size?
Found the answer:
$this->load->database(); // init db connection, already in code
$this->db->save_queries = false; // ADD THIS LINE TO SOLVE ISSUE
This is a lovely undocumented setting in CodeIgniter. CI apparently saves queries by default, even a certain amount of data is saved relative to insert / update queries. With the massive amount of inserts run during this import process, this memory leak became very consequential. Setting CI to not save queries solved the problem.
What threw me off was that memory_get_peak_usage()
was reporting the memory usage increasing before the insert query was run, not during it (PHP bug?).
As a final reality check, I removed all the other optimization recommendations (unset
, clearstatcache
, etc) and verified that they had no positive impact on the memory issue.
Related Topics
How to Use Multiple Databases in Laravel
Using Jquery $.Ajax to Call a PHP Function
How to Get a Variable Name as a String in PHP
How to Use Store and Use Session Variables Across Pages
"Fatal Error: Cannot Redeclare ≪Function≫"
How to Get the Current Date and Time in PHP
How to Debug in Woocommerce 3+
The Plugin Generated X Characters of Unexpected Output During Activation (Wordpress)
MySQLi Update Throwing Call to a Member Function Bind_Param() Error
When Should I Use 'Self' Over '$This'
Convert PHP Array to JavaScript
Open_Basedir Restriction in Effect. File(/) Is Not Within the Allowed Path(S):
Passing Base64 Encoded Strings in Url