Is there a way I get the size of a PHP variable in bytes?

前端 未结 4 1946
梦如初夏
梦如初夏 2021-01-02 00:41

I currently have a PHP CLI script using Zend Framework extensively which seems to be using a ever larger amount of memory as it runs. It loops through a large set of models

相关标签:
4条回答
  • 2021-01-02 01:07

    No. You are likely looking for memory that is not freed, e.g. you unlinked a variable or deleted a reference and the garbage collector did not yet release the associated block in memory.

    You could try Zend Server 5 (you need the commercial version though) to mem-profile your application. It has code tracing. I dont know if this would allow you to spot memory leaks though.

    Also see:

    • What's new in PHP V5.2, Part 1: Using the new memory manager
    • Memtrack (PECL)
    0 讨论(0)
  • 2021-01-02 01:10

    Here is a code snippet I found at weberdev

    <?php
    function array_size($a){
        $size = 0;
        while(list($k, $v) = each($a)){
            $size += is_array($v) ? array_size($v) : strlen($v);
        }
        return $size;
    }
    ?>
    

    It gets the size of the given array in bytes. Is this what you meant?

    0 讨论(0)
  • 2021-01-02 01:11

    i don't have a solution to check the size of every variable, but if you use doctrine its probably the reason

    you need to use

       $Elem->free(true);
    

    another thing is to upgrade to 5.3 (if you dont do it yet), the garbage collector of 5.3 is better

    0 讨论(0)
  • 2021-01-02 01:17

    I don't know how accurate it is, but I got a number by using apc_add('variable_name', $var);. I then go to my apc.php under user cache entries and look at the size column.

    Of course for this to work you need to have APC installed and running. :-)

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