Fatal Error (running out of memory) on writing more than 4000 rows approx
Hello.
I am trying to solve this since 2 days. Also read many threads on stackoverflow. I am trying to write an Excel/CSV file.
My code works fine except when rows exceed somewhere around 4000, with 15 columns, it throws a fatal error saying the memory limit has exhausted. I increased my memory limit in php.ini at 512MB, but no help! Whatever amount of memory I give, it gets exhausted after this limit. I am reading data from text file of around 700 kb. If anyone faced similar thing, please help!
So what cell caching methods have you tried using?
by default, it throws this fatal error.
tried sqlite and phpTemp. page becomes unresponsive in both caching method.
Do you mean unresponsive or simply slower? PHPExcel cannot exceed PHP's memory limits, so cell caching is method to reduce memory usage, but at cost in execution speed
upon using these 2 caching methods, browser says "site is unresponsive"
Are you inserting data through a loop or PHPExcel's fromArray?
@Lemoney I am inserting through loop. because I have to insert data vertically.
sorry for late response. I was unavailable.
@shreygokani the fromArray method allows you to easily put data into a sheet at volume. Like this example:
// example PDO Object
$PDO = new PDO('Connection String');
// Query database for 4000 rows of data
$PDO->query('SELECT * FROM example_table LIMIT 4000');
// Fetch data into an array NOTE: I use FETCH_ASSOC so only one copy returns
$Data = $PDO->fetchAll(PDO::FETCH_ASSOC);
// Start PHPExcel Object
$wb = new PHPExcel();
// ... setting up wb ...
$sheet = $wb->getSheet(0);
/* Take $Data and just dump the array into the table. This would output a
the result in each column by row. EX:
column 1 | column 2 | column 3
-----------------------------------
Row 1 | Row 1 | Row 1
-----------------------------------
Row 2 | Row 2 | Row 2
etc....
*/
$sheet->fromArray($Data, '', 'A2');
Does this solution fix the issue?
@Lemoney fromArray method inputs data row wise. I wish to add data column wise. do you get that? My program is not fetching row from a table. it is doing a file conversion. logic is complex. If I can add my array into column directly, I would not have used loop.
Ah, Okay. I wasn't understanding correctly. My apologies. Okay first question is how many columns are you generating? the full 4000+?
@Lemoney Max columns are 20. its rows, sometimes go more than 4000.
Person I am so sorry for not responding sooner! Friday's are crazy. Are you watching resources while running the script (*nix systems [h]top) or task manager in windows?
@lemoney sorry for late reply. i was experimenting with fromArray. but that too fails! how do i watch resources in php? i m on shared hosting
@shreygokani I am so sorry for the last response. If you are on a shared host try
// default
memory_get_usage();
// all memory
memory_get_usage();
I would do them before trying the insert, then use
try {
} catch ( /* Enter the exception type */ $e {
memory_get_usage();
print_r($e);
}
memory_get_usage();
To see what the differences are. You will need to echo out or insert into a DB. You could be maxing out your VPS. Have you tried
ini_set()
to increase PHP available memory?
@lemoney i am allocating 256mb. do i need more than that? I guess not.
You shouldn't. Without exposing sensitive source code is it possible for you to share the method you are trying to execute?
Hey! memory_get_usage(); was really helpful. also fromArray() it works fine now. issue was with multiple looping. fromArray reduces loops. also, i used function to clear garbage at regular interval. and unset unnecessary variables.
@shreygokani good to hear! In the future if you are running chrome if you type in the address bar:
php.net
hit space and then type you can query the documentation directly, especially if you are already on the PHP.net homepage. You can find just about any language function/feature that way with a few characters