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 6204975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:11:25+00:00 2026-05-24T05:11:25+00:00

$myStr = ”; for ($i = 0; $i<999500; $i++) { $myStr .= chr(rand(0,127)); }

  • 0
$myStr = '';
for ($i = 0; $i<999500; $i++) {
    $myStr .= chr(rand(0,127));
}
echo round(memory_get_usage()/1024/1024,2) . ' Mb';

I got 1.27 Mb

$myStr = '';
for ($i = 0; $i<999500; $i++) {
    $myStr .= chr(rand(0,127));
}
$myStr2 = &$myStr;
echo round(memory_get_usage()/1024/1024,2) . ' Mb';

I save myStr to myStr2 By reference, and I got 1.27 Mb

$myStr = '';
for ($i = 0; $i<999500; $i++) {
    $myStr .= chr(rand(0,127));
}
$myStr2 = $myStr;
echo round(memory_get_usage()/1024/1024,2) . ' Mb';

$myStr2 = $myStr also I got 1.27 Mb. What’s going on? Why I got 1.27 Mb, instead 2.54 Mb?

  • 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-24T05:11:25+00:00Added an answer on May 24, 2026 at 5:11 am

    By assigning a variable to another variable in PHP, PHP will not duplicate the variable’s data immediatly for memory optimization reasons.

    Only at the time you actually change one of the two variables, PHP will duplicate the data and then only change one of the two variables data:

    $myStr2 = $myStr; # this won't copy the data.
    $myStr2 .= 'X'; # this will trigger copy on write.
    

    This optimzation is called “copy on write” (Wikipedia).

    If you want to get a full scientific read on the topic, please see:

    • Copy-on-Write in the PHP Language (Jan 18 2009; by Akihiko Tozawa, Michiaki Tatsubori, Tamiya Onodera and Yasuhiko Minamide; PDF file)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DllExport void LoadString(char *myStr) { cout << Before: << myStr << endl; LoadStringData(&myStr); cout
I use this regex to put thousand separators in a string: while matchstr(mystr, '\(\d\)\(\d\{3}\)\(\D\|\s\|$\)')
In .Net when handling numbers with unspecified locale I use: return double.Parse(myStr, CultureInfo.InvariantCulture.NumberFormat); Whats
I'm trying to parse apart a string like this with javascript: var mystr =
Suppose we have mystr = 'hello.py' mystr = 'hello' if not 'py' in mystr.rsplit('.')[1]:
If I have a string like: const char* mystr = Test Test Bla Bla
Would a regular expression be faster than the following Python code? myStr.replace(,, ).replace(', ).replace(-,).replace(_,).replace([,
I wanted to url encode a python string and got exceptions with hebrew strings.
We have a subclass of str (call it MyStr), and I need to be
Not familiar with c++, can someone help me add cmd to the myStr array

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.