Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reclaim used memory #11

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
use function array_diff;
use function array_keys;
use function count;
use function gc_collect_cycles;
use function gc_enabled;
use function gc_mem_caches;
use function memory_get_usage;
use function microtime;
use function strpos;
Expand Down Expand Up @@ -111,9 +114,11 @@ public function getTotalSpace()
*/
public function getAvailableSpace()
{
$total = $this->getOptions()->getMemoryLimit();
$avail = $total - (float) memory_get_usage(true);
return $avail > 0 ? $avail : 0;
$memoryLimit = $this->getOptions()->getMemoryLimit();
$usedMemory = memory_get_usage(false);
$freeMemory = $memoryLimit - $usedMemory;

return $freeMemory > 0 ? $freeMemory : 0;
}

/* IterableInterface */
Expand Down Expand Up @@ -148,8 +153,19 @@ public function getIterator()
*/
public function flush()
{
if ($this->data === []) {
return true;
}

unset($this->data);

if (gc_enabled()) {
gc_collect_cycles();
}

$this->data = [];
return true;

return gc_mem_caches() >= 0;
ghostwriter marked this conversation as resolved.
Show resolved Hide resolved
}

/* ClearExpiredInterface */
Expand Down Expand Up @@ -751,7 +767,6 @@ protected function hasAvailableSpace()
return true;
}

$free = $total - (float) memory_get_usage(true);
return $free > 0;
return ($total - memory_get_usage(false)) > 0;
}
}
95 changes: 94 additions & 1 deletion test/unit/MemoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Laminas\Cache\Exception\OutOfSpaceException;

use function memory_get_usage;
use function mt_rand;
use function sha1;

/**
* @group Laminas_Cache
Expand Down Expand Up @@ -39,9 +41,100 @@ public function getCommonAdapterNamesProvider(): array

public function testThrowOutOfSpaceException()
{
$this->options->setMemoryLimit(memory_get_usage(true) - 8);
$this->options->setMemoryLimit($this->getUsedMemory() - 1);

$this->expectException(OutOfSpaceException::class);
$this->storage->addItem('test', 'test');
}

public function testReclaimMemory()
{
$outOfSpaceExceptionThrown = false;
try {
$startMemoryAllocatedToPhp = $this->getAllocatedMemory();

for ($i = 0; $i < 100000; ++$i) {
$this->storage->addItem('item' . $i, sha1((string) mt_rand()));
}

$finishMemoryAllocatedToPhp = $this->getUsedMemory();

$this->assertGreaterThan($startMemoryAllocatedToPhp, $finishMemoryAllocatedToPhp);

$this->storage->flush();

$flushedMemoryAllocatedToPhp = $this->getUsedMemory();

self::assertLessThan($finishMemoryAllocatedToPhp, $flushedMemoryAllocatedToPhp);
} catch (OutOfSpaceException $ignore) {
$outOfSpaceExceptionThrown = true;
}

self::assertFalse($outOfSpaceExceptionThrown, 'OutOfSpaceException was thrown');
}

/**
* See: https://github.com/laminas/laminas-cache-storage-adapter-memory/issues/5
*/
public function testReclaimMemoryAfterOutOfSpaceExceptionThrownAndMemoryGetsFlushed()
{
$this->options->setMemoryLimit($this->getAllocatedMemory() + 200);

try {
for ($i = 0; $i <= 100000; $i++) {
$this->storage->addItem('item' . $i, sha1((string) mt_rand()));
}

self::fail('filling the cache with test data to reach the memory limit failed');
} catch (OutOfSpaceException $ignore) {
}

$this->storage->flush();
$this->storage->addItem('item' . $i, sha1((string) mt_rand()));
}

public function testReclaimMemoryAfterOutOfSpaceExceptionThrown()
{
$startMemoryAllocatedToPhp = $this->getAllocatedMemory();
$this->options->setMemoryLimit($startMemoryAllocatedToPhp);
$outOfSpaceExceptionThrown = false;
try {
for ($i = 0; $i < 100000; ++$i) {
$this->storage->addItem('item' . $i, sha1((string) mt_rand()));
}
} catch (OutOfSpaceException $ignore) {
$outOfSpaceExceptionThrown = true;
}

self::assertTrue($outOfSpaceExceptionThrown, 'OutOfSpaceException was not thrown');

$finishMemoryAllocatedToPhp = $this->getUsedMemory();

$this->assertGreaterThan($startMemoryAllocatedToPhp, $finishMemoryAllocatedToPhp);

$this->storage->flush();

$flushedMemoryAllocatedToPhp = $this->getUsedMemory();

self::assertLessThan($finishMemoryAllocatedToPhp, $flushedMemoryAllocatedToPhp);

$this->storage->addItem('item1', sha1((string) mt_rand()));

$this->storage->addItem('item2', $this->storage->getItem('item1'));

self::assertSame(
$this->storage->getItem('item1'),
$this->storage->getItem('item2')
);
}

private function getUsedMemory(): int
{
return memory_get_usage(false);
}

private function getAllocatedMemory(): int
{
return memory_get_usage(true);
}
}