Phpexcel Resolve memory consumption problems-DW find memorycachesize change 1M to 2048M

Source: Internet
Author: User
Tags apc configuration settings terminates

Http://blog.sina.com.cn/s/blog_4ec7952d0101fcrd.htmlPHPExcel resolving excessive memory consumption-set the Cell object cache

Phpexcel is a powerful PHP open source class that handles Excel, but a big problem is that it takes up too much memory, Starting with 1.7.3, it supports setting up the cell cache, but it is recommended to use the current stable version 1.7.6, because the previous version will have a different degree of bug, the following is its official documentation:

Phpexcel averages the memory using 1k/cells, so large documents can cause memory consumption to be very fast. The cell cache mechanism allows Phpexcel to cache small cell objects in memory on disk or Apc,memcache or wincache, although it consumes some time to read the data, but can help you reduce memory consumption.

If it's not enough, open the php.ini and change the memory_limit inside.

The default is 128M to increase the point.

By default, Phpexcel still saves the cell object in memory, but you can customize it. You can use the Phpexcel_settings::setcachestoragemethod () method to pass caching as a parameter to this method to set the caching mode.

PHP code

$cacheMethod = phpexcel_cachedobjectstoragefactory::cache_in_memory;

Phpexcel_settings::setcachestoragemethod ($cacheMethod);

PHPExcel1.7.6 Official document wrote Setcachestoragemethod () would return a Boolean true on success, false on failure (for example if trying To the APC when APC was not enabled).

The Setcachestoragemethod () method returns a bool variable to indicate whether the setting was successful (for example, if APC is not available, you set the APC cache to return false)

PHPExcel1.7.6 Official document wrote a separate cache is maintained for each individual worksheet, and was automatically created when the Wo Rksheet is instantiated based on the caching method and settings so you have configured. You cannot change the configuration settings once and started to read a workbook, or created your first Workshee T.

Each worksheet will have a separate cache, and when an worksheet is instantiated, it is automatically created based on how the settings or configurations are cached. Once you start reading a file or you have created the first worksheet, you cannot change the way the cache is changed.

PHPExcel1.7.6 Official document writes currently, the following caching methods is available.

Currently, there are several ways to use the following caching methods:

PHP code

Phpexcel_cachedobjectstoragefactory::cache_in_memory;

PHPExcel1.7.6 Official document writes the default. If you don ' t initialise any caching method, then this is the method of that Phpexcel would use. Cell objects is maintained in PHP memory as at present.

By default, if you do not initialize any of the caching methods, Phpexcel will use the way the memory is cached.

===============================================

PHP code

phpexcel_cachedobjectstoragefactory::cache_in_memory_serialized;

PHPExcle1.7.6 Official document wrote using this caching method, cells is held in PHP memory as an array of serialized objects, which reduc Es the memory footprint with minimal performance overhead.

Using this caching method, cells are stored in memory in a serialized manner, which is a scenario that reduces memory utilization performance.

===============================================

PHP code

Phpexcel_cachedobjectstoragefactory::cache_in_memory_gzip;

PHPExcel1.7.6 Official document wrote like Cache_in_memory_serialized, this method holds cells in PHP memory as an array of serialized OBJEC TS, but gzipped to reduce the memory usage still further, although access to read or write a cell is slightly slower.

Similar to serialization, this method, after serialization, is then gzip compressed and then put into memory, which further reduces the use of memory, but there are some slow reads and writes.

===========================================================

PHP code

Phpexcel_cachedobjectstoragefactory::cache_to_discisam;

PHPExcel1.7.6 Official document wrote when using Cache_to_discisam all cells is held in a temporary disk file with a index to their Location in this file maintained in PHP memory. This was slower than any of the cache_in_memory methods, but significantly reduces the memory footprint.
The temporary disk file is automatically deleted when your script terminates.

When using Cache_to_discisam this way, all cells will be saved in a temporary disk file, only their location in the file in PHP memory, which is slower than any kind of cache in memory, but can significantly reduce the use of memory. Temporary disk files are automatically deleted at the end of the script run.

===========================================================

PHP code

Phpexcel_cachedobjectstoragefactory::cache_to_phptemp;

PHPExcel1.7.6 Official document writes like Cache_to_discisam, when using cache_to_phptemp all cells is held in the php://temp I/O stream, W ITH only a 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 St Oring the data when a certain memory limit is reached. The default is 1 MB, and you can change this when initialising cache_to_phptemp.

