Question

Variables typically get destroyed and the memory they've occupied is freed as soon as the garbage collector deems it necessary. Basically, pretty soon after the currently executed script is finished, yeah?

Now, I'm interested in the lifetime of Ajax retrieved variables.

Since variables are typically purged after the script they're running in is finished, that is, on page reload, does Ajax fall into this category as well? If I have a script that receives a fat JSON array from PHP, and clear only the JS variable with "arrayVar = null" after I'm done using it, is that enough? Or should I unset the PHP variables which created the returned array as well? For example:

// PHP CODE
<?
public function some_kind_of_ajax_called_action() {

    $someClass = new MyClass();
    $someArray = $someClass->getRequestedData();
    $arrayForReturning = array();

    foreach ($someArray as $element) {
        ($currentUser == "admin")?($arrayForReturning[] = $element):null;
    }

    die(json_encode($arrayForReturning));
}
?>

Would it be better to include...

unset($someClass);
unset($someArray);

...before the die() call? Would this conserve memory, regardless of how little?

Cheers

Was it helpful?

Solution

PHPs part is done the moment it has served your AJAX request and not until page reload. It doesn't matter whether you hold it in a JS variable. They aren't connected.

OTHER TIPS

I don't see a reason why this should be necessary, as an AJAX-request is not different from a usual HTTP-request, just that its response is handled differently by your browser.

Both those variables would unset at the script end, in the same way as any other page.

Incidentally, you could also use unset($someClass,$someArray) since it takes multiple inputs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top