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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:50:18+00:00 2026-05-18T11:50:18+00:00

I have yet to find an elegant solution for this. I have a class

  • 0

I have yet to find an elegant solution for this. I have a class with a method I want to track the memory usage of without modifying the function:

class Example
{
    public function hello($name)
    {
        $something = str_repeat($name, pow(1024, 2));
    }
}

$class = new Example;
$class->hello('a');

So the task is, how much memory does hello() use without interferring with it?

Note: The memory usage of this method should be 1MB. I’ve tried wrapping the call with memory_get_usage(); to no avail:

class Example
{
    public function hello($name)
    {
        $something = str_repeat($name, pow(1024, 2));
    }
}

$class = new Example;

$s = memory_get_usage();

$class->hello('a');

echo memory_get_usage() - $s;

This only results in 144 bytes (Not correct at all). I’ve tried various magic with Reflection by using the ReflectionMethod class.

I have a feeling what I need to do is calculate the difference in the method :(. If someone can think of anything cleaner then you’d really make my day.

Edit: I should mention this is in the context of a benchmarking application. So while memory_get_peak_usage works in the sense that it correctly returns the memory usage, it will also skew benchmarks ran after a high memory method. Now if there was a way to reset the memory stats, that could be good also.

  • 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-18T11:50:19+00:00Added an answer on May 18, 2026 at 11:50 am

    You could use register_tick_function and just dump memeory_get_usage out every tick (line) and analysis it later. The class below could be improved by using debug_backtrace to find line number related to memory usage or adding time per line using microtime.

    Profiler class

    class Profiler
    {
    
        private $_data_array = array();
    
        function __construct()
        {
            register_tick_function( array( $this, "tick" ) );
            declare(ticks = 1);
        }
    
        function __destruct()
        {
            unregister_tick_function( array( $this, "tick" ) );
        }
    
        function tick()
        {
            $this->_data_array[] = array(
                "memory" => memory_get_usage(),
                "time" => microtime( TRUE ),
                //if you need a backtrace you can uncomment this next line
                //"backtrace" => debug_backtrace( FALSE ),
            );
        }
    
        function getDataArray()
        {
            return $this->_data_array;
        }
    }
    

    Example

    class Example
    {
        public function hello($name)
        {
            $something = str_repeat($name, pow(1024, 2));
        }
    }
    
    $profiler = new Profiler(); //starts logging when created
    
    $class = new Example;
    $class->hello('a');
    
    $data_array = $profiler->getDataArray();
    
    unset( $profiler ); //stops logging when __destruct is called
    
    print_r( $data_array );
    

    Output

    Array (
        [0] => Array (
                [memory] => 638088
                [time] => 1290788749.72
            )
        [1] => Array (
                [memory] => 638896
                [time] => 1290788749.72
            )
        [2] => Array (
                [memory] => 639536
                [time] => 1290788749.72
            )
        [3] => Array (
                [memory] => 640480
                [time] => 1290788749.72
            )
        [4] => Array (
                [memory] => 1689800 // <~ money!
                [time] => 1290788749.72
            )
        [5] => Array (
                [memory] => 641664
                [time] => 1290788749.72
            )
    )
    

    Possible Issue

    Since this profiler class stores the data in PHP, the overall memory usage will increase artificially. One way to sidestep this issue would be to write the data out to a file as you go (serialized), and when your done you can read it back.

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

Sidebar

Related Questions

I have looked around and have yet to find a solution to this problem.
I've looked around, and I'm trying to find an elegant solution to this, and
I have yet to find a good reference on this topic. For this example
This has been bugging me for a while and have yet to find an
I've looked around for answers to this but have yet to find one that
I want to make colored dendrograms and have yet to find a sufficient library:
I've looked all over and have yet to find a single solution to address
I've heard bits and pieces here and there, but I have yet find a
Business Objects Web Services returns error codes and I have yet to find a
I've been thinking about this problem for a while and have yet to come

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.