Hello thanks in advance for reading my question.

I use a lot of 1, 2 and 3d arrays in my script and I thought it would be nice for the server if I released the arrays after it's been used?

Here is my code to unset my arrays

FUNCTION unsetArr( $Arr1, $Arr2=FALSE, $Arr3=FALSE )
{
        FUNCTION _threeD( $Arg ){
            FOREACH( $Arg AS $Key => $Value ) {
                IF(!is_array($Value)) unset( $Arg[$Key] ); ELSE die('UnsetArr only accept 3D Arrays');
            }
        }
        FUNCTION _twoD( $Arg ){
            FOREACH( $Arg AS $Key => $Value ) {
               IF(!is_array($Value)) unset( $Arg[$Key] ); ELSE _threeD( $Value );
            }
        }
        FUNCTION _oneD( $Arg ){
            FOREACH( $Arg AS $Key => $Value ){
                IF(!is_array($Value)) unset( $Arg[$Key] ); ELSE _twoD( $Value );
            }
        }

    $Mem['Pre'] = memory_get_usage();

    IF( is_array( $Arr1 ) )
         _oneD( $Arr1 );

    IF( is_array( $Arr2 ) )
         _oneD( $Arr2 );

    IF( is_array( $Arr3 ) )
         _oneD( $Arr2 );

    $Mem['Post'] = memory_get_usage();

    RETURN $Mem;
}

Basically it allows me to unset single or multi dimensional arrays and up to 3 arrays at a time.

I thought that it would release memory back to the OS, but it seems to actually consume more memory?

The above when run outputs the following with print_r

Array ( [Pre] => 1619184 [Post] => 1627072 )

So how can my post be greater than my pre when I should have release memory back to the system by unsetting a large number of vars.

In this case I have a huge lang set and page array at the end of my script where I do

/*
 * Close Data Base Connection
 * CLOSE DB IF NOT USING PCONNECT USE PCON ON DETICATED SERVERS
 */
        IF ( !DB_PCON ) mysql_close( $MySQL->link );
        $memArr = unsetArr($PageArr, $Lang);
        print_r($memArr);
/*
 * Template Engine Page Output!
 */
        IF ( !strstr( $PageArr['te_tpl_body'], '_page_') ) {
             $TE->parse('HEAD', 'header');
             $TE->parse('BODY', 'body');
             $TE->parse('FOOT', 'footer');

         $TE->FastPrint('HEAD');
         $TE->FastPrint('BODY');
         $TE->FastPrint('FOOT');
    }ELSE{
         $TE->parse('PAGE', 'page');
         $TE->FastPrint('PAGE');
    }

    $TE->showDebugInfo( $ShowDebug );

    Maybe I'm missing something, but why not just do unset($array_name); instead of all that looping?

      I thought you had to unset each array element individually?

        Well unset works with just
        unset($array_name);

        However, when I have it in a function and in the calling page I print_r the array that was unset I find that the array is still there. I think that kinda sucks cause it means I have to unset my arrays in each page individually.

        Is there some other function that will allow me to clean up my memory by calling a function at the end of my scripts.

        I didn't want to have to do

        code code code on each page then do
        unset( $Lang );
        unset( $PageArr );
        unset( $SomeOtherArr );

        It seems it would be easier to do
        code code code
        unsetArr($Lang, $PageArr, $SomeOtherArr);

        But this doesn't see to work with unset because it only releases the memory in the function and not the page that called the function, if I read it correctly

          Remember that functions have their own, separate variable scope, and any variables passed to them are copies unless you pass them by reference. On top of that, unsetting a reference does not get rid of the original value, just the reference to it. Therefore, if you want to free up the memory via a function, the best I can thing of is:

          function freeArray(&$arr) // note the "&"
          {
             $arr = null;
          }
          

            Hey cool NogDog, that works very well!

            Just one more question

            If prior to using

            freeArray(&$arr) // note the "&"

            say I have several functions that use the large arrays but as globals
            such as

            function mystuff()
            {
            global $myarray
            
            do stuff
            }

            Then in my page I have

            $bla = mystuff();
            freeArray($myarray);
            

            Will this also free the memory from the global in mystuff function? I assume it does

              You could do that, or just cut out the middle-man and set it to null within the function where it is global. In fact, as a global, I think you could unset() it, too; though I'd have to test that to be sure.

              However, I prefer to avoid the use of "global" within a function as it tends to create tight coupling between the function and the application, which decreases reusability and also can create debugging headaches. Therefore my recommendation would be to pass it as a function parameter by reference, then use that freeArray() function or just directly set it to null in the original function.

                Write a Reply...