My PHP is leaking memory and I have no idea why

Hi guys,

I’m doing some Wordpress development at the moment, and generally I know my way around enough to hack things together with PHP, wit a lot of misses, some luck, a lot of tutorials, and a lot of searching for things. Clearly not now: something I’ve written seems to be leaking a significant amount of memory, such that my 8GB RAM on my local development setup is exhausted within ten seconds of launching my Wordpress homepage locally.

I’m using a plugin called ‘Types’, which essentially is a custom fields plugin. The field I’ve implemented is just a text box, but it can be repeated, so I’ve tried to hack the php file so that it outputs “and” as the separator if there are two values, and commas between items if there are more than two fields (except for the last separator which should be “and”).

This is how I’ve done that, the line beginning with “$output = join” being the one that does the magic:

  if ( !empty( $output ) && isset( $params['separator2'] )
                        && $params['separator2'] !== '' ) {
                   $output = join(' <em>and</em> ', array_filter(array_merge(array(join(', ', array_slice($output, 0, -1))), array_slice($output, -1))));
                } else if ( !empty( $output ) ) {
                    $output = implode( ' ', $output );
                } else {
                    return '';
                }
            } else {
                // Make sure indexed right
                $_index = 0;
                foreach ( $meta as $temp_key => $temp_value ) {
                    if ( $_index == $index ) {
                        $params['field_value'] = $temp_value;
                        return types_render_field_single( $field, $params,
                                        $content, $code, $temp_key );
                    }
                    $_index++;
                }
                // If missed index
                return '';
            }
            $html = $output;
        } else {
            return '';
        }
    } else {

Would there be anything in there that could be causing memory leaks? Is there circular referencing? (Sorry, I’m clueless.)

Secondly, I’ve set up a thing so that I take the input of the field, make it lowercase, remove accents (if there are accents on the characters), remove spaces if there are, and tagged the input onto a URL. Then, I’ve asked PHP to check the headers of that URL to see if the page exists. If the page exists, it should wrap the URL of that page around the output in the post.

The code for that is below:

$ddee = 'http://localhost/wp/people/';

                           $collapse = strtolower(str_replace( array(' ','à','á','â','ã','ä', 'ç', 'è','é','ê','ë', 'ì','í','î','ï', 'ñ', 'ò','ó','ô','õ','ö', 'ù','ú','û','ü', 'ý','ÿ', 'À','Á','Â','Ã','Ä', 'Ç', 'È','É','Ê','Ë', 'Ì','Í','Î','Ï', 'Ñ', 'Ò','Ó','Ô','Õ','Ö', 'Ù','Ú','Û','Ü', 'Ý'), array('','a','a','a','a','a', 'c', 'e','e','e','e', 'i','i','i','i', 'n', 'o','o','o','o','o', 'u','u','u','u', 'y','y', 'A','A','A','A','A', 'C', 'E','E','E','E', 'I','I','I','I', 'N', 'O','O','O','O','O', 'U','U','U','U', 'Y'), $temp_output));


                      $urlarray = array($ddee,$collapse);




                      $url = implode("",$urlarray);




                           $file_headers = @get_headers($url);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
}


if($exists == false){
	
	$finalarray = array($temp_output);

	
}

if($exists == true){

$finalarray = array('<a href="', $url, '">',$temp_output,'</a>');



}







                    if ( !empty( $temp_output ) ) {


                   $booom = implode("",$finalarray);



                        $output[] = $booom;
                    }
                }
                

Again, is there anything which could be causing memory leaking here? Anything I’ve done drastically wrong? I have a feeling it is in the above code if there is anything.

This is how the PHP is included in the actual template file (you will notice that the code in the first block above causes it to ignore the separator inserted in this code below):

<?php

$writer_name = types_render_field("writer-name", array("raw"=>"true","separator2"=>"and"));

//Output the trainer email

echo $writer_name;
?></p>

I am hoping I have made a very obvious faux pas here and that the problem is easily fixable. If not, what other issues could be causing this? Is it correct to check the headers like I’m doing to see if the page exists?

I’ve also attached the full PHP file just in case the issue isn’t in what I’ve put about or indeed you need to see the full thing working together.

Any suggestions and pointers would be extremely appreciated. Thanks so much!

Ed

it may be here,


                foreach ( $meta as $temp_key => $temp_value ) { 
// add this
echo '<br>'.$temp_key.' => '.$temp_value;
                    if ( $_index == $index ) { 
                        $params['field_value'] = $temp_value; 
                        return types_render_field_single( $field, $params, 
                                        $content, $code, $temp_key ); 
                    } 


or before foreach.


echo count($meta);
exit();

You might have too much $meta going on.

PHP’s memory_limit would not let you consume anywhere near that much memory. Something else is amiss. What is your local development setup? You did not say. That is important information.

If you want to test your script, you might want to try placing the memory_get_usage() function within your code such as within your loop and echo out the value so you can see what is going on. If you need more, perhaps this may be helpful. Or not.

http://www.php.net/manual/en/intro.memtrack.php

It looks like the script is there to handle file uploads, how many files are being uploaded at a time and what’s the file sizes?