WordPress Out OF Memory Exceptions

wordpress out of memory post thumbnail
Home » Blog » WordPress Out OF Memory Exceptions
Table of Contents
    Add a header to begin generating the table of contents

    Recently I’ve been dabbling in programmatic SEO. I got an idea of rolling out a landing page to attract search engine traffic from every major city in the United States and came up a with a solution to bulk create a lot of pages in WordPress, including a page for all 50 states in the U.S. and the top cities that met a certain population requirement in each state. All in all this wound up being around 500 pages worth of content.

    Unfortunately my WordPress instance’s ability to handle such volume exceed my ambitions and I quickly started noticing a lot of out of memory exceptions in WordPress, notably in any place where a list of pages needed to be loaded. I wound up dialing the number of pages back to 350 pages, but even that required some troubleshooting steps and changes to be able to publish that volume on top of the already robust set of landing pages that my site contained. In this post I’ll talk a little bit about WordPress out of memory exceptions that can start appearing on a site when it gets too large.

    What Is An Out Of Memory Exception In WordPress?

    Each time a visitor comes to your WordPress site a thread is allocated to handle delivering your site to that visitor. WordPress sites need a pool of threads to be able to handle multiple visitors potentially hitting the website at the same time. Each thread is going to have a certain maximum size typically expressed in Megabytes. The total amount of threads and their maximum sizes add up the total memory pool.

    Think about a thread like a hallway that only one person can walk through at a time. Think about that like how big the hallway. How big the hallway is dictates how big of a person could feasibly walk through it. As long as the hallways is big enough to accomodate the person walking through the hallway, everything works fine. But if someone big, HUGE, like Andre The Giant or The Rock tries to walk through a tiny hallway and can’t fit, well that’s basically like an out of memory exception in WordPress.

    When the total size of the request exceeds the maximum size of a thread, it results in an out of memory exception.

    Why Would Too Many Pages Create An Out Of Memory Exception In WordPress?

    I first noticed this error when looking at the Yoast sitemap after publishing the initial 500 pages of content, but was also noticing the out of memory exceptions happening when trying to load the all pages view and when trying to load the menu pages, which load a list of pages you can choose to add to the menu.

    I was a little agitated because loading a list of pages should be a relatively simple and small query to run that shouldn’t really be resulting in out of memory exceptions. But with WordPress the simple and easy comes at the beginning and the headaches pile up as you scale. A trade off most are willing to make with websites.

    My website is using a page builder, Beaver Builder to be more specific, and most page builders store all of the data they use to display a page within the post_meta table in WordPress. Unfortunately the database queries that were driving the display of these pages were querying data from the post_meta table along with the posts table.

    The post_meta table is a flexible table designed to store simple metadata about a post such as date published. It was never really intended to store massive columns of data such as a full page of HTML data but this is the solution many page builders leverage and since many WordPress sites leverage page builders, it’s a common issue to have the post_meta table be the culprit of WordPress out of memory exceptions.

    In general database queries are typically the cause of the out of memory exceptions since they are likely to load large chunks of data into memory as part of processing a request to deliver a page in WordPress.

    Troubleshooting WordPress Out Of Memory Exceptions

    Since database queries are the most likely culprit a good first troubleshooting step is to look at the largest tables in your database. You can identify the largest tables in your WordPress instance in the popular PHPMyAdmin by clicking on the database name and sorting by the size of the database table.

    As you can see the post Meta table, even after preening it down from what was originally over 1GB in size is the largest at over 500 MB. This is pretty common when troubleshooting out of memory exceptions in WordPress but it could also be other tables.

    If it is the post meta table as it was in this case you can use a SQL query like this (while substituting you table prefix) to identify what the largest post meta values are.

    SELECT length(meta_value), meta_key, meta_value FROM `wp_postmeta` GROUP BY meta_value ORDER BY length(meta_value) desc LIMIT 10;

    The length(meta_value) value is the length in bytes of the meta_value column. You can see in the screenshot below that there are post_meta rows where the meta value is 1127262 bytes or 1.12 Megabytes. This is the html data being stored by Beaver Builder for certain individual pages. Before cleanup there were duplicate values for drafts and revisions being stored in the database and with almost 600 total pages of content in the Tortoise and Hare website’s WordPress database and some over 1MB in size each it was easy to see how the table size could bloat.

    Fixing The WordPress Out Of Memory Exceptions On Certain Pages

    Fixing the out of memory exceptions took a couple of steps. First we expanded the amount of memory per thread via our hosting provider Kinsta. Even after we had dialed back the number of pages we were going to publish the post_meta table was large and the need to have an expanded thread memory size was required. We increased the total memory pool to 4GB with 4 threads at roughly 1GB each. Not the most elegant solution, but it worked.

    Second we cleaned up the post meta table as much as we could to reduce the amount of unnecessary large rows. In this case we ran a Beaver Builder database cleanup utility that allowed us to purge drafts and revisions. We also deleted some other unnecessary rows from other plugins storing some non critical metadata about certain pages. Overall this brought the total size of the down from roughly 1.3 GB (more than the maximum allowable thread size) down to about 500MB. This shaved the query size down below the maximum memory per thread.

    Pinpointing the specific cause of an out of memory error on WordPress can be challenging. It requires understanding of the database, ability to review error logs for clues, and understanding of the hosting configuration for thread counts and thread memory. Fixing the out of memory errors usually requires tweaks to one or more of these areas or a compromise on the plugins and features used by a WordPress instance.

    Help With Fixing An Out Of Memory Error On WordPress

    Troubleshooting out of memory exceptions can be a pain if you don’t know what you’re doing but they are a common error on WordPress site. Hopefully this post gave you some context that can help you troubleshoot your own out of memory errors. But, If you need helping fixing an out of memory error on WordPress, contact us and we can help you troubleshoot and repair your WordPress instance for an hourly fee.

    Share This Article
    Posted in:
    Tagged: ,

    Hunter Nelson

    Hunter is the founder and president of Tortoise and Hare Software, a digital marketing agency for the technology sector and other lead generation oriented businesses. Hunter has more than 10 years’ experience building web applications and crafting digital strategies for companies ranging from scrappy startups to Fortune 50 household names. When not on the clock, you'll find him spending time with his family and pups, relaxing on the beach, or playing competitive online video games. See for more.

    Leave a Comment