protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

php protobuf decoding memory leak

Open fables-tales opened this issue 7 years ago • 5 comments

What version of protobuf and what language are you using? Version: master Language: PHP

What operating system (Linux, Windows, ...) and version? linux, ubuntu 18.04 docker container

What runtime / compiler are you using (e.g., python version or gcc version)

root@d6e88ef3fc0d:/workspace# gcc --version
gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

root@d6e88ef3fc0d:/workspace# php --version
PHP 7.2.19-0ubuntu0.18.04.2 (cli) (built: Aug 12 2019 19:34:28) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.19-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies
root@d6e88ef3fc0d:/workspace#

What did you do? While attempting to decode a large google ads report in a loop, I determined that memory allocation was increasing even after all references to the messages were free, a minimal reproduction example is:

<?php
namespace Google\Ads\GoogleAds\V2\Services;
require __DIR__ . '/google-ads-php/vendor/autoload.php';

use Google\Ads\GoogleAds\V2\Services\SearchGoogleAdsResponse;
function dec($proto_data) {
  $dec = new SearchGoogleAdsResponse();
  $dec->mergeFromString($proto_data);
  return $dec;
}
while (true) {
  $b = dec("");
  unset($b);
  gc_collect_cycles();
}

Running this for about 3 minutes allocated all the memory on my machine and crashed it.

What did you expect to see This example should probably reach some plateau of memory usage and then not allocate any more.

Anything else we should know about your project / environment

I have a reproducible docker container on git on borg I can share internally.

fables-tales avatar Aug 26 '19 14:08 fables-tales

We have some memory leak test: https://github.com/protocolbuffers/protobuf/blob/master/php/tests/test.sh#L28

I guess what we have doesn't cover your case. Could you do me a favor to simplify SearchGoogleAdsResponse to see which field actually cause the problem?

TeBoring avatar Aug 26 '19 22:08 TeBoring

My suspect is that map field has memory leak. Could you help verify be removing all map fields (also transitively) from SearchGoogleAdsResponse?

TeBoring avatar Aug 26 '19 23:08 TeBoring

I got the same problem as samphippen, memory will always increasing with very simple empty protobuf Message call of mergeFromString

syntax = "proto3";

package grpc.test;

message EmptyMessage {
}
<?php

use Grpc\Test\EmptyMessage;

include_once __DIR__ . '/grpc/GPBMetadata/Grpc/Test.php';
include_once __DIR__ . '/grpc/Grpc/Test/EmptyMessage.php';

$message = new EmptyMessage();
$str = $message->serializeToString();

while(true) {
  $message = new EmptyMessage();
  $message->mergeFromString($str);
  unset($message);
  gc_collect_cycles();
}

php version: 7.2.1 protobuf version: 3.9.1

starfalling avatar Sep 17 '19 13:09 starfalling

Also confirming an infinite increase in PHP's memory usage when using the protobuf C extension. Commenting out the protobuf extension and using the native PHP package as replacement eliminates the problem. There is absolutely a leak... somewhere.

frickenate avatar Nov 07 '19 20:11 frickenate

Ah, looks like I was not upgraded to the latest 3.10.0 pecl extension. It looks like #6483 fixed our memory leak (missing free on arrays and maps which was fixed back in August). No more problem here.

frickenate avatar Nov 12 '19 16:11 frickenate