• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

PHP class memory allocation question

If I instantiate a class in a loop, am I creating a memory leak?  If so, how do I "unset" a class?

Example:

$i = 10;
while($i > 0)
{
$aClass = new Class;
{do some stuff}
$i--;
}

Thanks,
ibanja
0
ibanja
Asked:
ibanja
  • 2
  • 2
1 Solution
 
PromethylCommented:
$aClass = new Class;

It appears here to me you are destroying the old class by removing it from the $aClass variable, and then reassigning it to the class.

http://us4.php.net/manual/en/function.memory-get-usage.php

Test it out.

Example:

$i = 10;
while($i > 0) {
   $aClass = new Class;
   //{do some stuff}
   $i--;
   echo memory_get_usage().'. ';
}
0
 
ibanjaAuthor Commented:
That worked.  Thanks  Promethyl. Thanks for pointing that function out. Below is the test I ran.


$i = 0;
while($i < 5)
{
  echo "Memory: ";
  echo memory_get_usage();
  echo "<br>\n";

  $nameVars = new PassStack();
  $nameVars->push("lastname", "lastname");
  $nameVars->push("firstname", "firstname");
  $nameVars->push("member", "member");
  $i++;
}

  echo "Memory: ";
  echo memory_get_usage();
  echo "<br>\n";

Produced the following:
 
 Memory Test
Memory: 137864
Memory: 138408
Memory: 138520
Memory: 138520
Memory: 138520
Memory: 138520
0
 
PromethylCommented:
So we've determined, 4 out of the 5 runs the memory stays the same, so there is not a leak in class instantiation that we can tell from this test, correct?

0
 
ibanjaAuthor Commented:
That's what I am going to say.

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now