[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP class memory allocation question

Posted on 2005-05-16
5
Medium Priority
?
223 Views
Last Modified: 2006-11-18
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
Comment
Question by:ibanja
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
Promethyl earned 750 total points
ID: 14012777
$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
 

Author Comment

by:ibanja
ID: 14015698
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
 
LVL 7

Expert Comment

by:Promethyl
ID: 14015726
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
 

Author Comment

by:ibanja
ID: 14017971
That's what I am going to say.

Thanks!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question