Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107310 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 11267 invoked from network); 24 Sep 2019 12:01:06 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 24 Sep 2019 12:01:06 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id E8D482C047C for ; Tue, 24 Sep 2019 02:39:57 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Virus: No Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Tue, 24 Sep 2019 02:39:57 -0700 (PDT) Received: by mail-io1-xd2c.google.com with SMTP id b136so2841560iof.3 for ; Tue, 24 Sep 2019 02:39:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=AEPn1JhivZN51lrGXgGOwHv99TgZnmD7f4zDpoXyCG0=; b=sMQ3riRFgX7SvTmdeWhb73yXY344twEGKEf8JXV8KyuZgIT+ivX8AO4PDgKLHjWMN1 KdF+h6FAhlf3d9V6V43cww+kZxmhnfGtewU3szI2a2z+irsQ16zkhOWzh0A74KBDg4yE LJRe92vrUYpXgWA4s8Be8TmaSRvfsb8WSE5HiBt3ouhdSQ10BS5a5AOCz1v525DJn2md cM1KXs8BtWI1td1lXFzhCdZeH6wvErjmNUmWuP5mf7rblw3H0OgKbHamylbYDzUZIpqh wIl7SO7qa78Ru0r0RIKO0o3oDWi6mdcOdnpC94f9NNNDO8TFzIw3LY+oWoGx4xt9+Ms0 4Whw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=AEPn1JhivZN51lrGXgGOwHv99TgZnmD7f4zDpoXyCG0=; b=tffS/Ud+JYOvKFYXEyehgBu7P4begCRU5vVQcjS7iKYYdjfWyZ6xTyaQBEIUhwQKXX FoJD5P/UCPUFortws/l1N/OcAY1E/87cM1VH9HatmjDe1WbSSEANwxz1mjLUDdMOeyte KSJqzndENvxKKPzTHaSAOduyCQtAZI73qy7/nKsCGVB7EnYMCI8u0KOliRvsTv0A+B+0 9L0Ox37IoY4pTMnOZUbR01p6So7IJKT+fOaPrjhS5Wdq8YhqndYGL2UYm/cjTNH8omZW 8WZnSyIvkaIsdtg5X5g+bQ1S89qBTwTNlj0ldULg2I5xeBuP//PKY9SBqBxafvuqvaHU qN2g== X-Gm-Message-State: APjAAAVuxD2rh1RTRyuAzea84DvkqFhnm2Tn6RWED7/4p3b8TGeo7eni IOA0ft/BOGEFyxgNIPYHIep9UVKx8ITHwK+8ZJPHBPLE63IfiQ== X-Google-Smtp-Source: APXvYqzhlWg6vQOmToGMdWWRsSipFuyeY4ikKIwF4ytGxNQ9ZY3axS2JFn4poLDeBZtsoQrTYgvxMohQtnBokGToxDc= X-Received: by 2002:a5e:9e04:: with SMTP id i4mr2473258ioq.175.1569317996564; Tue, 24 Sep 2019 02:39:56 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 24 Sep 2019 02:39:45 -0700 Message-ID: To: bishop@php.net Cc: "internals@lists.php.net" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Envelope-From: Subject: Re: [PHP-DEV] Shutdown Memory Allowance (aka Soft Memory Limit) From: sarkedev@gmail.com (Peter Stalman) On Tue, Sep 24, 2019 at 12:01 AM Bishop Bettini wrote: > > On Tue, Sep 24, 2019 at 2:26 AM Peter Stalman wrote: >> >> When PHP runs out of memory, a fatal error is triggered and whatever shu= tdown >> functions or error handlers take over. >> >> However, in the case of error logging, or just logging in general, there= often >> needs to be additional memory used to accommodate the final logging proc= ess. >> >> This can sort of be accomplished in userland a few ways: >> >> 1. Pre-allocating memory in a variable, such as the Yii2 error handler >> (http://bit.ly/2kLnpd2), but this requires wasting memory on every reque= st. >> >> 2. Continuously checking the memory usage, but this increases code compl= exity >> needlessly and also wastes resources with constant checking. >> >> 3. A second process with its own memory allowance, but this also increas= es >> complexity and transferring the required data for logging would require >> serialization without using additional memory. I'm not sure how this wo= uld be >> accomplished. >> >> So I would like to suggest an option for setting a shutdown memory allow= ance, >> which would be the amount of additional memory allowed to be used by any >> registered error handlers or shutdown functions. >> >> I think a C implementation of this in PHP would be far more efficient th= an the >> userland implementionations I mentioned. > > > Memory parachutes help when against a hard limit, but I'm not sure they'r= e applicable in the soft limit scenario you describe. When the OOM conditio= n fires, PHP resets its soft tracking of memory. You are free to allocate a= s much on the stack of the shutdown function, up to the soft limit again. > > Here[1] we see the shutdown function called because of OOM, then we alloc= ate a local variable that consumes less than the soft limit (which is ok), = while here[2] we see the shutdown function itself is constrained to the sof= t limit (it's killed b/c of OOM). > > Perhaps I am misunderstanding the scenario. Could you elaborate further, = perhaps provide a concrete example demonstrating where a parachute would be= needed? > > [1]:https://3v4l.org/bXMlX > [2]:https://3v4l.org/HDkRc Hi Bishop, Thanks for your reply. However, I do not think your statement that the mem= ory limit resets is correct. At least, not in a practical real-world sense, as= the logger (or perhaps other shutdown functions as well) need to be able to acc= ess Your examples do not really work for a few reasons. I don't think 2048 is = a valid value for the memory limit, as the output in your examples show it is 2 mb (which I believe is the minimum). The docs also state the number is in bytes. Even so, this is not a real-world number, and the way PHP handles it's memo= ry I don't think using only one variable will give us a real-world example eithe= r. As you can see from your first example, PHP tries to assign an additional 2= mb just to add another element to the array. I believe there is dome doubling going on with the memory allocation, when increasing the array size, so it doesn't have to be done on every append. Since that additional assignment failed, the actual memory usage goes back to what it was before the assignm= ent, which means any subsequent memory usage can be up to the 2 mb that you trie= d to assign. That's why the smaller loop in the shutdown function works, as the original variable is probably only about 1 mb in size. The fact that your second example tries to assign only 516 kb when it fails= at the memory limit supports this, and also suggests that that array was much smaller when it hit the limit. If it could assign as much as the original,= then it would have tried to increase the memory allocation by about the same amo= unt. I've put together my own example as you suggested, using an array of string= s instead. This would reduce the incremental memory allocations as there is = no longer a single giant variable being doubled. It fails when trying to assi= gn 416 kb. https://3v4l.org/BqfvU If you are correct then the shutdown function should be able to recreate th= e original array and not run out of memory. However, as you can see, it fail= s after only assigning 2 of the 100 elements of the original array, which sug= gests the shutdown function is using the same memory and it's limit. Thanks, Peter