php / php/php-src

Unserialized objects use significantly more memory than ones created with the normal constructor

オープン
#10,126 コメント 6 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Category: Engine Category: Serialization Extension: standard Status: Needs Triage
主要言語
C
スター
40.4k
フォーク
8.1k
平均マージ
2日 13時間
マージ済み PR(30日)
96

説明

Description

Consider the following code:

class SomeClass {
    public $a = 1;
    public $b = 2;
    public $c = 3;
    public $d = 4;
    public $e = 5;
    public $f = 6;
}

$s1 = serialize(new SomeClass());

function new_object() {
    return new SomeClass();
}

function unserialize_object() {
    global $s1;
    return unserialize($s1);
}


class OtherClass {
    public $a = 1;
    public $b = 2;
    public $c = 3;
    public $d = 4;
    public $e = 5;
    public $f = 6;
    
    public function __unserialize(array $attrs) {
        foreach ($attrs as $name => $value) 
            $this->$name = $value;
    }
}

$s2 = serialize(new OtherClass());

function new_object_other() {
    return new OtherClass();
}

function unserialize_object_other() {
    global $s2;
    return unserialize($s2);
}


foreach ([
    'new_object', 'unserialize_object',
    'new_object_other', 'unserialize_object_other',
] as $f) {
    $arr = [];
    for ($i = 0; $i < 50000; ++$i) {
        $arr[] = $f();
    }

    echo $f, " ", memory_get_usage() >> 20, "\n";
}

SomeClass contains some data. new_object() will create an instance, while unserialize_object() will unserialize an instance of exactly the same state (see $s1). When an array of 50k elements is created with one or another, the output for memory usage is as follows:

new_object 10
unserialize_object 43

I.e. the 50k instances created with the constructor use 10M memory, while the unserialized ones 43M. It seems that some data structures that are used while unserializing are not freed. However, when unsetting $arr to destroy the instances, memory consumption will drop to 1M, as expected.

Now consider OtherClass which has the __unserialize method. Nothing fancy, it just sets the attributes as the default handler would do. new_object_other() and unserialize_object_other() replicate the same logic as explained above, but for the new class. Output in this case is

new_object_other 10
unserialize_object_other 10

That is, memory consumption is the same regardless of how the objects are created.

Surplus memory usage grows with the number of attributes.

Tested with 8.1.2 and 8.2.0. Can also be seen on https://3v4l.org/ .

PHP Version

8.2.0

Operating System

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提供された PHP スクリプトを実行し、50,000 個のオブジェクトについて new_object()、unserialize_object()、および __unserialize() バリアントの memory_get_usage() を比較します。再現に関係する serialize()/unserialize() のパスを追跡します。通常どおり構築されたオブジェクトと比べて、同等のデフォルトでアンシリアライズされたオブジェクトが大幅に多くのメモリを保持しなくなれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
php
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。