![]() |
Memory Management with Declare
I'm sure it's happened with the best of us. A script suddenly gets stuck in a loop and saps all the memory from the server. However, aside from programmer-induced loops, memory errors can also be a good way for a hacker to gain insightful information in to the way your application is setup.
Not only that, but it'd be really nice to monitor how much memory your scripts are consuming, would it not? This is where declare steps into the door way and makes its presence felt. The declare construct allows you to execute a function every so many ticks. A tick is loosely defined as every line a low level action takes place. Thus when a mathematical equation takes place, or a value being assigned to a variable - even a curly brace to end if or while statements is considered a tick. A simple tick can be setup like so: PHP Code:
Putting it to some use The usefulness of declare may not be apparent immediately. However, it has some nice advantages when you wish to monitor something. Such as in our case, memory management. There are some other purposes I've thought of (or, in admission, read about):
Let's take a look at our memory management function for the declare construct: PHP Code:
We then specify our tick function as well as our declare construct for the chunk of code we wish to monitor: PHP Code:
Any programming outside of the declare block will not count towards our memory management handling. Thus to make the declare global we would use the declare construct like so: PHP Code:
PHP Code:
PHP Code:
Performance Concerns and Closure Just remember that calling this function inside a declare is a nice addition, but keep in mind the performance issues that may arise if your tick function contains server intensive actions. The declare function can be exceedingly powerful for absolutely all sorts of various things. Your task for today? Dream up some weird and wonderful uses and get back to us! |
Great article. I imagine it'll come in handy for some of the complex scripts I write. Cheers.
|
very useful code, thanks
|
| All times are GMT. The time now is 03:33 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0