-
-
Notifications
You must be signed in to change notification settings - Fork 586
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
Memory leaks #1150
Comments
if you change: $server->handle('/', function($request, $response) use ($serializer) {
$data = ['OK'];
$response->send($serializer->serialize($data, 'json'));
}); to for ($i = 0, $i<1000000; $i++) {
$data = ['OK'];
$serializer->serialize($data, 'json');
}; and just run it as a script? |
Thanks for your help. |
Then is related on how your application is built. |
Is this a reactphp application? |
I think it is.
No, it's an application based on swoole, an extreme fast asynchronous engine which is widely used in china. |
Consider that if you are using symfony, their compiled DI container leaks memory when executed in a loop. But I think here you will have to figure it out. (related to https://bugs.php.net/bug.php?id=75914) |
Wow...I'm using symfony DI indeed! |
if you disable the feature that splits the DI container into multiple files, you should have no leaks. |
Sorry I have no idea how to disable this feature... |
After many times of try, I believe it's not symfony DI's fault, because I even rewrite the DI container but without any effects... <?php
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializerInterface;
use Swoole\Coroutine\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
require 'vendor/autoload.php';
$serializer = SerializerBuilder::create()->build();
class StartCommand extends Command
{
/**
* @var SerializerInterface
*/
private $serializer = null;
public function setSerializer(SerializerInterface $serializer)
{
$this->serializer= $serializer;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
go(function () {
$server = new Server('127.0.0.1', 3000);
$server->handle('/', function (Request $request, Response $response) {
$data = ['OK'];
if ($this->serializer) {
$response->end($this->serializer->serialize($data, 'json'));
} else {
$response->end(json_encode($data));
}
});
$server->start();
});
return 0;
}
}
// serializer and symfony console
$test1 = function () use ($serializer) {
$app = new Application();
$command = new StartCommand('start');
$command->setSerializer($serializer);
$app->add($command);
$app->run();
};
// json_encode and symfony console
$test2 = function () {
$app = new Application();
$app->add(new StartCommand('start'));
$app->run();
};
// serializer and no symfony console
$test3 = function () use ($serializer) {
go(function () use ($serializer) {
$server = new Server('127.0.0.1', 3000);
$server->handle('/', function (Request $request, Response $response) use ($serializer) {
$data = ['OK'];
$response->end($serializer->serialize($data, 'json'));
});
$server->start();
});
};
//$test1(); // About 88M memory usage(in monitor) after 50,000 requests
//$test2(); // About 8.5M memory usage(in monitor) after 50,000 requests
//$test3(); // About 14.5M memory usage(in monitor) after 50,000 requests |
What is the memory after a single iteration? |
I didn't test it. BUT I find a usable solution ... <?php
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializerInterface;
use Swoole\Coroutine\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
require 'vendor/autoload.php';
$serializer = SerializerBuilder::create()->build();
class StartCommand extends Command
{
/**
* @var SerializerInterface
*/
private $serializer = null;
public function setSerializer(SerializerInterface $serializer)
{
$this->serializer= $serializer;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$server = new Server('127.0.0.1', 3000);
$server->handle('/', function (Request $request, Response $response) {
$data = ['OK'];
if ($this->serializer) {
$response->end($this->serializer->serialize($data, 'json'));
} else {
$response->end(json_encode($data));
}
});
$server->start();
return 0;
}
}
// This is correct
go(function () use ($serializer) {
$app = new Application();
$command = new StartCommand('start');
$command->setSerializer($serializer);
$app->add($command);
$app->run();
}); |
When having memory leaks, I use https://github.com/BitOne/php-meminfo to
hunt them down
…On Wed, 18 Dec 2019, 09:36 crystoy, ***@***.***> wrote:
What is the memory after a single iteration?
I didn't test it.
BUT I find a usable solution ...
Just place the go() function at the top level, not in execute() method
will solve the problem...
I don't know why, but it just works!
There maybe some conflicts between swoole and symfony console, I can't
figure it out, hope someone can do..
Anyway, thanks a lot for your careful help
<?phpuse JMS\Serializer\SerializerBuilder;use JMS\Serializer\SerializerInterface;use Swoole\Coroutine\Http\Server;use Swoole\Http\Request;use Swoole\Http\Response;use Symfony\Component\Console\Application;use Symfony\Component\Console\Command\Command;use Symfony\Component\Console\Input\InputInterface;use Symfony\Component\Console\Output\OutputInterface;require 'vendor/autoload.php';$serializer = SerializerBuilder::create()->build();class StartCommand extends Command{ /** * @var SerializerInterface */ private $serializer = null; public function setSerializer(SerializerInterface $serializer) { $this->serializer= $serializer; } protected function execute(InputInterface $input, OutputInterface $output) { $server = new Server('127.0.0.1', 3000); $server->handle('/', function (Request $request, Response $response) { $data = ['OK']; if ($this->serializer) { $response->end($this->serializer->serialize($data, 'json')); } else { $response->end(json_encode($data)); } }); $server->start(); return 0; }}// This is correctgo(function () use ($serializer) { $app = new Application(); $command = new StartCommand('start'); $command->setSerializer($serializer); $app->add($command); $app->run();});
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1150?email_source=notifications&email_token=AAF5UJY7F22SEBXYE6FOYNLQZHOHRA5CNFSM4J3VFXKKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEHFLE2A#issuecomment-566932072>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAF5UJY2NAHGHSBRNJUITH3QZHOHRANCNFSM4J3VFXKA>
.
|
OK, nice tool, I'll have a try later, thanks |
Steps required to reproduce the problem
The simplified code looks like this:
Expected Result
Actual Result
I'm not sure if it is caused by this serializer, but when I remove it, everything goes well again.
Does anyone else encoutered this problem?
Any help would be appreciated, thanks.
The text was updated successfully, but these errors were encountered: