Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /_sources/modules/zend.memory.overview.txt

Size:3414
Storage flags:no_autoload,compress/gzip (39%)

.. _zend.memory.overview:

Overview
========

.. _zend.memory.introduction:

Introduction
------------

The ``Zend\Memory`` component is intended to manage data in an environment with limited memory.

Memory objects (memory containers) are generated by memory manager by request and transparently swapped/loaded when
it's necessary.

For example, if creating or loading a managed object would cause the total memory usage to exceed the limit you
specify, some managed objects are copied to cache storage outside of memory. In this way, the total memory used by
managed objects does not exceed the limit you need to enforce.

The memory manager uses :ref:`Zend\Cache backends <zend.cache.backends>` as storage providers.

.. _zend.memory.introduction.example-1:

.. rubric:: Using Zend\Memory component

``Zend\Memory\Memory::factory()`` instantiates the memory manager class with specified backend options.

.. code-block:: php
   :linenos:

   $backendOptions = array(
       'cache_dir' => './tmp/' // Directory where to put the swapped memory blocks
   );

   $memoryManager = Zend\Memory\Memory::factory('File', $backendOptions);

   $loadedFiles = array();

   for ($count = 0; $count < 10000; $count++) {
       $f = fopen($fileNames[$count], 'rb');
       $data = fread($f, filesize($fileNames[$count]));
       $fclose($f);

       $loadedFiles[] = $memoryManager->create($data);
   }

   echo $loadedFiles[$index1]->value;

   $loadedFiles[$index2]->value = $newValue;

   $loadedFiles[$index3]->value[$charIndex] = '_';

.. _zend.memory.theory-of-operation:

Theory of Operation
-------------------

``Zend\Memory`` component operates with the following concepts:



   - Memory manager

   - Memory container

   - Locked memory object

   - Movable memory object



.. _zend.memory.theory-of-operation.manager:

Memory manager
^^^^^^^^^^^^^^

The memory manager generates memory objects (locked or movable) by request of user application and returns them
wrapped into a memory container object.

.. _zend.memory.theory-of-operation.container:

Memory container
^^^^^^^^^^^^^^^^

The memory container has a virtual or actual ``value`` attribute of string type. This attribute contains the data
value specified at memory object creation time.

You can operate with this ``value`` attribute as an object property:

.. code-block:: php
   :linenos:

   $memObject = $memoryManager->create($data);

   echo $memObject->value;

   $memObject->value = $newValue;

   $memObject->value[$index] = '_';

   echo ord($memObject->value[$index1]);

   $memObject->value = substr($memObject->value, $start, $length);

.. note::

   If you are using a *PHP* version earlier than 5.2, use the :ref:`getRef()
   <zend.memory.memory-objects.api.getRef>` method instead of accessing the value property directly.

.. _zend.memory.theory-of-operation.locked:

Locked memory
^^^^^^^^^^^^^

Locked memory objects are always stored in memory. Data stored in locked memory are never swapped to the cache
backend.

.. _zend.memory.theory-of-operation.movable:

Movable memory
^^^^^^^^^^^^^^

Movable memory objects are transparently swapped and loaded to/from the cache backend by ``Zend\Memory`` when it's
necessary.

The memory manager doesn't swap objects with size less than the specified minimum, due to performance
considerations. See :ref:`this section <zend.memory.memory-manager.settings.min-size>` for more details.



For more information about the PHK package format: http://phk.tekwire.net