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

Reclaim used memory #11

wants to merge 10 commits into from

Conversation

ghostwriter
Copy link
Contributor

@ghostwriter ghostwriter commented May 11, 2021

Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
@boesing
Copy link
Member

boesing commented May 11, 2021

Oh thats cool. Will have a look on this later on.
Just some questions here:

  • can you add items to the memory Adapter after flush? You dropped that and thus it is not tested. what was wrong with the unit test in Memory is not properly freed when removing data #7?

  • if the cache is empty and flush is used, it now returns false because nothing was freed, right? I'd add another sanity check to ensure it always returns true. I think we also need a test for that as it seems it is not yet tested?

  • Should we call gc_mem_caches before and after the data is "cleared"? Maybe it frees up memory from some where else and thus returns true even tho nothing was actually freed?

@boesing boesing added the Bug Something isn't working label May 11, 2021
@boesing boesing added this to the 1.1.1 milestone May 11, 2021
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
@ghostwriter
Copy link
Contributor Author

ghostwriter commented May 11, 2021

Yes. It was an oversight on my part.

  • if the cache is empty and flush is used, it now returns false because nothing was freed, right? I'd add another sanity check to ensure it always returns true. I think we also need a test for that as it seems it is not yet tested?

No. gc_mem_caches - Returns the number of bytes freed. (so, if no bytes were freed it returns 0.)

flush() will always return true, that behavior has not changed.

  • Should we call gc_mem_caches before and after the data is "cleared"? Maybe it frees up memory from some where else and thus returns true even tho nothing was actually freed?

No. It has 0 noticeable difference in performance calling it before the data is "cleared".

@boesing
Copy link
Member

boesing commented May 11, 2021

No. It has 0 noticeable difference in performance calling it before the data is "cleared".

So what would you think about this? Would that work? Did not test that yet.

public function flush()
{
    if ($this->data === []) {
        return true;
    }
    
    gc_mem_caches();
    $this->data = [];

    return gc_mem_caches() > 0;
}

@boesing
Copy link
Member

boesing commented May 11, 2021

And, what was wrong with the unit test from #7? The unit test was originally provided by #5 and thus, thats an actual real-world example I guess.

When I apply your changes to my local copy, the test still fails. Any ideas?

@boesing
Copy link
Member

boesing commented May 11, 2021

#7 fails because $i is undefined $this->storage->addItem('item' . $i, sha1((string) mt_rand()));

In theory, yes. But running the tests - thats wrong. And as the OutOfSpaceException is thrown after $i is being defined, there is exactly no scenario where $i is not set. PHPUnit would fail with a notice error when that would be the case.

The test results are pretty clear:

1) LaminasTest\Cache\Storage\Adapter\MemoryTest::testReclaimMemory
Laminas\Cache\Exception\OutOfSpaceException: Memory usage exceeds limit (10485960).

/Users/max/git/laminas/laminas-cache-storage-adapter-memory/src/Memory.php:514
/Users/max/git/laminas/laminas-cache-storage-adapter-memory/vendor/laminas/laminas-cache/src/Storage/Adapter/AbstractAdapter.php:775
/Users/max/git/laminas/laminas-cache-storage-adapter-memory/test/unit/MemoryTest.php:64

@InvisibleSmiley
Copy link

Calling gc_mem_caches() also has no effect for me in my real-world testcase (which I cannot share unfortunately).

I'm still convinced that the actual issue lies somewhere in the laminas-cache base implementation (\Laminas\Cache\Storage\Adapter\AbstractAdapter::setItem), probably related to references getting attached to arrays inside objects in such a way that PHP's GC cannot resolve what is actual garbage that can be collected.

@boesing
Copy link
Member

boesing commented May 11, 2021

@InvisibleSmiley I removed any referencing from the code and ran the test. Same problem. Even tho I did not checked any LoC, I think its not related to circular referencing.

Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Copy link
Member

@boesing boesing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ghostwriter can you please use the test case from #7 - if needed, put an $i = 0 outside the try {} catch () {} to avoid "undefined variable" issues.

I still end up having my unit tests fail when applying the code changes in here so either there is something different with the test cases in this PR or anything else is going on.

src/Memory.php Show resolved Hide resolved
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
@ghostwriter ghostwriter requested a review from boesing May 11, 2021 12:01
test/unit/MemoryTest.php Outdated Show resolved Hide resolved
test/unit/MemoryTest.php Outdated Show resolved Hide resolved
test/unit/MemoryTest.php Outdated Show resolved Hide resolved
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
@ghostwriter ghostwriter requested review from boesing and Xerkus May 11, 2021 16:48
@ghostwriter
Copy link
Contributor Author

Calling gc_mem_caches() also has no effect for me in my real-world testcase (which I cannot share unfortunately).

I'm still convinced that the actual issue lies somewhere in the laminas-cache base implementation (\Laminas\Cache\Storage\Adapter\AbstractAdapter::setItem), probably related to references getting attached to arrays inside objects in such a way that PHP's GC cannot resolve what is actual garbage that can be collected.

It could be laminas/laminas-cache#14 and or https://bugs.php.net/70098

@boesing
Copy link
Member

boesing commented Sep 13, 2021

Closing this for now as the approach of this PR might not result to what the initial author was expecting.
If there is still a need for this patch, please let me know.

@boesing boesing closed this Sep 13, 2021
@boesing boesing removed this from the 1.1.1 milestone Sep 13, 2021
@ghostwriter ghostwriter deleted the bugfix/reclaim-memory branch September 16, 2021 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Psr\SimpleCache\CacheInterface::clear() has no effect on hasAvailableSpace()
4 participants