Smush Pro memory allocation issue


I’d like to report a potential problem with the WP Smush Pro plugin that appears after a large number of images are smushed. This is limited to the admin panel only. WordPress has a caching mechanism to store post meta within meta.php that keeps around any post meta in case it will be requested again on that page load. The function that stores the meta data is called “update_meta_cache()”.

With a large number of images it appears that Smush Pro attempts to regenerate statistics about how many images have been smushed and how much space is saved. Each smushed image has a post meta attached to it “wp-smpro-smush-data” that contains a serialized array with the data in it. Once this post meta is loaded WordPress will cache it.

If you have a large number of images (like 40.000) this causes a lot of memory being allocated and eventual out of memory error depending on your php.ini setting. My test run with the 40.000 images does that. I’ve logged what happens in update_meta_cache() and I can see it issuing a new WP_Query doing a “SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (…list of 40000 post ids…:wink:”. Before the query is fired the memory is at 158774 k and after query it is at 479532 k. Shortly after the we run out of memory in a loop populating the cache with memory limit at 512M.

Due to the nature of how this works the problem will only get worse as you go along smushing more images. Perhaps tracking the saved space / number of images needs a more efficient way. I have not looked inside the plugin exactly where this happens, but I suggest something that bypasses the post meta caching and if the saved bytes are necessary have that counted in chunks and avoid recounts as much as possible.