LoginSignup
22
21

More than 5 years have passed since last update.

PHP: メモリリークのデバッグ、肥大化するオブジェクトを探す

Last updated at Posted at 2013-12-04

PHP: メモリリークのデバッグ、肥大化するオブジェクトを探す

memory_get_usage()をデバッグプリントして、メモリ使用量が増えている箇所を特定する

print sprintf("MEMORY USAGE %0.2fMB",round(memory_get_usage() / 1024 /1024,2);

肥大化してそうなオブジェクトをファイルにダンプ

// objectをファイルにダンプ
file_put_contents(
    '/tmp/debug_object_' . time() . '.txt',
    print_r($object, TRUE)
);

// $objectを操作する処理をして・・・

// またobjectをファイルにダンプ
file_put_contents(
    '/tmp/debug_object_' . time() . '.txt',
    print_r($object, TRUE)
);

※処理が早くファイル名を分けるのにtime()で不十分なときにはmicrotime()を使うか、sleep()を挟む

ダンプファイルのリストでファイルサイズが大きくなっていないか確認

ls -l /tmp/debug_object_*

ファイルサイズが大きくなっていたらどの要素が変わっているかdiffで確認

diff /tmp/debug_object_1386117835.txt /tmp/debug_object_1386117837.txt 
22
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
21