Skip to content

Memory leak? #9

@yangxikun

Description

@yangxikun

I use apache jemeter to make benchmark on the example server(with litter modify):

public function onRequest(Request $request, Socket $socket)
    {
        $data = sprintf(
            'Hello to %s:%d from %s:%d!',
            $socket->getRemoteAddress(),
            $socket->getRemotePort(),
            $socket->getLocalAddress(),
            $socket->getLocalPort()
        );

        $body = $request->getBody();

        if ($body->isReadable()) {
            $data .= "\n\n";
            do {
                $data .= (yield $body->read());
            } while ($body->isReadable());
        }

        $coroutine = Coroutine\create(function () {
            $client = new Client();
            $encoder = new Http1Encoder();

            // Connect to a google.com IP.
            // Use Icicle\Dns\connect() in icicleio/dns package to resolve and connect using domain names.
            $data = '';
            //Make a http request to another site
            try {
                $socket = (yield Icicle\Socket\connect('10.123.5.34', 80));

                /** @var \Icicle\Http\Message\Response $response */
                $response = (yield $client->request($socket, 'GET', 'an url', ['Cookie' => 'cookie=xxx']));

                $stream = $response->getBody();
                while ($stream->isReadable()) {
                    $data .= (yield $stream->read());
                }
            } catch (\Exception $e) {
                echo "Catch Exception by me : " . $e->getMessage() . "\n";
            }

            yield $data;
        });

        $coroutine->done(null, function (Exception $exception) {
            printf("Exception: %s\n", $exception);
        });
        $data .= (yield $coroutine);
        $sink = new MemorySink();
        yield $sink->end($data);

        $response = new BasicResponse(200, [
            'Content-Type' => 'text/plain',
            'Content-Length' => $sink->getLength(),
        ], $sink);

        yield $response;
    }

I found the memory use by the process is increase, and not decrease:

~/dev/icicle » ps -e -o 'pid,ppid,rsz,vsz,args' | grep php
114552   7223 125376 411320 php server.php

And there have many error output like:

Error when handling request from 192.168.103.1:51302: Failed to write to stream. Errno: 2; fwrite(): 2588 is not a valid stream resource

If I continue benchmark, will suffer a fatal error:

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted at /home/rokety/software/php-5.6.15/Zend/zend_execute.h:187 (tried to allocate 130968 bytes) in /home/rokety/dev/icicle/vendor/icicleio/http/src/Driver/Encoder/Http1Encoder.php on line 70

Another, If I ctrl+c the server script, it print a memory leak message:

/home/rokety/software/php-5.6.15/Zend/zend_vm_execute.h(944) :  Freeing 0x7F8E06527168 (32 bytes), script=/home/rokety/dev/icicle/server.php
=== Total 1 memory leaks detected ===

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions