Jon Fabian
Jon Fabian

Reputation: 286

php exit() function results to memory leaks

I am using a page for ajax requests and inside it i add exit(); function to stop loading the template, since it is an ajax request.

The problem is everytime i add the exit() the memory is not freed. i checked the memory like this:

//mypage.php
echo memory_get_usage(); exit(); // This results to memory to climb up for every page refresh.
===============================
//mypage.php
echo memory_get_usage(); // The memory is ok

//For clarification the above codes are executed at different times.

What could be the problem? How can i free the memory on exit? Help please Thanks!

Upvotes: 3

Views: 696

Answers (4)

Jon Fabian
Jon Fabian

Reputation: 286

Guys Thank you for the help, i truly appreciate it. Im lucky i found the issue. It is probably the render() method of my Template Class which contains the ob_start(). So when the exit() function is called in mypage.php(which is included in render()). the output buffer maybe exhausted. Im not sure this could be the problem but i was able to solve the issue by changing the sequence of executing the template->render();

Thanks guys!!

Upvotes: 0

Mike
Mike

Reputation: 24363

The only way I was able to reproduce your error is by storing a value in $_SESSION and doubling its length every time. Here is my code:

<?php

session_start();

if (!isset($_SESSION['test'])) {
    $_SESSION['test'] = 'abcdefghij';
}
else {
    $_SESSION['test'] .= $_SESSION['test'];
}
$_SESSION['memory'][] = memory_get_usage();

print_r($_SESSION['memory']);
exit;

After refreshing the page 24 times, I finally get it to run out of memory:

Array
(
    [0] => 231768
    [1] => 232088
    [2] => 232248
    [3] => 232424
    [4] => 232640
    [5] => 232728
    [6] => 233200
    [7] => 233984
    [8] => 235376
    [9] => 238136
    [10] => 243392
    [11] => 253768
    [12] => 274384
    [13] => 315480
    [14] => 397536
    [15] => 561512
    [16] => 889328
    [17] => 1544952
    [18] => 2855808
    [19] => 5477384
    [20] => 10720400
    [21] => 21206296
    [22] => 42177952
    [23] => 84121128
)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 83886344 bytes) in Unknown on line 0

You can see that the memory usage increases every time the page is refreshed.

When you tell PHP to exit, it doesn't execute anything further in your code. My guess is that somewhere after you do exit, you also do something like:

$_SESSION['test'] = 'something else';

When you exit the page, this won't get executed and therefore throws it into an exponentially incrementing loop, increasing the memory usage every time. If you can't find anything like this, I would recommend looking into Xdebug to get a better picture of what's happening.

Upvotes: 1

James C
James C

Reputation: 14149

I think you're mistaken about what is causing the memory leak. If you try to modify your code to the below then you'll see that nothing runs after the exit call.

echo memory_get_usage(); exit(); // This results to memory to climb up for every page refresh.

echo "I will never echo". PHP_EOL;
echo memory_get_usage(); // The memory is ok

To debug this properly I'd recommend including some extra debug context to all of your inline debugging like this:

echo "before database call: ". memory_get_usage() . PHP_EOL;

and then from them work out where the memory usage starts climbing

Upvotes: 0

ZorleQ
ZorleQ

Reputation: 1427

This seems to be a known aspect of PHP and according to people with definitely much more knowledge than me, it is kind of intentional. Someone had an exactly the same question (http://comments.gmane.org/gmane.comp.php.devel/77918) and it turns out they do it on purpose:

We do have some intentional leaks where we rely on the pool allocator to wipe things at the end of a request. So I am less concerned about things you see at request termination than I would be if you found one mid-request.

Hope this shines some light on your question.

Upvotes: 2

Related Questions