PHPExcel solves the problem of excessive memory usage-Setting cell object cache PHPExcel is a very powerful PHP Open Source class for processing Excel, but a big problem is that it occupies too much memory, starting from 1.7.3, it supports cell caching, but it is recommended to use stable version 1.7.6 because PHPExcel is used in earlier versions to solve the problem of excessive memory usage-set cell object caching
PHPExcel is a powerful PHP open-source class for processing Excel, but a major problem is that it occupies too much memory. from 1.7.3, PHPExcel supports cell caching, however, we recommend that you use the stable version 1.7.6, because previous versions have bugs of different degrees. The following is the official documentation:
?
PHPExcel1.7.6 official documentation
PHPExcel uses an average of about 1 k/cell in your worksheets, so large workbooks can quickly use up available memory. cell caching provides a mechanisms that allows PHPExcel to maintain the cell objects in a smaller size of memory, on disk, or in APC, memcache or Wincache, rather than in PHP memory. this allows you to reduce the memory usage for large workbooks, although at a cost of speed to access cell data.
?
PHPExcel uses 1 k/cell memory on average. Therefore, large files may cause high memory consumption. The cell Cache mechanism allows PHPExcel to cache small cell objects in memory in disks, APC, memcache, or Wincache, although it takes some time to read data, but it can help you reduce memory consumption.
?
PHPExcel1.76. according to official documents, By default, PHPExcel still holds all cell objects in memory, but you can specify alternatives. to enable cell caching, you must call the PHPExcel_Settings: setCacheStorageMethod () method, passing in the caching method that you wish to use .?
By default, PHPExcel still saves cell objects in memory, but you can customize them. You can use the PHPExcel_Settings: setCacheStorageMethod () method to pass the cache method as a parameter to this method to set the cache method.
?
?
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory;PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
? PHPExcel1.7.6 official documentation writes setCacheStorageMethod () will return a boolean true on success, false on failure (for example if trying to cache to APC when APC is not enabled ).
? The setCacheStorageMethod () method returns a boolean variable to indicate whether the setting is successful. (for example, if APC cannot be used, if you set to use APC cache, false is returned)
?
PHPExcel1.7.6 official documentation writes that A separate cache is maintained for each individual worksheet, and is automatically created when the worksheet is instantiated based on the caching method and settings that you have configured. you cannot change the configuration settings once you have started to read a workbook, or have created your first worksheet.
?
Each worksheet has an independent cache. when a worksheet is instantiated, it is automatically created based on the configured or configured cache mode. Once you start to read a file or you have created the first worksheet, you cannot change the caching method.
?
PHPExcel1.7.6 official documentation writes Currently, the following caching methods are available.
? Currently, the following cache methods are available:
?
PHPExcel_CachedObjectStorageFactory::cache_in_memory;
PHPExcel1.7.6 official documents write The default. If you don't initialise any caching method, then this is the method that PHPExcel will use. Cell objects are maintained in PHP memory as at present.
? By default, if you do not initialize any caching method, PHPExcel uses the memory cache method.
========================================================== ========
?
PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;
PHPExcle1.7.6 official documents write Using this caching method, cells are held in PHP memory as an array of serialized objects, which has CES the memory footprint with minimal performance overhead.
? Using this cache method, cells are stored in the memory in a serialized manner, which is a solution to reduce memory usage performance.
========================================================== ========
?
PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
PHPExcel1.7.6 official documents write Like cache_in_memory_serialized, this method holds cells in PHP memory as an array of serialized objects, but gzipped to reduce the memory usage still further, although access to read or write a cell is slightly slower.
? Similar to the serialization method, this method is compressed by gzip and then put into the memory after serialization. this method further reduces memory usage, but reading and writing will be slow.
?
========================================================== ==============================
?
PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;
PHPExcel1.7.6 official documentation says When using cache_to_discISAM all cells are held in a temporary disk file, with only an index to their location in that file maintained in PHP memory. this is slower than any of the cache_in_memory methods, but significantly ces CES the memory footprint.
The temporary disk file is automatically deleted when your script terminates.
? When cache_to_discISAM is used, all cells will be saved in a temporary disk file, and only their location in the file will be saved in the PHP memory, this is slower than any caching method in the memory, but can significantly reduce the memory usage. Temporary disk files are automatically deleted after the script is run.
?
========================================================== ==============================
?
PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
PHPExcel1.7.6 official documents write Like cache_to_discISAM, when using cache_to_phpTemp all cells are held in the php: // temp I/O stream, with only an index to their location maintained in PHP memory. in PHP, the php: // memory wrapper stores data in the memory: php: // temp behaves similarly, but uses a temporary file for storing the data when a certain memory limit is reached. the default is 1 MB, but you can change this when initialising cache_to_phpTemp.
? Similar to cache_to_discISAM, when cache_to_phpTemp is used, all cells will still exist in the php: // temp I/O stream and only store their locations in the PHP memory. The PHP: // memory package of php stores data in the memory. php: // temp performs similar operations, but when the size of the stored data exceeds the memory limit, the data will be stored in the temporary file. the default size is 1 MB, but you can modify it during initialization:
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;$cacheSettings = array( ' memoryCacheSize ' => '8MB' );PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
PHPExcel1.7.6 official documentation writes The php: // temp file is automatically deleted when your script terminates.
Php: // The temp file will be automatically deleted after the script ends.
?
?
========================================================== ==============================
?
PHPExcel_CachedObjectStorageFactory::cache_to_apc;
PHPExcle1.7.6 official documents write When using cache_to_apc, cell objects are maintained in APC with only an index maintained in PHP memory to identify that the cell exists. by default, an APC cache timeout of 600 seconds is used, which shocould be enough for most applications: although it is possible to change this when initialising cache_to_APC.
? When cach_to_apc is used, the cell is saved in APC and only the index is saved in memory. By default, the APC cache timeout time is 600 seconds, which is sufficient for most applications. you can also modify the value during initialization:
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_APC;$cacheSettings = array( 'cacheTime' => 600 );PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
PHPExcel1.7.6 official documentation writes When your script terminates all entries will be cleared from APC, regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanism.
? When the script stops running, all the data will be clear from the APC (ignore cache time). this mechanism cannot be used as a persistent cache.
?
?
========================================================== ==============================
PHPExcel_CachedObjectStorageFactory::cache_to_memcache
PHPExcel1.7.6 official documents write When using cache_to_memcache, cell objects are maintained in memcache with only an index maintained in PHP memory to identify that the cell exists.
By default, PHPExcel looks for a memcache server on localhost at port 11211. it also sets a memcache timeout limit of 600 seconds. if you are running memcache on a different server or port, then you can change these defaults when you initialise cache_to_memcache:
? When cache_to_memory is used, the cell object is saved in memcache and only the index is saved in memory. By default, PHPExcel searches for the memcache service on localhost and port 11211. the timeout time is 600 seconds. if you run the memcache service on another server or port, you can modify it during initialization:
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_memcache;$cacheSettings = array( 'memcacheServer' => 'localhost', 'memcachePort' => 11211, 'cacheTime' => 600 );PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
? In terms of initialization settings, MS does not support multiple memcache servers polling.
PHPExcel1.7.6 official documentation writes When your script terminates all entries will be cleared from memcache, regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanism.
? When the script ends, all data is cleared from memcache (the cache time is ignored). this mechanism cannot be used for persistent storage.
?
?
========================================================== ==============================
PHPExcel_CachedObjectStorageFactory::cache_to_wincache;
PHPExcel1.7.6 official documents write When using cache_to_wincache, cell objects are maintained in Wincache with only an index maintained in PHP memory to identify that the cell exists. by default, a Wincache cache timeout of 600 seconds is used, which shoshould be enough for most applications: although it is possible to change this when initialising cache_to_wincache.
? When cache_towincache is used, cell objects are saved in Wincache and only indexes are saved in memory. by default, the Wincache expiration time is 600 seconds, which is sufficient for most applications, of course, you can also modify it during initialization:
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_wincache;$cacheSettings = array( 'cacheTime' => 600 );PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
PHPExcel official documentation 1.7.6 writes When your script terminates all entries will be cleared from Wincache, regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanism.
?
?
Er, I finally finished writing "again". some of the previous versions of the text were directly pasted from the word, with a lot of formats that were truncated and miserable, it's not easy to translate documents ...... PHPExcel is still relatively powerful, and the biggest problem is memory usage. I used 1.7.2 before, but I still don't have this mechanism. I exported 2 W + data, and occupied 400 M + memory, change to 1.7.6 and use cach_to_diskISAM to reduce the memory to 200 M-. The effect is still obvious, but it is still high enough. The excel file is 5.1 M, it uses 200 M-and an unknown size of disk space. When can PHPExcel produce a lightweight version, there is no need for so many fancy features, just export the version of the most common data!
?
1 floor loserwn 1. modify the typo: when the script is running, all the data will be in the APC
Clear(Ignore cache time), you cannot use this mechanism as a persistent cache.
2. I chew on it. Excel is used as the self-owned file format of M $, and there are too many things hidden inside it. PHPExcel can achieve such compatibility is not easy. Especially without relying on. NET or various COM + libraries. The cost of 200 MB-memory is worthwhile. Silentime 2011-07-01 on the second floor. well, PHPExcel is very powerful ~~ 3 Floor yhg7752 2011-10-26 $ cacheMethod = PHPExcel_CachedObjectStorageFactory: cache_to_phpTemp;
$ CacheSettings = array ('memorycachesize' => '8mb'
);
PHPExcel_Settings: setCacheStorageMethod ($ cacheMethod, $ cachesetd );
I used the settings here, but I cannot export only 10000 pieces of data, I want to export a total of 160000 pieces of data from multiple excel files. each excel file contains 10000 pieces of data. Only one 4-floor yhg7752 2011-10-26 is exported. I don't know why. can you help me solve this problem? Thanks for the silentime 2011-10-30 yhg7752 on the fifth floor. I don't know why. can you help me solve this problem? Thank you.
Look at the memory usage. windows uses the task manager, and linux uses the top 6 floor ashmodeus, which is good. Basically, if you export a table with 5000 rows of memory, it will exceed MB.