2

Within WordPress, the WP_Query class can be called with certain arguments to retrieve posts (data) from your database. Unforunately the object itself is big and if it retrieves 300-400 posts, it gets hectic pretty fast because, assume you store this WP_Query and its newly found posts in an array, it gets big pretty fast, I can see that it eats up ~30-40mb of my memory on local host which is already a powerful machine, so I decided to test on my server machine, a bit lower memory usage since it's running Linux but still the same.

Now, imagine I had 800 posts. This is a very, very real and often met possibility, heck, I saw a lot of customer sites with more posts.

Obviously, my script will exceed, first, the execution time on a bad host and second, if it ever reaches here, memory exhaustion.

I kind of know how to solve it and had very good success but I went in totally blind, just looking at the differences between the task manager's memory consumption.

Are there no tools that map the memory consumption of a certain script together with its execution time? XDEBUG only shows execution time.

coolpasta
  • 725
  • 5
  • 19
  • 2
    http://php.net/manual/en/function.memory-get-usage.php – Jeff Dec 16 '18 at 23:39
  • @Jeff Believe me, I've tried and searched, I respect everyone's time so I do due dilligence before - but this never helped me that much, perhaps I was doing something wrong but that's another question. Are there no tools that maybe make use of this that allow encapsulation of my scripts to run them in a "tracked sandbox" which can output charts? – coolpasta Dec 16 '18 at 23:43
  • 2
    Possible duplicate of [Tracking Memory Usage in PHP](https://stackoverflow.com/questions/2290611/tracking-memory-usage-in-php) – miken32 Dec 17 '18 at 00:01

1 Answers1

1

To take a look in general, if I have access via SSH or something I usually us the top command (for linux boxes). BUt here is a list of all kinds of memory-checking related commands: https://www.cyberciti.biz/faq/linux-check-memory-usage/

Another thing I've used is this plugin: https://wordpress.org/plugins/wp-server-stats/ However, it will only tell you how much ram is available/used. It is not granular enough to tell you which script is using the memory.

And, finally, one of my favorite plugins for diagnosing query issues: https://wordpress.org/plugins/query-monitor/

Let me know if these don't provide specific enough details and we can dig into it more :)

Justin Waulters
  • 303
  • 2
  • 8
  • Also, if you want specific help about how to make your WordPress queries more efficient please let me know. – Justin Waulters Dec 17 '18 at 04:14
  • Query monitor has been of great help and really, XDEBUG helped me narrow down exactly what was problematic. I ran XDEBUG while trying to have a few hundreds "mock clients" browsing on local host to see exactly what chokes, but never saw ram usage, unfortunately your answer doesn't really help me as, for development, I'm running on Windows and can't have continuous deployment, I'll try to refer to some unrelated Windows diag tools. As for optimizing WP queries, sure, I'd love to read on anything about it. – coolpasta Dec 17 '18 at 09:44
  • What version of windows are you using, and what software are you using to run your localhost? As far as query efficiency is concerned, you may find this useful: https://codex.wordpress.org/Class_Reference/WP_Meta_Query Often times you can avoid having to grab a whole bunch of posts by making the query more specific. Having taxonomies set up is helpful too. – Justin Waulters Dec 19 '18 at 04:23
  • Both `WP_Query` and that are grossly underperforming when querying for a lot of posts. – coolpasta Dec 19 '18 at 13:58
  • There is a lot of data that is queried and returned when grabbing entire posts. If you absolutely need to return so much post data then it might be a good idea to do the query and store it in a transient or some other form of cache-like state so you don't need to keep running the query. Then you could update it in the background as needed. If you can provide some more information about your implementation maybe we can suggest a way to make things more performant. – Justin Waulters Dec 20 '18 at 00:45