fix php 5.6 in docker dev env (#1740)
[openemr.git] / vendor / zendframework / zend-memory / doc / book / memory-manager.md
blobbe39b8fbd7760287671fd84784e6fb801e7c2570
1 # Memory Manager
3 ## Creating a Memory Manager
5 You can create new a memory manager (`Zend\Memory\MemoryManager` object) using its constructor:
7 ```php
8 __construct(Zend\Cache\Storage\StorageInterface $cache = null) : void
9 ```
11 As an example, the following creates an instance which *is not* backed by cache
12 storage:
14 ```php
15 $memoryManager = new Zend\Memory\MemoryManager();
16 ```
18 While the following creates an instance backed by a filesystem cache storage
19 adapter, storing memory blocks in the `tmp/` directory of the current working
20 directory:
22 ```php
23 use Zend\Cache\StorageFactory;
24 use Zend\Memory\MemoryManager;
26 $cache = StorageFactory::factory([
27     'adapter' => [
28         'name' => 'Filesystem',
29         'options' => [
30             'cache_dir' => './tmp/', // Directory in which to put swapped memory blocks
31         ],
32     ],
33 ]);
35 $memoryManager = new MemoryManager($cache);
36 ```
38 The `MemoryManager` uses [zend-cache storage adapters](http://zendframework.github.io/zend-cache/storage/adapter/)
39 to cache memory blocks; if no cache instance is provided, the system temporary
40 directory is used. This is useful if you know that memory is not limited or the
41 overall size of objects never reaches the memory limit.
43 ## Managing Memory Objects
45 This section describes creating and destroying objects in the managed memory,
46 and settings to control memory manager behavior.
48 ### Creating Movable Objects
50 Create movable objects (objects which may be swapped into cache storage) using
51 the `Zend\Memory\MemoryManager::create([$data])` method:
53 ```php
54 $memObject = $memoryManager->create($data);
55 ```
57 The `$data` argument is optional and used to initialize the object value. If the
58 `$data` argument is omitted, the value is an empty string.
60 ### Creating Locked Objects
62 Create locked objects (objects which will never be swapped into cache storage)
63 using the `Zend\Memory\MemoryManager::createLocked([$data])` method:
65 ```php
66 $memObject = $memoryManager->createLocked($data);
67 ```
69 The `$data` argument is optional and used to initialize the object value. If the
70 `$data` argument is omitted, the value is an empty string.
72 ### Destroying Objects
74 Memory objects are automatically destroyed and removed from memory when they go
75 out of scope:
77 ```php
78 function foo() use ($memoryManager, $memList) {
79     // ...
81     $memObject1 = $memoryManager->create($data1);
82     $memObject2 = $memoryManager->create($data2);
83     $memObject3 = $memoryManager->create($data3);
85     // ...
87     $memList[] = $memObject3;
89     // ...
91     unset($memObject2); // $memObject2 is destroyed here
93     // ...
94     // $memObject1 is destroyed here
95     // but $memObject3 object is still referenced by $memList
96     // and is not destroyed
98 ```
100 This applies to both movable and locked objects.
102 ## Settings
104 ### Memory Limit
106 The memory limit is the maximum number of bytes allowed for use by loaded
107 movable objects.
109 If loading or creation of an object causes memory usage to exceed of this limit,
110 then the memory manager swaps some other objects.
112 You can retrieve or set the memory limit setting using the `getMemoryLimit()` and
113 `setMemoryLimit($newLimit)` methods:
115 ```php
116 $oldLimit = $memoryManager->getMemoryLimit();  // Get memory limit in bytes
117 $memoryManager->setMemoryLimit($newLimit);     // Set memory limit in bytes
120 A negative value for memory limit means 'no limit'.
122 The default value is two-thirds of the value of `memory_limit` in `php.ini` or
123 'no limit' (-1) if `memory_limit` is not set in `php.ini`.
125 ### MinSize
127 The `MinSize` is the minimum size an object must be before it will be swapped to a
128 cache backend; objects with sizes smaller than this value will not be swapped.
129 This reduces the number of swap/load operations.
131 You can retrieve or set the minimum size using the `getMinSize()` and
132 `setMinSize($newSize)` methods:
134 ```php
135 $oldMinSize = $memoryManager->getMinSize();  // Get MinSize in bytes
136 $memoryManager->setMinSize($newSize);        // Set MinSize limit in bytes
139 The default minimum size value is 16KB (16384 bytes).