Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:106092 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 29541 invoked from network); 28 Jun 2019 18:42:13 -0000 Received: from unknown (HELO mail.experimentalworks.net) (84.19.169.162) by pb1.pair.com with SMTP; 28 Jun 2019 18:42:13 -0000 Received: from maniacmansion.fritz.box (ppp-188-174-56-116.dynamic.mnet-online.de [188.174.56.116]) by mail.experimentalworks.net (Postfix) with ESMTPSA id C98545A715; Fri, 28 Jun 2019 17:59:08 +0200 (CEST) Message-ID: To: Benjamin Morel , PHP Internals Date: Fri, 28 Jun 2019 17:59:08 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.28.5-0ubuntu0.18.04.1 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Memory leak in eval()'d code From: johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=) On Fri, 2019-06-28 at 02:41 +0200, Benjamin Morel wrote: > Hi internals, > > I've tracked down a memory leak to an anonymous class created within > eval(): > > ``` > for ($i = 0; $i < 1000000; $i++) { > $object = eval('return new class {};'); > > if ($i % 1000 == 0) { > echo memory_get_usage() . "\n"; > } > } > > ``` > > The memory usage quickly ramps up and memory_limit is reached in > seconds. > Without eval(), the memory usage stays flat. I'm on 7.3.6. > > *Is this a bug?* Or is this some inherent limitation of eval() that > cannot > be fixed? I case this is non-trivial to fix. Each invocation recompile a new piece of code, which creates a new class. Similar to for ($i = 0; $i < 1000000; $i++) { eval("function f$i(){}"); } f1(); f2(); ... or for ($i = 0; $i < 1000000; $i++) { file_put_contents("c$i.php", 'return new class {};'); $object = include "c$i.php"; } Theoretical it is thinkable we could either detect equal classes (but unlikely to happen ... if it's the same why do it in eval, zthus cost likely is higher than benefit in most cases?) or unload the class once it's not needed anymore (which is hard to detect with reflection and other mechanisms which aren't bound to an instance's zval and also not cheap) johannes