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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:00:52+00:00 2026-06-16T17:00:52+00:00

Not long ago I had a debates about assignment variables to itself after some

  • 0

Not long ago I had a debates about assignment variables to itself after some action on them.

Assign variable to itself [AI]:

echo memory_get_usage() . "\n"; // 645680
$repeatedString = str_repeat('Hello,', 10000);
echo memory_get_usage() . "\n"; // 705944, AI_delta1 = 60264
$repeatedString = explode(',', $repeatedString);
echo memory_get_usage() . "\n"; // 3337888, AI_delta2 = 2631944
echo memory_get_peak_usage() . "\n"; // AI_peak = 3401024

Assign variable to another variable [AAV]:

echo memory_get_usage() . "\n"; // 645752
$repeatedString = str_repeat('Hello,', 10000);
echo memory_get_usage() . "\n"; // 706024, AAV_delta1 = 60272
$explodedString = explode(',', $repeatedString);
echo memory_get_usage() . "\n"; // 3398256, AAV_delta2 = 2692232
echo memory_get_peak_usage() . "\n"; // AAV_peak = 3400984

I created tests of memory usage in both cases and subtract values:

AAV_delta1 - AI_delta = 8
AAV_delta2 - AI_delta2 = 60288
AAV_peak - AI_peak = -40

According to this results it doesn’t matter which approach to use, memory usage – same. It’s only a question should I use variable $repeatedString at my code below or not.

Am I right at my conclusions or my tests are not correct?

Why it happens this way?

Also a question: AAV_delta1 - AI_delta = 8, I expect it should be equal 0. Why it’s equals 8?

Note: Memory usage can vary on your system.

PHP Version: 5.3.5-1ubuntu7.11.

  • 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-16T17:00:53+00:00Added an answer on June 16, 2026 at 5:00 pm

    In AAV, there are two copies of the data, since each variable has to retain its data. They both have to persist until their respective variable is reassigned or goes out of scope.

    In AI, there are temporarily two copies of the data. Once the assignment takes place, the original value is no longer needed. However, the memory it uses won’t be recovered until a garbage collection takes place.

    If you check memory usage immediately after the assignment you won’t notice the difference, since there probably hasn’t been a GC yet. If you force a GC, you should see that AI uses less memory than AAV.

    You can also get the same memory use improvement in AAV by unsetting the original variable after the assignment (call this AAVU):

    $explodedString = explode(",", $repeatedString);
    unset($repeatedString); // or $repeatedString = false;
    

    As with AI, you will have to force a GC to notice the reduced memory use immediately.

    UPDATE:

    Because PHP uses reference counting for normal memory management, AI should reclaim the memory used by the string immediately. The string starts with a reference count of 1. When it gets passed to explode(), the refcount is increased to 2 (for the reference by explode’s parameter variable). When explode() returns, the parameter variable’s scope ends and the refcount returns to 1. Then when the assignment to $repeatedString takes place, the refcount drops to 0 and the string’s memory is reclaimed.

    gc_collect_cycles() is only needed to reclaim memory used by arrays and objects that are part of cycles. To see the impact of this, try the following code:

    gc_enable();
    echo "Begin: " .  memory_get_usage() . "\n";
    $array = array(str_repeat('Hello,', 10000));
    $array[] =& $array; // Create circular reference
    echo "After allocate: " . memory_get_usage() . "\n";
    unset($array);
    echo "After unset: " . memory_get_usage() . "\n";
    gc_collect_cycles();
    echo "After GC: " . memory_get_usage() . "\n";
    

    Results:

    Begin: 226088
    After allocate: 286640
    After unset: 286552
    After GC: 226088
    

    For details, see PHP Garbage Collection documentation

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

Sidebar

Related Questions

I posted a question not to long ago about how my program was essentially
I had this problem a long time ago and recently I got some clues
Not long ago I posted a question about a design decision of my F1
Not long ago, I wondered about the question: why are all processes killed when
I asked a question not long ago about the same batch of code, but
I just discovered PHP-ActiveRecord not too long ago after struggling for nearly a month
Not too long ago, I had a problem which required me to set WinDbg.exe
Not long ago developed a system based on compact framework for Windows Mobile Professional
Somebody asked similar question not long ago. But nobody answered comprehensively. Assume I have:
not so long ago i found out that you can do this in css

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.