Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Memory Management in WordPress

923 views

Published on

A talk I gave at WordCamp Sofa 2016 on measuring and optimizing memory usage, dealing with memory related errors, as well as monitoring server memory health.

Published in: Internet
  • Be the first to comment

Memory Management in WordPress

  1. 1. Keep Your Memory Under Control Konstantin Kovshenin konstantin.blog
  2. 2. Fast
  3. 3. Fast Expensive
  4. 4. Fast Expensive Never enough
  5. 5. Memory Usage free top, htop, ...
  6. 6. free
  7. 7. free
  8. 8. top
  9. 9. top
  10. 10. PHP / WordPress
  11. 11. Zend Memory Manager
  12. 12. Zend Memory Manager OS Kernel foo.php $a = 5; malloc, mmap, ...
  13. 13. memory_get_usage() memory_get_peak_usage() Debug Bar, Query Monitor Memory Usage
  14. 14. $before = memory_get_usage(); // Do something $after = memory_get_usage(); echo $after - $before;
  15. 15. Environment loaded Init action Shutdown action 12.9 MB 14.1 MB 14.4 MB
  16. 16. 12.9 MB 14.1 MB 14.4 MB 12.9 MB 27.3 MB 28.6 MB Environment loaded Init action Shutdown action
  17. 17. Fatal error: Allowed memory size of 134217728 bytes exhausted
  18. 18. ... tried to allocate 8552 bytes in /path/to/wp-includes/wp-db.php on line 1775
  19. 19. ... tried to allocate 8552 bytes in /path/to/wp-includes/wp-db.php on line 1775 1775: while ( $row = mysqli_fetch_object( ...
  20. 20. ... tried to allocate 8552 bytes in /path/to/wp-includes/wp-db.php on line 1775 1775: while ( $row = mysqli_fetch_object( ...
  21. 21. Google...
  22. 22. ini_set( 'memory_limit', '2048M' );
  23. 23. Stop
  24. 24. ini_set( 'memory_limit', '2048M' );
  25. 25.   script.php script.php
  26. 26.   Zend Memory Manager script.php 2GB 2GB script.php
  27. 27.   Zend Memory Manager Kernel script.php 2GB 2GB 2x2GB script.php
  28. 28. OOM Killer
  29. 29. OOM Killer mysqld nginx sshd postfix fail2ban
  30. 30. 1. Fine-tune the OOM
  31. 31. 1. Fine-tune the OOM 2. Add more Swap memory
  32. 32. 1. Fine-tune the OOM 2. Add more Swap memory 3. Figure things out
  33. 33. ... tried to allocate 8552 bytes in /path/to/wp-includes/wp-db.php on line 1775
  34. 34. xdebug xhprof, tideways memprof Memory Profiling
  35. 35. Call Stack: 34032184 6. do_action() .../htdocs/wp-settings.php:393 34925000 7. call_user_func_array:{}() .../wp-includes/plugin.php:525 34925104 8. my_plugin_init() .../wp-includes/plugin.php:525 134139664 9. wpdb->query() .../wp-content/plugins/memory.php:25 134172496 10. mysqli_fetch_object() .../wp-includes/wp-db.php:1775
  36. 36. XDEBUG_TRACE tracefile-analyser.php Memory Profiling
  37. 37. vim trace.xt
  38. 38. Why?
  39. 39. posts_per_page => -1
  40. 40. // Pretty bad $posts = get_posts( array( 'posts_per_page' => -1, // ... ) ); foreach ( $posts as $post ) { // ... }
  41. 41. // A little better $paged = 1; while ( $posts = get_posts( array( 'posts_per_page' => 100, 'paged' => $paged++, ) ) ) { foreach ( $posts as $post ) { // ... } }
  42. 42. // Good wp_suspend_cache_addition( true ); $paged = 1; while ( $posts = get_posts( array( 'posts_per_page' => 100, 'paged' => $paged++, ) ) ) { // foreach ... } wp_suspend_cache_addition( false );
  43. 43. file_get_contents()
  44. 44. readfile() fopen/fread/feof X-Accel-Redirect
  45. 45. ob_start()
  46. 46. // Not so good ob_start( 'ob_callback' ); // Much better ob_start( 'ob_callback', 1024*1024*2 ); function ob_callback( $content, $phase ) { // ... }
  47. 47. Context
  48. 48. is_admin() is_network_admin() is_front_page() is_singular() ... template_redirect wp_head wp_footer shutdown cron Context
  49. 49. Monitoring top, free cat /proc/meminfo cat /proc/pid/{status,maps}
  50. 50. add_action( 'shutdown', function() { $line = json_encode( array( 'timestamp' => time(), 'request_uri' => $_SERVER['REQUEST_URI'], 'peak_memory' => memory_get_peak_usage(), ) ); file_put_contents( '/tmp/php-memory.log', $line . PHP_EOL, FILE_APPEND | LOCK_EX ); });
  51. 51. Kibana Elasticsearch Logstash php-memory.log
  52. 52. Munin
  53. 53. 1. Choose the right context
  54. 54. 2. Profile with large datasets
  55. 55. 3. Keep consumption under control
  56. 56. konstantin.blog/wcsof2016
  57. 57. Attributions: Success Kid (Sammy Griner): https://en.wikipedia.org/wiki/Success_Kid Ninja character: http://www.vecteezy.com/vector-art/88129-ninja-vector-set Blood spatter CC-Attr-ShareALike 4.0: http://www.freevector.com/splattered-blood-graphics-set User icons by FontAwesome, SIL OFL 1.1: http://fontawesome.io

×