Aws-sdk-php: Memory leak occurs when SqsClient is continuously called

Created on 5 Nov 2018  路  4Comments  路  Source: aws/aws-sdk-php

Executing the following code increases the usage memory more and more as requested memory is requested.

using version: latest

use Aws\Sqs\SqsClient;

    $client = new SqsClient([
        'profile' => 'default',
        'region' => 'ap-northeast-1',
        'version' => '2012-11-05'
     ]);

    while (true) {

        $receiveMessage = $client->receiveMessage([
            'AttributeNames' => ['SentTimestamp'],
            'MaxNumberOfMessages' => 1,
            'MessageAttributeNames' => ['All'],
            'QueueUrl' => 'http://192.168.20.31:4576/queue/test', // REQUIRED
            'Endpoint' => 'http://192.168.20.31:4576',
            'WaitTimeSeconds' => 0,
        ]);

        echo memory_get_usage();
    }

I think that there is something that the guzzle sync method obtained keeps on for the promise that has not been scheduled yet maybe.

guidance

Most helpful comment

Hi @sh-ogawa, thanks for reaching out to us about this. This behavior sounds similar to #1645 where we found that the SDK (or one of its dependencies) does appear to have cyclic references that have to be cleaned up by PHP's garbage collector, however garbage collection does occur naturally after a certain amount of references have built up.

When running the code you provided I see that memory usage builds up from 3.29MB to about 8.19MB across ~500 iterations of the loop before PHP's garbage collection begins naturally. This is a result of the SDK favoring a faster runtime over a lighter memory footprint, however you can call gc_collect_cycles() manually to override this behavior. Including this function at the end of the loop resulted in the memory usage remaining at a consistent 3.29MB instead of slowly increasing to ~8MB before garbage collection begins automatically.

All 4 comments

Hi @sh-ogawa, thanks for reaching out to us about this. This behavior sounds similar to #1645 where we found that the SDK (or one of its dependencies) does appear to have cyclic references that have to be cleaned up by PHP's garbage collector, however garbage collection does occur naturally after a certain amount of references have built up.

When running the code you provided I see that memory usage builds up from 3.29MB to about 8.19MB across ~500 iterations of the loop before PHP's garbage collection begins naturally. This is a result of the SDK favoring a faster runtime over a lighter memory footprint, however you can call gc_collect_cycles() manually to override this behavior. Including this function at the end of the loop resulted in the memory usage remaining at a consistent 3.29MB instead of slowly increasing to ~8MB before garbage collection begins automatically.

Hi @diehlaws , thanks for your reply.

We can also manually invoke garbage collection in PHP.
It became very helpful, so I would like to try this suggestion.
Thank you very much.

I was able to confirm that the memory does not continue to increase by using gc_collect_cycles().
Thank you very much.

Is this being caused by this following issue:
https://forums.aws.amazon.com/message.jspa?messageID=695298

The short of it being something with cURL SSL verification being leaky.

Also this: https://github.com/aws/aws-sdk-php/issues/1273

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pihish picture pihish  路  3Comments

chiukit picture chiukit  路  4Comments

andy3rdworld picture andy3rdworld  路  4Comments

sm2017 picture sm2017  路  4Comments

carlalexander picture carlalexander  路  4Comments