laruence / php-lua

This extension embeds the lua interpreter and offers an OO-API to lua variables and functions.
http://pecl.php.net/package/lua
Other
149 stars 50 forks source link

Memory leak when passing anonymous functions to lua interpreter #26

Open ComaVN opened 7 years ago

ComaVN commented 7 years ago

When passing anonymous PHP functions to LUA using assign or registerCallback, some memory is not reclaimed when the Lua object is unset.

See https://github.com/ComaVN/php-lua-memoryleak for a proof of concept.

hvt commented 7 years ago

I can affirm this behavior, also on older PHP and older php-lua versions:

$ php -v
PHP 5.6.30-1+deb.sury.org~trusty+1 (cli) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
$ dpkg -s liblua5.2-dev | grep Version
Version: 5.2.3-1
$ php -i | grep '^lua '
lua support => enabled
lua extension version => 1.1.0
lua release => Lua 5.2.3
lua copyright => Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
lua authors => R. Ierusalimschy, L. H. de Figueiredo, W. Celes
$ php test-assign.php
Mem at start of test (real): 235000 (262144)
Mem at start of loop (real): 235184 (262144)
Mem at end of loop (real): 262520 (524288)
Mem at start of loop (real): 262520 (524288)
Mem at end of loop (real): 262960 (524288)
...
Mem at start of loop (real): 721288 (1048576)
Mem at end of loop (real): 721656 (1048576)
Mem at start of loop (real): 721656 (1048576)
Mem at end of loop (real): 722160 (1048576)
Mem at end of test (real): 695704 (1048576)
$ php test-registerCallback.php 
Mem at start of test (real): 235152 (262144)
Mem at start of loop (real): 235336 (262144)
Mem at end of loop (real): 262696 (524288)
Mem at start of loop (real): 262696 (524288)
Mem at end of loop (real): 263112 (524288)
...
Mem at start of loop (real): 721912 (1048576)
Mem at end of loop (real): 722360 (1048576)
Mem at start of loop (real): 722360 (1048576)
Mem at end of loop (real): 722824 (1048576)
Mem at end of test (real): 696344 (1048576)
hvt commented 7 years ago

Hey there, I was wondering whether I could somehow help fixing this problem. Does anyone have any pointers where to start investigating this?

sgolemon commented 6 years ago

https://github.com/laruence/php-lua/pull/31 fixes this.

laruence commented 6 years ago

the problem is these callbacks are stashed as lua class's static properties.. which could only be reclaimed in request shutdown phase.

hmm, yeah it is better to reclaim it after object's destruction... but, need some work to do...

ComaVN commented 6 years ago

the PoC still shows the same memory leak, am I doing anything wrong here? I don't see a difference with the latest version:

$ cat /etc/issue
Ubuntu 16.04.3 LTS \n \l

$ php -v
PHP 7.0.22-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.22-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies

$ dpkg -s liblua5.2-dev | grep Version
Version: 5.2.4-1ubuntu1

$ php -i | grep '^lua '
lua support => enabled
lua extension version => 2.0.5
lua release => Lua 5.2.4
lua copyright => Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
lua authors => R. Ierusalimschy, L. H. de Figueiredo, W. Celes

$ php test-assign.php
Mem at start of test (real): 362960 (2097152)
Mem at start of loop (real): 362992 (2097152)
Mem at end of loop (real): 363752 (2097152)
Mem at start of loop (real): 363752 (2097152)
Mem at end of loop (real): 364072 (2097152)
(...)
Mem at start of loop (real): 719336 (2097152)
Mem at end of loop (real): 719656 (2097152)
Mem at start of loop (real): 719656 (2097152)
Mem at end of loop (real): 719976 (2097152)
Mem at end of test (real): 719928 (2097152)

$ php test-registerCallback.php 
Mem at start of test (real): 363120 (2097152)
Mem at start of loop (real): 363152 (2097152)
Mem at end of loop (real): 363912 (2097152)
Mem at start of loop (real): 363912 (2097152)
Mem at end of loop (real): 364232 (2097152)
(...)
Mem at start of loop (real): 719496 (2097152)
Mem at end of loop (real): 719816 (2097152)
Mem at start of loop (real): 719816 (2097152)
Mem at end of loop (real): 720136 (2097152)
Mem at end of test (real): 720088 (2097152)