1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.memory.overview">
4 <title>Overview</title>
6 <sect2 id="zend.memory.introduction">
7 <title>Introduction</title>
10 The <classname>Zend_Memory</classname> component is intended to manage data in an
11 environment with limited memory.
15 Memory objects (memory containers) are generated by memory manager
16 by request and transparently swapped/loaded when it's necessary.
20 For example, if creating or loading a managed object would cause
21 the total memory usage to exceed the limit you specify, some
22 managed objects are copied to cache storage outside of memory.
23 In this way, the total memory used by managed objects does not
24 exceed the limit you need to enforce.
28 The memory manager uses
29 <link linkend="zend.cache.backends">Zend_Cache backends</link>
33 <example id="zend.memory.introduction.example-1">
34 <title>Using Zend_Memory component</title>
37 <methodname>Zend_Memory::factory()</methodname> instantiates the memory
38 manager object with specified backend options.
41 <programlisting language="php"><![CDATA[
42 $backendOptions = array(
43 'cache_dir' => './tmp/' // Directory where to put the swapped memory blocks
46 $memoryManager = Zend_Memory::factory('File', $backendOptions);
48 $loadedFiles = array();
50 for ($count = 0; $count < 10000; $count++) {
51 $f = fopen($fileNames[$count], 'rb');
52 $data = fread($f, filesize($fileNames[$count]));
55 $loadedFiles[] = $memoryManager->create($data);
58 echo $loadedFiles[$index1]->value;
60 $loadedFiles[$index2]->value = $newValue;
62 $loadedFiles[$index3]->value[$charIndex] = '_';
67 <sect2 id="zend.memory.theory-of-operation">
68 <title>Theory of Operation</title>
71 <classname>Zend_Memory</classname> component operates with the following concepts:
75 <para>Memory manager</para>
79 <para>Memory container</para>
83 <para>Locked memory object</para>
87 <para>Movable memory object</para>
92 <sect3 id="zend.memory.theory-of-operation.manager">
93 <title>Memory manager</title>
96 The memory manager generates memory objects (locked or movable)
97 by request of user application and returns them wrapped into
98 a memory container object.
102 <sect3 id="zend.memory.theory-of-operation.container">
103 <title>Memory container</title>
106 The memory container has a virtual or actual <code>value</code>
107 attribute of string type. This attribute contains the data value
108 specified at memory object creation time.
112 You can operate with this <code>value</code> attribute as
115 <programlisting language="php"><![CDATA[
116 $memObject = $memoryManager->create($data);
118 echo $memObject->value;
120 $memObject->value = $newValue;
122 $memObject->value[$index] = '_';
124 echo ord($memObject->value[$index1]);
126 $memObject->value = substr($memObject->value, $start, $length);
132 If you are using a <acronym>PHP</acronym> version earlier than 5.2, use the
133 <link linkend="zend.memory.memory-objects.api.getRef">getRef()</link>
134 method instead of accessing the value property directly.
139 <sect3 id="zend.memory.theory-of-operation.locked">
140 <title>Locked memory</title>
143 Locked memory objects are always stored in memory.
144 Data stored in locked memory are never swapped to the cache
149 <sect3 id="zend.memory.theory-of-operation.movable">
150 <title>Movable memory</title>
153 Movable memory objects are transparently swapped and loaded
154 to/from the cache backend by <classname>Zend_Memory</classname> when it's necessary.
158 The memory manager doesn't swap objects with size less than
159 the specified minimum, due to performance considerations.
160 See <xref linkend="zend.memory.memory-manager.settings.min-size" />