How can I free up the memory used by update_post_meta?

I’ve noticed that each call to update_post_meta results in a small increase in my memory usage.

Normally, the increase is small enough to not matter. But I’m writing a CSV import function that calls it 10,000+ times, which eventually results in an out-of-memory error.

Here’s a minimal code example:

function update_business($myData)
error_log(“Used memory 1: ” . memory_get_usage(false));
error_log(“Allocated memory 1: ” . memory_get_usage(true));
update_post_meta(‘13663’, ‘business_id’, $myData);
error_log(“Used memory 2: ” . memory_get_usage(false));
error_log(“Allocated memory 2: ” . memory_get_usage(true));

This results in:

Used memory 1: 17995848
Used memory 2: 17996992

Used memory 1: 17996992
Used memory 2: 18027720

Used memory 1: 18027720
Used memory 2: 18058448

Used memory 1: 18058448
Used memory 2: 18089176

Used memory 1: 18089176
Used memory 2: 18119904

Without the update_post_meta statement, the memory usage stays the same throughout the entire loop (as expected).

But with the update_post_meta statement, the memory usage grows bigger and bigger and never gets freed.

I’ve tried a bunch of different techniques for clearing the memory, including:


But nothing seems to work.

I’ve looked at tons of search results and although quite a few people have had this problem, there aren’t many answers besides “raise your memory limit” or “do it a different way”, neither of which seems like the best solution.

Surely there’s some way to free up the memory? I assume that either WordPress, PHP, MySQL or something else is caching something that I don’t want it to, or that there’s a memory leak somewhere, but I can’t figure it out.

Read more here:: How can I free up the memory used by update_post_meta?

Leave a Reply

Your email address will not be published. Required fields are marked *