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

Question regarding reservedMemory var #4

Closed
JayBizzle opened this issue Mar 14, 2018 · 2 comments
Closed

Question regarding reservedMemory var #4

JayBizzle opened this issue Mar 14, 2018 · 2 comments

Comments

@JayBizzle
Copy link

Hi,

Thanks for this great package. I was just looking through the code and wondered if you could explain what the following line is for.

$this->reservedMemory = str_repeat(' ', 20 * 1024);

Thanks

@dmitry-ivanov
Copy link
Owner

Hi, @JayBizzle !

Thanks for your kind words! :)

I'm reserving a bit of memory (while registering shutdown function), to have an ability to execute all required operations while handling "Fatal Error: Allowed Memory Size of ... Bytes Exhausted".

If you take a look on handling method:

public function onShutdown()
{
$this->reservedMemory = null;
$this->timeFinished = microtime(true);
$executionTime = round($this->timeFinished - $this->timeStarted, 3);
$this->logger->info("Execution time: {$executionTime} sec.");
$memoryPeak = format_bytes(memory_get_peak_usage(true));
$this->logger->info("Memory peak usage: {$memoryPeak}.");
$this->logger->info('%separator%');
$handlers = (array) $this->logger->getHandlers();
foreach ($handlers as $handler) {
$handler->close();
}
}

You'll see, that the first thing I do - is releasing that memory:

$this->reservedMemory = null;

And after that, I'm able to do all stuff, which you can see there below (which requires a bit of memory too).

PS: I've saw that trick at Monolog actually, so hat tips to Monolog :)
https://github.com/Seldaek/monolog/blob/master/src/Monolog/ErrorHandler.php

@JayBizzle
Copy link
Author

Thanks for taking the time to explain 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants