Approximating PHP variable sizes in memory

Post

You ever become curious about how much memory your fancy variable is using in your php script? Although there is no direct function to tell you the usage of a specific variable, you can infer an approximation using memory_get_usage().

It is simple, call memory_get_usage() once and store the result. Set your variable up and then call memory_get_usage() again and get the difference. There are many factors that affect memory though so beware this is just an approximation.

$startmem = memory_get_usage();
$var = "Variable";
echo memory_get_usage() - $startmem;

Comments

Leave a comment

November 2026
Sun
Mon
Tue
Wed
Thu
Fri
Sat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Share