Showing posts with label backend programming. Show all posts
Showing posts with label backend programming. Show all posts

Sunday, March 20, 2011

Dynamically Adjusting PHP Memory Limit During Runtime

Abstract

At PHP runtime, processes would typically require a variable size memory requirement based on data input and how the process manipulates the input. For a very predictable amount of input and resulting data, a fixed limit would often suffice. But what if you would encounter frequent and intermittent spikes from your predicted memory requirement? You would probably want the memory limit adjusted dynamically during the process runtime.

PHP has a static declaration of its memory limit per process. The default prior to PHP 5.2 was 8M and 16M all throughout 5.2. Now at 5.3 the default is 128M.This memory limit is the amount of memory that it could use in the duration of its runtime unless changed. In a per script basis, a developer can change the limit using ini_set function (i.e ini_set('memory_limit', '256M') ). You can write a function or method to frequently check for memory usage and use ini_set to adjust memory limit accordingly. However, you wouldn't want your code littered with some sort of check logic unnecessarily, hence the need for a better way of doing it.

Theory of Operation

Using PHP's declare construct, we can use the tick directive to call upon a handler function to adjust the memory of your running process / script dynamically without the hassle of littering code your code with such check logic. See below for a reference implementation. It's working but experimental as I haven't fully tested it for production use. On a side note, other than what is presented, the same implementation opens the door for event-driven software architecture in PHP programming.

Wednesday, October 20, 2010

Memory Leak Caused By Variable and String Concatenation On Long Running PHP Applications

This memory leak is observed if the following conditions are met:

1. A variable is being concatenated.
2. The data length buffered within the variable is more than 8-bytes, 4-bytes on class (not sure why).
3. Using double quotes to evaluate the variable(s) automatically.
4. Present on PHP 5.1.x below

Monday, May 3, 2010

Unyielding Long Running Loops or Infinite Loops are Not Good For Your Code

There would definitely be cases where there's a need for long running or infinite loops (in some programming terminologies called a noop) somewhere in your applications. Perhaps you need your application to wait for some external processing and thus, simply put in a loop to wait until it gets the expected result or status. Or perhaps simply, you are processing a long list of items within a stack or queue. Whatever the need might be, this area will rapidly eat your memory and CPU resources if you do not yield or release memory allocation every iteration. This is an area in your code that requires careful evaluation, should the need to yield in resources for other processes is required or not, in order for you to come-up with an application with better integrity and performance.

The concept behind this is that, you never let the process iterations stack up, clogging the call stack (which consumes all resources), and provide or yield adequate resources for other processes, both internal and external, to make sure that all necessary operations are working as expected.

Wednesday, April 28, 2010

Using PHP to Prompt Input Entry from the Command Line

The following snippet is a simple way to prompt a user with input request from the command line using PHP like the good old programming languages like C, PERL etc. This has been tested to work on Linux environment.

The concept behind this is to use the built-in OS standard input stream protocol of PHP and feed it through fopen. Then get the value using fgets (getting a line from the current file pointer) which is typically the latest input received within the PHP runtime. The application will pause / stall because it's expecting an input with the currently open input stream protocol which makes it similar to the prompt function or language construct by earlier programming languages.