<?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
            <methodname>Zend_Memory::factory($backendName [, $backendOprions])</methodname>
            method.
        </para>

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

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

        <programlisting language="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 'None' as a backend name,
            in addition to standard <classname>Zend_Cache</classname> backends.
        </para>

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

        <para>
            If you use 'None' 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 'None' 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 <methodname>Zend_Memory_Manager::create([$data])</methodname> method:
            </para>

            <programlisting language="php"><![CDATA[
$memObject = $memoryManager->create($data);
]]></programlisting>

            <para>
                The <varname>$data</varname> argument is optional and used to
                initialize the object value. If the <varname>$data</varname>
                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 <methodname>Zend_Memory_Manager::createLocked([$data])</methodname> method:
            </para>

            <programlisting language="php"><![CDATA[
$memObject = $memoryManager->createLocked($data);
]]></programlisting>

            <para>
                The <varname>$data</varname> argument is optional and used to
                initialize the object value. If the <varname>$data</varname>
                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:
            </para>

            <programlisting language="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>
                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
                <methodname>getMemoryLimit()</methodname> and
                <methodname>setMemoryLimit($newLimit)</methodname> methods:
            </para>

            <programlisting language="php"><![CDATA[
$oldLimit = $memoryManager->getMemoryLimit();  // Get memory limit in bytes
$memoryManager->setMemoryLimit($newLimit);     // Set memory limit in bytes
]]></programlisting>

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

            <para>
                The default value is two-thirds of the value of
                'memory_limit' in php.ini or 'no limit' (-1)
                if 'memory_limit' 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
                <methodname>getMinSize()</methodname> and
                <methodname>setMinSize($newSize)</methodname> methods:
            </para>

            <programlisting language="php"><![CDATA[
$oldMinSize = $memoryManager->getMinSize();  // Get MinSize in bytes
$memoryManager->setMinSize($newSize);        // Set MinSize limit in bytes
]]></programlisting>

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