Tracking Memory Usage in PHP

前端 未结 4 967
无人及你
无人及你 2020-11-29 02:15

I\'m trying to track the memory usage of a script that processes URLs. The basic idea is to check that there\'s a reasonable buffer before adding another URL to a cURL multi

相关标签:
4条回答
  • 2020-11-29 02:49

    real_usage works this way:

    Zend's memory manager does not use system malloc for every block it needs. Instead, it allocates a big block of system memory (in increments of 256K, can be changed by setting environment variable ZEND_MM_SEG_SIZE) and manages it internally. So, there are two kinds of memory usage:

    1. How much memory the engine took from the OS ("real usage")
    2. How much of this memory was actually used by the application ("internal usage")

    Either one of these can be returned by memory_get_usage(). Which one is more useful for you depends on what you are looking into. If you're looking into optimizing your code in specific parts, "internal" might be more useful for you. If you're tracking memory usage globally, "real" would be of more use. memory_limit limits the "real" number, so as soon as all blocks that are permitted by the limit are taken from the system and the memory manager can't allocate a requested block, there the allocation fails. Note that "internal" usage in this case might be less than the limit, but the allocation still could fail because of fragmentation.

    Also, if you are using some external memory tracking tool, you can set this environment variable USE_ZEND_ALLOC=0 which would disable the above mechanism and make the engine always use malloc(). This would have much worse performance but allows you to use malloc-tracking tools.

    See also an article about this memory manager, it has some code examples too.

    0 讨论(0)
  • 2020-11-29 03:00

    I also assume memory_get_usage() is safe but I guess you can compare both methods and decide for yourself, here is a function that parses the system calls:

    function Memory_Usage($decimals = 2)
    {
        $result = 0;
    
        if (function_exists('memory_get_usage'))
        {
            $result = memory_get_usage() / 1024;
        }
    
        else
        {
            if (function_exists('exec'))
            {
                $output = array();
    
                if (substr(strtoupper(PHP_OS), 0, 3) == 'WIN')
                {
                    exec('tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output);
    
                    $result = preg_replace('/[\D]/', '', $output[5]);
                }
    
                else
                {
                    exec('ps -eo%mem,rss,pid | grep ' . getmypid(), $output);
    
                    $output = explode('  ', $output[0]);
    
                    $result = $output[1];
                }
            }
        }
    
        return number_format(intval($result) / 1024, $decimals, '.', '');
    }
    
    0 讨论(0)
  • 2020-11-29 03:13

    Well I have never really had a memory problem with my PHP scripts so I do not think I could be of much help finding the cause of the problem but what I can recomend is that you get a PHP accelerator, you will notice a serious performance increase and memory usage with decline. Here is a list of accelerators and an article comparing a few of them (3x better performance with any of them)

    Wikipedia List

    Benchmark

    The benchmarks are 2 years old but you get the idea of the performance increases.

    If you have to you can also increase you memory limit in PHP if you are still having problems even with the accelerator. Open up your php.ini and find:

    memory_limit = 32M;
    

    and just increase it a little.

    0 讨论(0)
  • 2020-11-29 03:14

    Use xdebug, as it was recently (January of 29th) updated to now include memory profiling information. It keeps track of the function calls and how much memory they consume. This allows you to get very insightful view into your code and at the very least sets you in a direction of being aware of the problems.

    The documentation is helpful, but essentially you, install it enable the profiling xdebug.profiler_enable = 1 and give the output xdebug.profiler_output_dir=/some/path to a tool such as qcachegrind to do the heavy lifting, letting visually see it.

    0 讨论(0)
提交回复
热议问题