<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.memory.memory-manager">
    <title>Memory Manager</title>

    <sect2 id="zend.memory.memory-manager.creation">

        <title>Creating a Memory Manager</title>

        <para>
            You can create new a memory manager
            (<classname>Zend_Memory_Manager</classname> object) using the
            <classname>Zend_Memory::factory($backendName [, $backendOprions])</classname>
            method.
        </para>

        <para>
            The first argument <code>$backendName</code> is a string that
            names one of the backend implementations supported by <classname>Zend_Cache</classname>.
        </para>

        <para>
            The second argument <code>$backendOptions</code> is an optional
            backend options array.
        </para>

        <programlisting role="php"><![CDATA[
$backendOptions = array(
    'cache_dir' => './tmp/' // Directory where to put the swapped memory blocks
);

$memoryManager = Zend_Memory::factory('File', $backendOptions);
]]></programlisting>

        <para>
            <classname>Zend_Memory</classname> uses <link linkend="zend.cache.backends"><classname>Zend_Cache backends</classname></link>
            as storage providers.
        </para>

        <para>
            You may use the special name '<code>None</code>' as a backend name,
            in addition to standard <classname>Zend_Cache</classname> backends.

            <programlisting role="php"><![CDATA[
$memoryManager = Zend_Memory::factory('None');
]]></programlisting>
        </para>

        <para>
            If you use '<code>None</code>' as the backend name, then the memory
            manager never swaps memory blocks. This is useful if you know that
            memory is not limited or the overall size of objects never reaches
            the memory limit.
        </para>

        <para>
            The '<code>None</code>' backend doesn't need any option specified.
        </para>

    </sect2>

    <sect2 id="zend.memory.memory-manager.objects-management">

        <title>Managing Memory Objects</title>

        <para>
            This section describes creating and destroying objects in the
            managed memory, and settings to control memory manager behavior.
        </para>

        <sect3 id="zend.memory.memory-manager.objects-management.movable">

            <title>Creating Movable Objects</title>

            <para>
                Create movable objects (objects, which may be swapped) using
                the <classname>Zend_Memory_Manager::create([$data])</classname> method:
                <programlisting role="php"><![CDATA[
$memObject = $memoryManager->create($data);
]]></programlisting>

            </para>

            <para>
                The <code>$data</code> argument is optional and used to
                initialize the object value. If the <code>$data</code>
                argument is omitted, the value is an empty string.
            </para>

        </sect3>

        <sect3 id="zend.memory.memory-manager.objects-management.locked">

            <title>Creating Locked Objects</title>

            <para>
                Create locked objects (objects, which are not swapped) using
                the <classname>Zend_Memory_Manager::createLocked([$data])</classname> method:
                <programlisting role="php"><![CDATA[
$memObject = $memoryManager->createLocked($data);
]]></programlisting>
            </para>

            <para>
                The <code>$data</code> argument is optional and used to
                initialize the object value. If the <code>$data</code>
                argument is omitted, the value is an empty string.
            </para>

        </sect3>

        <sect3 id="zend.memory.memory-manager.objects-management.destruction">

            <title>Destroying Objects</title>

            <para>
                Memory objects are automatically destroyed and removed from
                memory when they go out of scope:

                <programlisting role="php"><![CDATA[
function foo()
{
    global $memoryManager, $memList;

    ...

    $memObject1 = $memoryManager->create($data1);
    $memObject2 = $memoryManager->create($data2);
    $memObject3 = $memoryManager->create($data3);

    ...

    $memList[] = $memObject3;

    ...

    unset($memObject2); // $memObject2 is destroyed here

    ...
    // $memObject1 is destroyed here
    // but $memObject3 object is still referenced by $memList
    // and is not destroyed
}
]]></programlisting>
            </para>

            <para>
                This applies to both movable and locked objects.
            </para>

        </sect3>

    </sect2>

    <sect2 id="zend.memory.memory-manager.settings">

        <title>Memory Manager Settings</title>

        <sect3 id="zend.memory.memory-manager.settings.memory-limit">

            <title>Memory Limit</title>

            <para>
                Memory limit is a number of bytes allowed to be used by loaded
                movable objects.
            </para>

            <para>
                If loading or creation of an object causes memory usage to
                exceed of this limit, then the memory manager swaps some other
                objects.
            </para>

            <para>
                You can retrieve or set the memory limit setting using the
                <code>getMemoryLimit()</code> and <code>setMemoryLimit($newLimit)</code>
                methods:
                <programlisting role="php"><![CDATA[
$oldLimit = $memoryManager->getMemoryLimit();  // Get memory limit in bytes
$memoryManager->setMemoryLimit($newLimit);     // Set memory limit in bytes
]]></programlisting>
            </para>

            <para>
                A negative value for memory limit means 'no limit'.
            </para>

            <para>
                The default value is two-thirds of the value of
                '<code>memory_limit</code>' in php.ini or 'no limit' (-1)
                if '<code>memory_limit</code>' is not set in php.ini.
            </para>

        </sect3>

        <sect3 id="zend.memory.memory-manager.settings.min-size">

            <title>MinSize</title>

            <para>
                MinSize is a minimal size of memory objects, which may be
                swapped by memory manager. The memory manager does not swap
                objects that are smaller than this value. This reduces the
                number of swap/load operations.
            </para>

            <para>
                You can retrieve or set the minimum size using the
                <code>getMinSize()</code> and <code>setMinSize($newSize)</code>
                methods:
                <programlisting role="php"><![CDATA[
$oldMinSize = $memoryManager->getMinSize();  // Get MinSize in bytes
$memoryManager->setMinSize($newSize);        // Set MinSize limit in bytes
]]></programlisting>
            </para>

            <para>
                The default minimum size value is 16KB (16384 bytes).
            </para>

        </sect3>

    </sect2>

</sect1>
