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

  • Home
  • SEARCH
  • 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 8400415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:33:21+00:00 2026-06-09T21:33:21+00:00

As my understanding , when i passed array by value , a copy of

  • 0

As my understanding , when i passed array by value , a copy of array is created.
i.e in below program $y & $z should need same memory as of $x. however memory utilization hardly increases.
Obviousy my understanding is wrong , Can anyone explain the reason.

for($i=0;$i<1000000;$i++)

        $x[] = $i; // memory usage : 76519792


echo memory_get_usage(); 

function abc($y){

    $y[1] = 1; //memory usage  : 76519948 
    $z[]= $y;   //memory usage : 76520308

}
  • 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-06-09T21:33:22+00:00Added an answer on June 9, 2026 at 9:33 pm

    I heard that php uses copy-on-write:
    http://en.wikipedia.org/wiki/Copy-on-write

    as an example:

    <?
    for($i=0;$i<100000;$i++)
        $x[] = $i;
    
    // we output the memory use:
    echo memory_get_usage().'<br/>';  // outputs 14521040
    
    // here we equate $y to $x, but instead of creating a copy, 
    // php engine just creates a pointer to the same memory space
    $y = $x;
    
    echo memory_get_usage().'<br/>';  // outputs 14521128
    
    // here we change something in y, now php engine 
    // "creates a seperate copy" for y and makes the change
    $y[1]=8;
    
    echo memory_get_usage().'<br/>';  // outputs 23569904
    
    ?>
    

    and similar behaviour with the function calls:

    <?
    for($i=0;$i<100000;$i++)
        $x[] = $i;
    
    echo memory_get_usage().'<br/>'; /* 14524968 */
    
    function abc($y){
        echo memory_get_usage().'<br/>'; /* 14524968 */
        $y[1] = 1;
        echo memory_get_usage().'<br/>'; /* 23573752 */
        $z[]= $y;  
        echo memory_get_usage().'<br/>'; /* 23574040 */
    
    }
    abc($x);
    echo memory_get_usage().'<br/>'; /* 14524968 */
    ?>
    

    PS: I am testing this on windows, maybe it is different on linux

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

Sidebar

Related Questions

function heaviside(&$value, $key, &$array) { if($key > 0) $value = $array[$key-1].$array[$key]; } function test_heaviside()
Understanding that I should probably just dig into the source to come up with
My understanding is that when using && as an mathematical operation in Java, if
My understanding is that Java's implementation of regular expressions is based on Perl's. However,
My understanding of C# says (thanks to Jeff Richter & Jon Skeet) that assignment
I have a problem understanding how the parameter passed to the drawInRect method is
I have some trouble understanding the need for std::result_of in C++0x. If I understood
I am new to c# and need help understanding what going on in the
I have difficulty understanding how a javascript callback should work. I am using the
I need a little help understanding how HTML forms work. It is my understanding

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.