phpconpl 2013 - allowed memory size of x bytes exhausted

106
Allowed memory size of X bytes exhausted Piotr Pasich piotr.pasich @xsolve.pl @piotrpasich

Upload: piotr-pasich

Post on 18-May-2015

2.536 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Allowed memory size of X bytes exhausted

Piotr Pasich piotr.pasich @xsolve.pl @piotrpasich

Page 2: PHPConPl 2013 - Allowed memory size of X bytes exhausted

„A good understanding of how variables are stored

and manipulated is essential to becoming

a Hacker.” php.net

Page 3: PHPConPl 2013 - Allowed memory size of X bytes exhausted

„A good understanding of how variables are stored

and manipulated is essential to becoming

a Hacker.” php.net

Page 4: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Have you ever thought why don't you do this?

Page 5: PHPConPl 2013 - Allowed memory size of X bytes exhausted

public function foo() { $a = new stdClass; $b = array(); //(...) unset($a); unset($b); return; }

Page 6: PHPConPl 2013 - Allowed memory size of X bytes exhausted

It might save memory

Page 7: PHPConPl 2013 - Allowed memory size of X bytes exhausted

But it doesn't

Page 8: PHPConPl 2013 - Allowed memory size of X bytes exhausted

It...

Page 9: PHPConPl 2013 - Allowed memory size of X bytes exhausted

S...KS

Page 10: PHPConPl 2013 - Allowed memory size of X bytes exhausted

STACKS

Page 11: PHPConPl 2013 - Allowed memory size of X bytes exhausted
Page 12: PHPConPl 2013 - Allowed memory size of X bytes exhausted

First in, last out

Page 13: PHPConPl 2013 - Allowed memory size of X bytes exhausted

It's mostly used to store variables in functions

Page 14: PHPConPl 2013 - Allowed memory size of X bytes exhausted

So, variables live only in functions

Page 15: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Not PHP functions, but C functions

Page 16: PHPConPl 2013 - Allowed memory size of X bytes exhausted

One PHP function != One C function

Page 17: PHPConPl 2013 - Allowed memory size of X bytes exhausted

They are allocated and freed

automatically

Page 18: PHPConPl 2013 - Allowed memory size of X bytes exhausted

At function return the stack is popped

Page 19: PHPConPl 2013 - Allowed memory size of X bytes exhausted

public function foo($file1, $file2){ $obj = new Obj(); $data = array(); $data[] = $obj->importAFile($file1); $data[] = $obj->importAFile($file2); return $data; }

Page 20: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Does the memory allocated for $obj get freed after the return?

Page 21: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Yes, it is

Page 22: PHPConPl 2013 - Allowed memory size of X bytes exhausted

And when it isn't?

Page 23: PHPConPl 2013 - Allowed memory size of X bytes exhausted

public function foo($file1, $file2){ $obj = new Obj(); $data = array(); $data[] = $obj->importAFile($file1); $data[] = $obj->importAFile($file2); return $data; }

Page 24: PHPConPl 2013 - Allowed memory size of X bytes exhausted

public function importAFile($file) { //(...) return $this; }

Page 25: PHPConPl 2013 - Allowed memory size of X bytes exhausted

It frees all variables to which there are no references left

Page 26: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Size limited by OS

Page 27: PHPConPl 2013 - Allowed memory size of X bytes exhausted

HEAPS

Page 28: PHPConPl 2013 - Allowed memory size of X bytes exhausted
Page 29: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Fragmented

Page 30: PHPConPl 2013 - Allowed memory size of X bytes exhausted

No limit on memory size*

Page 31: PHPConPl 2013 - Allowed memory size of X bytes exhausted

You have to manage it

Page 32: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Variables can be accessed globally

Page 33: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Extensions Parsed code

Variables

Page 34: PHPConPl 2013 - Allowed memory size of X bytes exhausted

EXTENSIONS

Page 35: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Consume memory

Page 36: PHPConPl 2013 - Allowed memory size of X bytes exhausted

memory_get_usage() profiles the heap

Page 37: PHPConPl 2013 - Allowed memory size of X bytes exhausted

echo memory_get_usage(true);

Page 38: PHPConPl 2013 - Allowed memory size of X bytes exhausted

PHP 5.3 786 432

Page 39: PHPConPl 2013 - Allowed memory size of X bytes exhausted

PHP 5.4 262 144

Page 40: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Upgrade!

Page 41: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Also extensions

Page 42: PHPConPl 2013 - Allowed memory size of X bytes exhausted

PARSED CODE

Page 43: PHPConPl 2013 - Allowed memory size of X bytes exhausted

more code == more memory

Page 44: PHPConPl 2013 - Allowed memory size of X bytes exhausted

more code == more time

Page 45: PHPConPl 2013 - Allowed memory size of X bytes exhausted

but...

Page 46: PHPConPl 2013 - Allowed memory size of X bytes exhausted

OPcache

Page 47: PHPConPl 2013 - Allowed memory size of X bytes exhausted

sudo pecl install zendopcache-7.0.2

Page 48: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Substitute for APC

Page 49: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Stores precompiled script bytecode

in shared memory

Page 50: PHPConPl 2013 - Allowed memory size of X bytes exhausted
Page 51: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Difference ab -n 10000 -c 20

Page 52: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Without cache 1743.994 seconds

Page 53: PHPConPl 2013 - Allowed memory size of X bytes exhausted

OPcache 658.408 seconds

Page 54: PHPConPl 2013 - Allowed memory size of X bytes exhausted

VARIABLES

Page 55: PHPConPl 2013 - Allowed memory size of X bytes exhausted

$startMemory = memory_get_usage(); $array = range(1, 100000); echo memory_get_usage() - $startMemory, ' bytes';

Page 56: PHPConPl 2013 - Allowed memory size of X bytes exhausted

C 100 000 * 8 bytes = 800 000 bytes

Page 57: PHPConPl 2013 - Allowed memory size of X bytes exhausted

PHP 14 649 016 bytes

Page 58: PHPConPl 2013 - Allowed memory size of X bytes exhausted

!?

Page 59: PHPConPl 2013 - Allowed memory size of X bytes exhausted

All variables are represented by one structure

Page 60: PHPConPl 2013 - Allowed memory size of X bytes exhausted

The zval

Page 61: PHPConPl 2013 - Allowed memory size of X bytes exhausted

typedef struct _zval_struct { zvalue_value value; /* variable value */ zend_uint refcount__gc; /* reference counter */ zend_uchar type; /* value type */ zend_uchar is_ref__gc; /* reference flag */ } zval;

Page 62: PHPConPl 2013 - Allowed memory size of X bytes exhausted

typedef union _zvalue_value { long lval; /* long value */ double dval; /* double value */ struct { char *val; int len; /* this will always be set for strings */ } str; /* string (always has length) */ HashTable *ht; /* an array */ zend_object_value obj; /* stores an object store handle, and handlers */ } zvalue_value;

Page 63: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Each variable needs much more memory than in C

Page 64: PHPConPl 2013 - Allowed memory size of X bytes exhausted

| 64 bit | 32 bit --------------------------------------------------- zval | 24 bytes | 16 bytes + cyclic GC info | 8 bytes | 4 bytes + allocation header | 16 bytes | 8 bytes =================================================== zval (value) total | 48 bytes | 28 bytes =================================================== bucket | 72 bytes | 36 bytes + allocation header | 16 bytes | 8 bytes + pointer | 8 bytes | 4 bytes =================================================== bucket (array element) total | 96 bytes | 48 bytes =================================================== total total | 144 bytes | 76 bytes

Page 65: PHPConPl 2013 - Allowed memory size of X bytes exhausted

144 bytes * 100 000 = 1 440 0000

Page 66: PHPConPl 2013 - Allowed memory size of X bytes exhausted

What happened to the rest 249 016 bytes?

Page 67: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Unfilled buckets in C

Page 68: PHPConPl 2013 - Allowed memory size of X bytes exhausted

2^17 = 131 072 131 072 – 100 000 = 31 072 pointers

Page 69: PHPConPl 2013 - Allowed memory size of X bytes exhausted

31 072 pointers * 8 bytes =

248 576 missing bytes

Page 70: PHPConPl 2013 - Allowed memory size of X bytes exhausted

PHP ain’t C

Page 71: PHPConPl 2013 - Allowed memory size of X bytes exhausted

We can use SplFixedArray

Page 72: PHPConPl 2013 - Allowed memory size of X bytes exhausted

That’s 56 bytes per element

Page 73: PHPConPl 2013 - Allowed memory size of X bytes exhausted

SplFixedArray is writting 33 % faster reading 10% faster

Page 74: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Uff, now you know a lot

Page 75: PHPConPl 2013 - Allowed memory size of X bytes exhausted

„Am I a hacker now?”

Page 76: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Not yet, wait a second

Page 77: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Be like a ninja!

Page 78: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Looking for leaks

Page 79: PHPConPl 2013 - Allowed memory size of X bytes exhausted

PHP Fatal Error: Allowed memory size of 8388608 bytes exhausted

Page 80: PHPConPl 2013 - Allowed memory size of X bytes exhausted

ini_set("memory_limit","12M");

Page 81: PHPConPl 2013 - Allowed memory size of X bytes exhausted

ini_set("memory_limit",”512M");

Page 82: PHPConPl 2013 - Allowed memory size of X bytes exhausted

ini_set("memory_limit",”64G");

Page 83: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried

to allocate 2798048 bytes) in /***/app/http.php on line 6331

Page 84: PHPConPl 2013 - Allowed memory size of X bytes exhausted

That’s the one way

Page 85: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Let’s move to another

Page 86: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Xdebug will help you

Page 87: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Memory needs time

Page 88: PHPConPl 2013 - Allowed memory size of X bytes exhausted

You can debug it step by step

Page 89: PHPConPl 2013 - Allowed memory size of X bytes exhausted
Page 90: PHPConPl 2013 - Allowed memory size of X bytes exhausted

or by xdebug profiler

Page 91: PHPConPl 2013 - Allowed memory size of X bytes exhausted

xdebug.profiler_enable=1 xdebug.profiler_output_dir=/var/logs/profiler

Page 92: PHPConPl 2013 - Allowed memory size of X bytes exhausted

xdebug profiler

Page 93: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Holy moly!

Page 94: PHPConPl 2013 - Allowed memory size of X bytes exhausted

xdebug.trace_output_name

Page 95: PHPConPl 2013 - Allowed memory size of X bytes exhausted

xdebug.collect_params

Page 96: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Default 0.0004 114272 -> str_split() ../trace.php:8 0.0153 117424 -> ret_ord() ../trace.php:10 1 0.0004 114272 -> str_split(string(6)) ../trace.php:8 0.0007 117424 -> ret_ord(string(1)) ../trace.php:10 3 0.0004 114272 -> str_split('Xdebug') ../trace.php:8 0.0007 117424 -> ret_ord('X') ../trace.php:10 4 0.0004 114272 -> str_split('Xdebug') ../trace.php:8 0.0007 117424 -> ret_ord($c = 'X') ../trace.php:10

Page 97: PHPConPl 2013 - Allowed memory size of X bytes exhausted

What's inside?

Page 98: PHPConPl 2013 - Allowed memory size of X bytes exhausted

What's inside?

Page 99: PHPConPl 2013 - Allowed memory size of X bytes exhausted

You can use Webgrind

Page 100: PHPConPl 2013 - Allowed memory size of X bytes exhausted

You can use Webgrind

Page 101: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Analyzer in PHPStorm

Page 102: PHPConPl 2013 - Allowed memory size of X bytes exhausted
Page 103: PHPConPl 2013 - Allowed memory size of X bytes exhausted
Page 104: PHPConPl 2013 - Allowed memory size of X bytes exhausted

After all

Page 105: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Everything consumes memory

Page 106: PHPConPl 2013 - Allowed memory size of X bytes exhausted

Thank you!