Taking out the trash – unset and wp_cache_flush to free up memory

I’ve been working on a third party API plugin for WordPress that requires a script that auto updates from the API. It consists of pulling a big list of items from the API and checking them against the local site entries, often adding new items or updating existing entries.
The problem was with a number of memory allocation errors with PHP as the script went on, often about half way through the execution. Memory exhausted and Fatal Allowed memory issues popping up. A memory limit change would fix the problem to a point, it would still leave issues though – to work through the full list of items would need a very high PHP memory limit and also what happens if the list gets bigger in future? When 64Mb stops being enough do we simply up it to 128Mb what about when that isn’t enough do we carry on upping it? That still leaves a period where errors are being thrown and updates are missed until it is picked up and the memory limit is upped.

I made sure I called wordpress without themes to reduce a little bit of load and a quick check through to make sure unset() was being used as it should be – I’ll admit my code discipline has been lax in the past with regards to this but as ever we are always learning and making sure that we can improve our techniques. Unsetting things properly to free up the memory is one of those things I now make sure I’m doing rather than just overwriting old variables.
So the script was unsetting as it should and with diligent debugging with php memory usage outputs it became apparent that it was around WordPress functions that the memory usage was going up and not being unset, gradually leading to the errors. Plenty of looking around and eventually I stumbled upon wp_cache_flush and it worked a treat – every time I went around a processing loop or finished a process I called the cache flush and it removed all the stuff WordPress had been storing. I’m sure this wouldn’t be the best fix all for every situation but it’s certainly something to be well aware of.

Where to keep up with the web and continue learning

In my last proper post I introduced a couple of resources I recommend to those getting started on the web.
But where do you go from there to keep up to date and continue learning? Apart from browsing these hallowed pages of course.

Here are a few of the resources I regularly use to keep track and reference, I have plenty more in my RSS reader (Google reader! I’ll be looking for an update) but these are the ones I regularly end up visiting and reading from. They offer a variety of levels of interest and difficulties.

Continue reading

Getting started learning on the web

How do you get started with web design and development? It’s something I get asked a bit, often by people not necessarily looking at a career but looking to get to grips with the online world a bit better and start to do things for themselves.
There are two resources I point them at every time – Don’t fear the internet and Treehouse. So here’s a quick summary of what they are and why I recommend them.
Continue reading

A quick CSS based “Back to top” link

You see these in plenty of places, especially with popular single page sites. A page where the menu is at the top in the header and as you scroll down a small “Back to top” link appears that whizzes you back to the top of the page and therefore back to the menu. Here’s a quick way I do it just using a bit of CSS, it might not work in every circumstance but it’s easy to do and can be modified quite easily. Continue reading

Coding that makes you say WTF!

Every so often you pick up a project that has been coded by somebody else and encounter something and you can’t help but wonder what the f**k they were thinking, often it’s down to naivety or a lack of knowledge, or even a use of old outdated coding methods but then sometimes it’s just mind boggling bizarre.

Here’s a few examples I’ve come across recently but I’m sure there’s plenty of you with more to share. Continue reading