Similar to Cache_to_discisam, when using cache_to_phptemp, all cells are still present in the php://temp I/O stream, and only their location is stored in PHP memory. PHP's Php://memory wrapper stores the data in memory, Php://temp behaves similarly, but when the stored data size exceeds the memory limit, the data is saved in a temporary file, the default size is 1MB, but you can modify it at initialization time:

PHP code

$cacheMethod = phpexcel_cachedobjectstoragefactory:: cache_to_phptemp;

$cacheSettings = Array (' memorycachesize ' = ' 8MB ');

Phpexcel_settings::setcachestoragemethod ($cacheMethod, $cacheSettings);

PHPExcel1.7.6 Official document writes the Php://temp file is automatically deleted when your script terminates.

The Php://temp file is automatically deleted at the end of the script.

===========================================================

PHP code

PHPEXCEL_CACHEDOBJECTSTORAGEFACTORY::CACHE_TO_APC;

PHPExcle1.7.6 Official document wrote when using CACHE_TO_APC, cell objects is maintained in APC with only a index maintained in PHP Memor Y to identify the cell exists. By default, a APC cache timeout of seconds is used, which should was enough for most applications:although it is poss Ible to Initialising CACHE_TO_APC.

When CACH_TO_APC is used, the cell is saved in APC, and the index is saved only in memory. APC caches the default timeout time of 600 seconds, which is sufficient for most applications, and of course you can modify it at initialization time:

PHP code

$cacheMethod = PHPEXCEL_CACHEDOBJECTSTORAGEFACTORY::CACHE_TO_APC;

$cacheSettings = Array (' cacheTime ' = 600);

Phpexcel_settings::setcachestoragemethod ($cacheMethod, $cacheSettings);

PHPExcel1.7.6 Official document wrote when your script terminates all entries would be cleared from APC, regardless of the cacheTime value, s O It cannot is used for persistent storage using this mechanism.

When the script finishes running, all the data is clear from the APC (ignoring the cache time) and cannot be used as a persistent cache.

===========================================================

PHP code

Phpexcel_cachedobjectstoragefactory::cache_to_memcache

PHPExcel1.7.6 Official document wrote when using Cache_to_memcache, cell objects is maintained in Memcache with only a index maintained in PHP memory to identify the cell exists.
By default, the Phpexcel looks for a memcache server on localhost at Port 11211. It also sets a memcache timeout limit of seconds. If you is running memcache on a different server or port and 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 the index is saved in memory only. By default, Phpexcel will look for the memcache service on localhost and Port 11211, time out for 600 seconds, and if you run the Memcache service on a different server or other port, you can modify it at initialization:

PHP code

$cacheMethod = Phpexcel_cachedobjectstoragefactory::cache_to_memcache;

$cacheSettings = Array (' memcacheserver ' = ' localhost ',

' Memcacheport ' = 11211,

' CacheTime ' = 600

);

Phpexcel_settings::setcachestoragemethod ($cacheMethod, $cacheSettings);

From the initial setting of the formal view, MS also does not support multiple Memcache server polling method, it is regrettable.

PHPExcel1.7.6 Official document wrote when your script terminates all entries would be cleared from memcache, regardless of the cacheTime Val UE, so it cannot is used for persistent storage using this mechanism.

When the script ends, all data is emptied from memcache (ignoring cache time) and cannot be persisted using this mechanism.

===========================================================

PHP code

Phpexcel_cachedobjectstoragefactory::cache_to_wincache;

PHPExcel1.7.6 Official document wrote when using Cache_to_wincache, cell objects is maintained in Wincache with only a index maintained in PHP memory to identify the cell exists. By default, a Wincache cache timeout of seconds was used, which should be enough for most applications:although it is possible to initialising Cache_to_wincache.

Using the Cache_towincache method, the Cell object is saved in Wincache, the index is saved in memory only, and by default the Wincache expires at 600 seconds, which is sufficient for most applications and can of course be modified at initialization time:

PHP code

$cacheMethod = Phpexcel_cachedobjectstoragefactory::cache_to_wincache;

$cacheSettings = Array (' cacheTime ' = 600);

Phpexcel_settings::setcachestoragemethod ($cacheMethod, $cacheSettings);

Phpexcel Official document 1.7.6 wrote when your script terminates all entries would be cleared from Wincache, regardless of the cacheTime Val UE, so it cannot is used for persistent storage using this mechanism.

Phpexcel is still relatively powerful, the biggest problem is the memory consumption problem, phpexcel when can be a lightweight version, do not need so many fancy features, only need to export the most common version of the data!

Phpexcel Resolve memory consumption problems-DW find memorycachesize change 1M to 2048M

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.