Advertisement
fooflington

PHP memory leak in unserialize() test script

Oct 2nd, 2013
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1. <?php
  2.  
  3. function generateRandomString($length = 10) {
  4.     $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  5.     $randomString = '';
  6.     for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, strlen($characters) - 1)]; }
  7.     return $randomString;
  8. }
  9.  
  10. class obj {
  11.     public $items = array();
  12.     public $data;
  13. }
  14.  
  15. function subobj($obj, $depth=5) {
  16.         for($i = 0; $i < 5; $i++) {
  17.                 $obj->items[$i] = new obj;
  18.                 $obj->items[$i]->data = generateRandomString(64);
  19.         if($depth>1) subobj($obj->items[$i], $depth-1);
  20.         }
  21. }
  22.  
  23. print "Populating object...";
  24. $obj = new obj;
  25. for($i = 0; $i < 10; $i++) {
  26.     subobj($obj);
  27. }
  28. print "\nSerializing...";
  29.  
  30. $data = serialize($obj);
  31.  
  32. print "\nLooping...\n";
  33. $iterations = 20000;
  34. while($iterations--) {
  35.     $newstr = unserialize($data);
  36.     if($iterations % 1000 == 0)
  37.         print "$iterations: " . sprintf('%0.2f', memory_get_usage()/1024/1024) . "Mb\n";
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement