SlideShare a Scribd company logo
Keep Your Memory
Under Control
Konstantin Kovshenin
konstantin.blog
Memory Management in WordPress
Fast
Fast
Expensive
Fast
Expensive
Never enough
Memory Usage
free
top, htop, ...
free
free
top
top
PHP / WordPress
Zend Memory Manager
Zend Memory Manager
OS Kernel
foo.php
$a = 5;
malloc, mmap, ...
memory_get_usage()
memory_get_peak_usage()
Debug Bar, Query Monitor
Memory Usage
$before = memory_get_usage();
// Do something
$after = memory_get_usage();
echo $after - $before;
Environment loaded
Init action
Shutdown action
12.9 MB
14.1 MB
14.4 MB
12.9 MB
14.1 MB
14.4 MB
12.9 MB
27.3 MB
28.6 MB
Environment loaded
Init action
Shutdown action
Fatal error: Allowed memory size
of 134217728 bytes exhausted
... tried to allocate 8552 bytes in
/path/to/wp-includes/wp-db.php
on line 1775
... tried to allocate 8552 bytes in
/path/to/wp-includes/wp-db.php
on line 1775
1775: while ( $row = mysqli_fetch_object( ...
... tried to allocate 8552 bytes in
/path/to/wp-includes/wp-db.php
on line 1775
1775: while ( $row = mysqli_fetch_object( ...
Google...
ini_set( 'memory_limit', '2048M' );
Memory Management in WordPress
Stop
ini_set( 'memory_limit', '2048M' );


script.php
script.php


Zend
Memory
Manager
script.php
2GB
2GB
script.php


Zend
Memory
Manager
Kernel
script.php
2GB
2GB
2x2GB
script.php
OOM Killer
OOM Killer
mysqld
nginx
sshd
postfix
fail2ban
1. Fine-tune the OOM
1. Fine-tune the OOM
2. Add more Swap memory
1. Fine-tune the OOM
2. Add more Swap memory
3. Figure things out
... tried to allocate 8552 bytes in
/path/to/wp-includes/wp-db.php
on line 1775
xdebug
xhprof, tideways
memprof
Memory Profiling
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
XDEBUG_TRACE
tracefile-analyser.php
Memory Profiling
vim trace.xt
Why?
posts_per_page => -1
// Pretty bad
$posts = get_posts( array(
'posts_per_page' => -1,
// ...
) );
foreach ( $posts as $post ) {
// ...
}
// A little better
$paged = 1;
while ( $posts = get_posts( array(
'posts_per_page' => 100,
'paged' => $paged++,
) ) ) {
foreach ( $posts as $post ) {
// ...
}
}
// 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 );
file_get_contents()
readfile()
fopen/fread/feof
X-Accel-Redirect
ob_start()
// Not so good
ob_start( 'ob_callback' );
// Much better
ob_start( 'ob_callback', 1024*1024*2 );
function ob_callback( $content, $phase ) {
// ...
}
Context
is_admin()
is_network_admin()
is_front_page()
is_singular()
...
template_redirect
wp_head
wp_footer
shutdown
cron
Context
Monitoring
top, free
cat /proc/meminfo
cat /proc/pid/{status,maps}
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 );
});
Kibana
Elasticsearch
Logstash
php-memory.log
Munin
1. Choose the right context
2. Profile with large datasets
3. Keep consumption
under control
konstantin.blog/wcsof2016
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
Memory Management in WordPress

More Related Content

TXT
ODP
Caching and tuning fun for high scalability
PPTX
Dc kyiv2010 jun_08
PDF
Beyond Golden Containers: Complementing Docker with Puppet
PDF
Manage WordPress with Awesome using wp cli
PDF
HTTP Caching and PHP
PDF
Scaling antispam solutions with Puppet
PDF
Caching and tuning fun for high scalability
Dc kyiv2010 jun_08
Beyond Golden Containers: Complementing Docker with Puppet
Manage WordPress with Awesome using wp cli
HTTP Caching and PHP
Scaling antispam solutions with Puppet

What's hot (20)

ODP
NUMOSS 4th Week - Commandline Tutorial
PDF
WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli
KEY
Mysqlnd uh
KEY
Operation Oriented Web Applications / Yokohama pm7
PDF
DevOps: Falando um pouco sobre desenvolvimento orientado a testes com Puppet
PDF
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
PPT
Drupal Performance - SerBenfiquista.com Case Study
PPTX
agri inventory - nouka data collector / yaoya data convertor
ODP
Varnish: Making eZ Publish sites fly
PPTX
MongoDB Replication (Dwight Merriman)
PDF
Mac OS X Lion で作る WordPress local 環境
PPTX
Jk rubyslava 25
PDF
File::CleanupTask
ODP
Redmine on amazon ec2
PDF
Continuous deployment of puppet modules
PPTX
Developers’ mDay 2019. - Rastko Vasiljević, SuperAdmins – Infrastructure as c...
PDF
Profiling with Xhprof
DOC
Compile kernel
PDF
Introduction to ansible
PDF
Puppet Camp 2012
NUMOSS 4th Week - Commandline Tutorial
WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli
Mysqlnd uh
Operation Oriented Web Applications / Yokohama pm7
DevOps: Falando um pouco sobre desenvolvimento orientado a testes com Puppet
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
Drupal Performance - SerBenfiquista.com Case Study
agri inventory - nouka data collector / yaoya data convertor
Varnish: Making eZ Publish sites fly
MongoDB Replication (Dwight Merriman)
Mac OS X Lion で作る WordPress local 環境
Jk rubyslava 25
File::CleanupTask
Redmine on amazon ec2
Continuous deployment of puppet modules
Developers’ mDay 2019. - Rastko Vasiljević, SuperAdmins – Infrastructure as c...
Profiling with Xhprof
Compile kernel
Introduction to ansible
Puppet Camp 2012
Ad

Viewers also liked (9)

PDF
Основые безопасности WordPress
PDF
Почему HTTPS соединение необходимо реализовать на Вашем сайте?
PDF
The Settings API
PDF
Пожиратели памяти в WordPress
PDF
7 убийц производительности WordPress
PDF
WordPress под нагрузкой: масштабирование и отказоустойчивость
PDF
Цикл разработки WordPress
PDF
Секреты WP_Query
Основые безопасности WordPress
Почему HTTPS соединение необходимо реализовать на Вашем сайте?
The Settings API
Пожиратели памяти в WordPress
7 убийц производительности WordPress
WordPress под нагрузкой: масштабирование и отказоустойчивость
Цикл разработки WordPress
Секреты WP_Query
Ad

Similar to Memory Management in WordPress (20)

PDF
Understanding PHP memory
ODP
Caching and tuning fun for high scalability
PDF
Bottom to Top Stack Optimization with LAMP
PDF
Bottom to Top Stack Optimization - CICON2011
PDF
Zend Server Data Caching
PPTX
PHP Performance tuning for Drupal 8
ODP
phptek13 - Caching and tuning fun tutorial
PDF
Os Gopal
PDF
WordPress Performance & Scalability
PPTX
PHP Performance with APC + Memcached
PDF
php & performance
PDF
Improving WordPress performance (xdebug and profiling)
PDF
PHPConPl 2013 - Allowed memory size of X bytes exhausted
PPT
Caching Data For Performance
PDF
Improving WordPress Performance with Xdebug and PHP Profiling
ODP
Caching and tuning fun for high scalability @ phpBenelux 2011
PPT
Performance and Scalability
PDF
Caching in WordPress
ODP
Caching and tuning fun for high scalability
ODP
Memory management in Linux
Understanding PHP memory
Caching and tuning fun for high scalability
Bottom to Top Stack Optimization with LAMP
Bottom to Top Stack Optimization - CICON2011
Zend Server Data Caching
PHP Performance tuning for Drupal 8
phptek13 - Caching and tuning fun tutorial
Os Gopal
WordPress Performance & Scalability
PHP Performance with APC + Memcached
php & performance
Improving WordPress performance (xdebug and profiling)
PHPConPl 2013 - Allowed memory size of X bytes exhausted
Caching Data For Performance
Improving WordPress Performance with Xdebug and PHP Profiling
Caching and tuning fun for high scalability @ phpBenelux 2011
Performance and Scalability
Caching in WordPress
Caching and tuning fun for high scalability
Memory management in Linux

More from Konstantin Kovshenin (8)

PDF
Scaling out with WordPress
PDF
Будущее WordPress (SPB, 2014)
PDF
Road to Platform @ WordPress.com VIP Workshop 2014
PDF
Будущее WordPress
PDF
Templating WordPress
PDF
Как повысить скорость вашего сайта на WordPress
PDF
Things I Wish I Had Known Before Developing a WordPress Theme
PDF
7 Tips for Better WordPress Theme Development
Scaling out with WordPress
Будущее WordPress (SPB, 2014)
Road to Platform @ WordPress.com VIP Workshop 2014
Будущее WordPress
Templating WordPress
Как повысить скорость вашего сайта на WordPress
Things I Wish I Had Known Before Developing a WordPress Theme
7 Tips for Better WordPress Theme Development

Recently uploaded (20)

PPTX
Funds Management Learning Material for Beg
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
The Internet -By the Numbers, Sri Lanka Edition
PPTX
QR Codes Qr codecodecodecodecocodedecodecode
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
PPTX
innovation process that make everything different.pptx
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PPTX
Internet___Basics___Styled_ presentation
PPTX
international classification of diseases ICD-10 review PPT.pptx
PPTX
presentation_pfe-universite-molay-seltan.pptx
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PPT
tcp ip networks nd ip layering assotred slides
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PPTX
SAP Ariba Sourcing PPT for learning material
PPTX
Digital Literacy And Online Safety on internet
PDF
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
Funds Management Learning Material for Beg
Design_with_Watersergyerge45hrbgre4top (1).ppt
The Internet -By the Numbers, Sri Lanka Edition
QR Codes Qr codecodecodecodecocodedecodecode
RPKI Status Update, presented by Makito Lay at IDNOG 10
innovation process that make everything different.pptx
introduction about ICD -10 & ICD-11 ppt.pptx
522797556-Unit-2-Temperature-measurement-1-1.pptx
Internet___Basics___Styled_ presentation
international classification of diseases ICD-10 review PPT.pptx
presentation_pfe-universite-molay-seltan.pptx
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
Tenda Login Guide: Access Your Router in 5 Easy Steps
tcp ip networks nd ip layering assotred slides
Unit-1 introduction to cyber security discuss about how to secure a system
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
SAP Ariba Sourcing PPT for learning material
Digital Literacy And Online Safety on internet
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
Decoding a Decade: 10 Years of Applied CTI Discipline

Memory Management in WordPress