Overview

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 ZendCache backends as storage providers.

Using ZendMemory component

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
$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] = '_';

Theory of Operation

Zend\Memory component operates with the following concepts:

  • Memory manager
  • Memory container
  • Locked memory object
  • Movable memory object

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.

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$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 getRef() method instead of accessing the value property directly.

Locked memory

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

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 this section for more details.

Table Of Contents

Previous topic

Introduction to Zend\Math

Next topic

Memory Manager

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Overview on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.