Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4615638
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:52:07+00:00 2026-05-22T01:52:07+00:00

I was wondering when does PHP free the memory which is used for a

  • 0

I was wondering when does PHP free the memory which is used for a variables

for example

function foo(){
  $foo = 'data';
  return $foo;  // <- is the memory space for `$foo` emptied at this point?
}

is it slower than:

function foo(){
  return 'data'; 
}

?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:52:07+00:00Added an answer on May 22, 2026 at 1:52 am

    Well, let’s find out!

    <?php
    $checkpoints = array( 'start' => memory_get_usage() );
    
    $checkpoints['before defining demo1'] = memory_get_usage();
    function demo1() { $foo = 'data'; return $foo; }
    $checkpoints['after defining demo1'] = memory_get_usage();
    
    $checkpoints['before defining demo2'] = memory_get_usage();
    function demo2() { return 'data'; }
    $checkpoints['after defining demo2'] = memory_get_usage();
    
    
    $checkpoints['before calling demo1'] = memory_get_usage();
    demo1();
    $checkpoints['after calling demo1'] = memory_get_usage();
    
    $checkpoints['before calling demo2'] = memory_get_usage();
    demo2();
    $checkpoints['after calling demo2'] = memory_get_usage();
    
    $checkpoints['before calling demo1 with storage'] = memory_get_usage();
    $storage1 = demo1();
    $checkpoints['after calling demo1 with storage'] = memory_get_usage();
    
    $checkpoints['before calling demo2 with storage'] = memory_get_usage();
    $storage2 = demo2();
    $checkpoints['after calling demo2 with storage'] = memory_get_usage();
    
    echo '<pre>';
    print_r($checkpoints);
    
    $last_key = 'start';
    foreach($checkpoints as $key => $value) {
        echo "{$key} - {$last_key} = ", ($value - $checkpoints[$last_key]), "\n";
        $last_key = $key;
    }
    

    Under PHP 5.3.6, my output is:

    Array
    (
        [start] => 321920
        [before defining demo1] => 322188
        [after defining demo1] => 322788
        [before defining demo2] => 322880
        [after defining demo2] => 323188
        [before calling demo1] => 323280
        [after calling demo1] => 323368
        [before calling demo2] => 323464
        [after calling demo2] => 323552
        [before calling demo1 with storage] => 323692
        [after calling demo1 with storage] => 323896
        [before calling demo2 with storage] => 324000
        [after calling demo2 with storage] => 324204
    )
    

    and then

    start - start = 0
    before defining demo1 - start = 268
    after defining demo1 - before defining demo1 = 600
    before defining demo2 - after defining demo1 = 92
    after defining demo2 - before defining demo2 = 308
    before calling demo1 - after defining demo2 = 92
    after calling demo1 - before calling demo1 = 88
    before calling demo2 - after calling demo1 = 96
    after calling demo2 - before calling demo2 = 88
    before calling demo1 with storage - after calling demo2 = 140
    after calling demo1 with storage - before calling demo1 with storage = 204
    before calling demo2 with storage - after calling demo1 with storage = 104
    after calling demo2 with storage - before calling demo2 with storage = 204
    

    It’s very likely that the memory increase during the initial calls to demo1 and demo2 that discard the output is due to the creation of variables to store memory use.

    However, the bottom line here is the two storage examples, where both returning data directly and assigning it to a variable before returning it resulted in the same exact memory use for the given data.

    Conclusion: PHP seems smart enough in this simple test to not needlessly copy string variables — though do keep an eye on the memory use difference between the two functions. Just declaring the demo1 function took more memory that declaring demo2. A few hundred bytes, really.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just wondering, does anyone know, whether uniqid function in PHP has a certain
Has anyone used Versions for OSX? I'm mainly wondering if it does everything TortoiseSVN
I am wondering if anyone has used or read about PHP scripts compiled as
I'm creating a PHP website which involves users signing up, and I'm wondering about
I am working on a CRON job that invokes a PHP script which does
Wondering why php keeps telling me a file doesnt exist when it does. this
I realize that PHP does not support unsigned integers, but I'm wondering how to
I'm basically wondering if Python has any OOP shortcomings like PHP does. PHP has
Does anyone here know when PHP executes include and include_once calls? I'm wondering because
hi people i am new at php and wondering what does int(4) mean in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.