Wednesday 1 February 2017

An Unexpected Memory Bug I encountered

Programming is a very funny craft - errors pop up every where every time when they feel like.

So, I was working on one of my usual long term apps, I then tried logging in to the admin area of the web app, and I got a blank page in return. I checked the source of the page, and only about 3-4 lines of javascript was displayed. The error must have been just before immediately after this line (I whispered to myself).

(I think I should mention at this point that, the app is made using PHP [codeigniter], Javascript [JQuery] and the usual HTML, CSS, Bootstrap)

I delved into the code to unravel our bug, but then, there was no clue as to what could be wrong. "This code was still working last night, and I'm pretty sure no body else had access to the code", I mumbled to myself.

Okay, I had an idea, let me turn on error reporting (humm, on a production app), I did turn on error reporting and got a little clue.

Undefined property on object title... blah blah blah, in file .....php 
That appeared on about 20 lines, (Okay, this would imply the property is being accessed in a loop, I said). I went into the said file, nothing seemed wrong. This is getting more serious than I thought.



I went to check CodeIgniter error logs, and then I found this

PHP Fatal error:  Allowed memory size of 268435456 bytes exhausted (tried to allocate 9865442 bytes) in dashboard_view.php on line 78

I still appear to be clueless, you can't blame me, can you? I have never come across this type of error.
Then I went to the "developer's bible" (StackOverflow) , this was where I saw that the most likely culprit is that a code is trying to retrieve a large data from the database at a go.
Delving back into the code again, I found out there was indeed a particular statement retrieving all records from a database without limits.
Yes, this code worked fine when I had just about 2,000 rows in the table, but now, I have about 50,000 rows and fetching them all into memory all at once is nothing wise.

Now, lesson learnt.

0 comments:

Post a Comment

Say something...