Question on how memory is managed

Topics: User Forum
Nov 18, 2012 at 3:50 PM

Howdy all,

I'm having the usual problems with memory allocation when dealing with large spreadsheets.  I've been reading the threads and trying all the various caching options.  I'm still seeing a lot of crashing, but that might be because I have a fundamental misunderstanding of how things tie together.

Is caching also bound to the memory limit set in the php.ini (or locally, in code, if possible)?


I have 16Gb of RAM allocated to this server.  If I turn the memory limit off I can regularly crash apache (obviously).  If I set any limit at all I always run out.  I was experimenting with the various caching options, but even with the caching options I always get out of memory errors.  I've used this bit of code that I saw recommended to ensure that the caching option is invoked properly:


if (!PHPExcel_Settings::setCacheStorageMethod($cacheMethod,$cacheSettings)) die('CACHEING ERROR');

Thanks for any help,



Nov 18, 2012 at 10:53 PM

PHP has a memory limit defined for any thread executing a PHP script. This limit is defined in the php.ini file, and defines the maximum amount of memory that can be used by PHP scripts... that comprises the PHP script code (including PHPExcel and all its classes), and any data that PHP holds in memory (such as variables, object instances, etc). As PHPExcel works with an "in-memory" model, all cells are (by default) loaded as cell objects into that limited PHP memory. As a typical rule of thumb, each cell requires an average of 1k of PHP memory if using 32-bit PHP, 1.6k if using 64-bit PHP.

You might have 16GB of memory in your server, but the PHP memory limit defined in php.ini is likely to be a great deal less than that (and unless you are using a 64-bit version of PHP, you can't even use 16GB of memory with PHP).


Cell caching provides alternatives to the way cell data is loaded into memory, providing options to reduce the amount of memory required for each loaded cell. cache_in_memory is the default behaviour, maintaining each cell as a cell object in memory. Options such as cache_in_memory_serialized, cache_igbinary and cache_in_memory_gzip store the cell object in a serialised or a compressed format in memory, which typically takes less memory than an object instance. A cell only exists as a cell object instance while it is actually being accessed. cache_to_discISAM, cache_to_phpTemp, cache_to_apc, cache_to_memcache and cache_to_wincache store the cell data outside of PHP memory; but maintain an index in PHP memory telling PHPExcel where the cell data is stored. cache_to_sqlite and cache_to_sqlite3 are the most memory efficient, using a SQLite database to store cell data in an indexed database table, so no "in memory" index is required. Again, a cell object instance will only exist in memory while actually being accessed.


Nov 19, 2012 at 4:22 PM

Thanks for the reply, Mark, appreciated.

We're running the 64-bit version of php:

file /usr/bin/php
/usr/bin/php: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), stripped


I'm not sure where to begin troubleshooting then.  With these caching methods I'm still running out of memory: cache_to_discISAM, cache_to_phpTemp, cache_to_apc.  I'm still trying to get memcache(d) to work properly, so I don't have any data for that yet.