tags:

views:

140

answers:

2

Is it as simple as calling memory_get_usage() at the start and end of a script and subtracting the 1st from the second value, to get the total memory used on that script? If so, how can I convert that value to a more understandable number like kb and mb?

A: 

you mean something like this , it use to nice format of file size but u can use it for your problem

function file_size($size)
{

    $filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
    return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) .$filesizename[$i] : '0 Bytes';
}
Haim Evgi
Why are you dividing by 1024 multiple times when you can just use the number in your if sentence? And its really KiB, MiB, GiB and TiB for binary (1024) and for decimal (1000) its kB (note lower k), MB, GB and TB.
OIS
i found some thing more simple i edit
Haim Evgi
+2  A: 

You may prefer to just call memory_get_peak_usage at the end of your script, which will return the highest total allocation during execution. This is more likely to be a useful figure - getting the start and end values doesn't account for memory allocated then deallocated during runtime.

Formatting this into a human readable number can be handled manually (just divide by 1024 then print as Kb), or with a class like NumberFormatter.

Adam Wright