Reduce the memory consumption by forcing PHP to release the unused memory #6230
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
At our company we run our codeception tests in parallel using multiple processes.
When 50 Codeception processes run in parallel the memory consumption of a single Codeception process starts to matter. We noticed that some of our Cests consume 40MB of RAM while others consume 80MB (run on Ubuntu 20.04, PHP 7.4.3 and Centos 7, PHP 7.4.14).
I profiled our tests to find how can I reduce their memory footprint. I found that the source of the problem lies in a single call to token_get_all() in the \Codeception\Lib\Parser::getClassesFromFile() function.
It seems this is a well known issue:
https://bugs.php.net/bug.php?id=71375
On large files the result of token_get_all() can consume dozens of megabytes of RAM, but even after result got out of scope PHP doesn't return the consumed memory to the OS.
I added lines:
to the end of the \Codeception\Lib\Parser::getClassesFromFile() function to force PHP to return the cached memory. It reduced the memory consumption of our parallel run by 1GB.
Though \Codeception\SuiteManager::loadTests() looks like a better place for the gc_mem_caches() call:
for some unknown reason a call to gc_mem_caches() in \Codeception\SuiteManager::loadTests() releases less memory for some Cests than the same call at the end of \Codeception\Lib\Parser::getClassesFromFile() (700MB vs 1GB in the case of our parallel run).