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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:35:14+00:00 2026-05-12T06:35:14+00:00

In hopes of trying to avoid future memory leaks in php programs (drupal modules,

  • 0

In hopes of trying to avoid future memory leaks in php programs (drupal modules, etc.) I’ve been messing around with simple php scripts that leak memory.

Could a php expert help me find what about this script causes the memory usage to continually climb?

Try running it yourself, changing various parameters. The results are interesting. Here it is:

<?php

function memstat() {
  print "current memory usage: ". memory_get_usage() . "\n";
}

function waste_lots_of_memory($iters) {
  $i = 0;
  $object = new StdClass;
  for (;$i < $iters; $i++) {
    $object->{"member_" . $i} = array("blah blah blha" => 12345);
    $object->{"membersonly_" . $i} = new StdClass;
    $object->{"onlymember"} = array("blah blah blha" => 12345);
  }
  unset($object);
}

function waste_a_little_less_memory($iters) {
  $i = 0;
  $object = new StdClass;
  for (;$i < $iters; $i++) {

    $object->{"member_" . $i} = array("blah blah blha" => 12345);
    $object->{"membersonly_" . $i} = new StdClass;
    $object->{"onlymember"} = array("blah blah blha" => 12345);

    unset($object->{"membersonly_". $i});
    unset($object->{"member_" . $i});
    unset($object->{"onlymember"});

  }
  unset($object);
}

memstat();

waste_a_little_less_memory(1000000);

memstat();

waste_lots_of_memory(10000);

memstat();

For me, the output is:

current memory usage: 73308
current memory usage: 74996
current memory usage: 506676

[edited to unset more object members]

  • 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-12T06:35:14+00:00Added an answer on May 12, 2026 at 6:35 am

    unset() doesn’t free the memory used by a variable. The memory is freed when the “garbage collector” (in quotes since PHP didn’t have a real garbage collector before version 5.3.0, just a memory free routine which worked mostly on primitives) sees fit.

    Also, technically, you shouldn’t need to call unset() since the $object variable is limited to the scope of your function.

    Here is a script to demonstrate the difference. I modified your memstat() function to show the memory difference since the last call.

    <?php
    function memdiff() {
        static $int = null;
    
        $current = memory_get_usage();
    
        if ($int === null) {
            $int = $current;
        } else {
            print ($current - $int) . "\n";
            $int = $current;
        }
    }
    
    function object_no_unset($iters) {
        $i = 0;
        $object = new StdClass;
    
        for (;$i < $iters; $i++) {
            $object->{"member_" . $i}= array("blah blah blha" => 12345);
            $object->{"membersonly_" . $i}= new StdClass;
            $object->{"onlymember"}= array("blah blah blha" => 12345);
        }
    }
    
    function object_parent_unset($iters) {
        $i = 0;
        $object = new StdClass;
    
        for (;$i < $iters; $i++) {
            $object->{"member_" . $i}= array("blah blah blha" => 12345);
            $object->{"membersonly_" . $i}= new StdClass;
            $object->{"onlymember"}= array("blah blah blha" => 12345);
        }
    
        unset ($object);
    }
    
    function object_item_unset($iters) {
        $i = 0;
        $object = new StdClass;
    
        for (;$i < $iters; $i++) {
    
            $object->{"member_" . $i}= array("blah blah blha" => 12345);
            $object->{"membersonly_" . $i}= new StdClass;
            $object->{"onlymember"}= array("blah blah blha" => 12345);
    
            unset ($object->{"membersonly_" . $i});
            unset ($object->{"member_" . $i});
            unset ($object->{"onlymember"});
        }
        unset ($object);
    }
    
    function array_no_unset($iters) {
        $i = 0;
        $object = array();
    
        for (;$i < $iters; $i++) {
            $object["member_" . $i] = array("blah blah blha" => 12345);
            $object["membersonly_" . $i] = new StdClass;
            $object["onlymember"] = array("blah blah blha" => 12345);
        }
    }
    
    function array_parent_unset($iters) {
        $i = 0;
        $object = array();
    
        for (;$i < $iters; $i++) {
            $object["member_" . $i] = array("blah blah blha" => 12345);
            $object["membersonly_" . $i] = new StdClass;
            $object["onlymember"] = array("blah blah blha" => 12345);
        }
        unset ($object);
    }
    
    function array_item_unset($iters) {
        $i = 0;
        $object = array();
    
        for (;$i < $iters; $i++) {
            $object["member_" . $i] = array("blah blah blha" => 12345);
            $object["membersonly_" . $i] = new StdClass;
            $object["onlymember"] = array("blah blah blha" => 12345);
    
            unset ($object["membersonly_" . $i]);
            unset ($object["member_" . $i]);
            unset ($object["onlymember"]);
        }
        unset ($object);
    }
    
    $iterations = 100000;
    
    memdiff(); // Get initial memory usage
    
    object_item_unset ($iterations);
    memdiff();
    
    object_parent_unset ($iterations);
    memdiff();
    
    object_no_unset ($iterations);
    memdiff();
    
    array_item_unset ($iterations);
    memdiff();
    
    array_parent_unset ($iterations);
    memdiff();
    
    array_no_unset ($iterations);
    memdiff();
    ?>
    

    If you are using objects, make sure the classes implements __unset() in order to allow unset() to properly clear resources. Try to avoid as much as possible the use of variable structure classes such as stdClass or assigning values to members which are not located in your class template as memory assigned to those are usually not cleared properly.

    PHP 5.3.0 and up has a better garbage collector but it is disabled by default. To enable it, you must call gc_enable() once.

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

Sidebar

Ask A Question

Stats

  • Questions 200k
  • Answers 200k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer object.class.to_s.tableize May 12, 2026 at 7:57 pm
  • Editorial Team
    Editorial Team added an answer This worked for me: #zd-nav .zd-sub-nav li{ float:left; clear:left; position:relative;… May 12, 2026 at 7:57 pm
  • Editorial Team
    Editorial Team added an answer Use String.IndexOf http://msdn.microsoft.com/en-us/library/system.string.indexof.aspx May 12, 2026 at 7:57 pm

Related Questions

Well, I sort of want to have sort of a faint striped pattern across
I'm trying to create a multiline textarea in a php page, and I want
In order to cleanly port my game to the iPhone, I'm trying to make
I'm starting to step into unfamiliar territory with regards to performance improvement and our

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.