PHPExcel icon indicating copy to clipboard operation
PHPExcel copied to clipboard

Fatal Error (running out of memory) on writing more than 4000 rows approx

Open shreygokani opened this issue 10 years ago • 18 comments

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!

shreygokani avatar Aug 21 '16 17:08 shreygokani

So what cell caching methods have you tried using?

MarkBaker avatar Aug 21 '16 17:08 MarkBaker

by default, it throws this fatal error.

tried sqlite and phpTemp. page becomes unresponsive in both caching method.

shreygokani avatar Aug 21 '16 17:08 shreygokani

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

MarkBaker avatar Aug 21 '16 18:08 MarkBaker

upon using these 2 caching methods, browser says "site is unresponsive"

shreygokani avatar Aug 21 '16 18:08 shreygokani

Are you inserting data through a loop or PHPExcel's fromArray?

abstract-base-method avatar Aug 25 '16 20:08 abstract-base-method

@Lemoney I am inserting through loop. because I have to insert data vertically.

shreygokani avatar Sep 13 '16 06:09 shreygokani

sorry for late response. I was unavailable.

shreygokani avatar Sep 13 '16 06:09 shreygokani

@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?

abstract-base-method avatar Sep 13 '16 13:09 abstract-base-method

@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.

shreygokani avatar Sep 14 '16 12:09 shreygokani

Ah, Okay. I wasn't understanding correctly. My apologies. Okay first question is how many columns are you generating? the full 4000+?

abstract-base-method avatar Sep 14 '16 17:09 abstract-base-method

@Lemoney Max columns are 20. its rows, sometimes go more than 4000.

shreygokani avatar Sep 16 '16 05:09 shreygokani

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?

abstract-base-method avatar Sep 17 '16 00:09 abstract-base-method

@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 avatar Sep 21 '16 13:09 shreygokani

@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?

abstract-base-method avatar Sep 22 '16 22:09 abstract-base-method

@lemoney i am allocating 256mb. do i need more than that? I guess not.

shreygokani avatar Sep 23 '16 14:09 shreygokani

You shouldn't. Without exposing sensitive source code is it possible for you to share the method you are trying to execute?

abstract-base-method avatar Sep 23 '16 14:09 abstract-base-method

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 avatar Sep 23 '16 17:09 shreygokani

@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

abstract-base-method avatar Sep 23 '16 22:09 abstract-base-method