You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
php-memprof is a memory profiler for PHP that can be used to detect memory leaks.
5
+
php-memprof is a fast and accurate memory profiling extension for PHP that can be used to detect memory leaks.
6
+
7
+
## Features
8
+
9
+
The extension tracks the allocation and release of memory blocks to report the amount of memory leaked by every function, method, or file in a program.
10
+
11
+
* Reports non-freed memory at arbitrary points in the program
12
+
* Dumps profile in callgrind, pprof, or raw array formats
13
+
* Can track memory allocated by PHP itself as well as native malloc
14
+
15
+
### How does it differ from Blackfire ?
16
+
17
+
Accuracy: Blackfire tracks how functions affect the total memory usage (memory_usage_after - memory_usage_before, for each function call). Memprof tracks every allocated memory block in the program, and reports what has not been freed at an arbitrary point in the program. In effect, the difference between these two strategies is that functions that allocate small amounts of memory but whose allocations are retained may not be visible in Blackfire, whereas functions that allocate big chunks of memory that are freed by the caller will look like big memory consumers although they are not really.
18
+
19
+
Native malloc: Memprof can track the memory allocated with malloc(), in addition to PHP's own allocator.
6
20
7
21
## Install
8
22
@@ -41,20 +55,37 @@ Or permanently, in php.ini:
41
55
42
56
extension=memprof.so
43
57
44
-
The extension has no overhead when not profiling, so it can be enabled by default on dev environments.
58
+
The extension has no overhead when not profiling, so it can be loaded by default on dev environments.
45
59
46
60
## Usage
47
61
62
+
Using the extension is done in three steps:
63
+
64
+
### 1. Enabling profile
65
+
48
66
Profiling is enabled at request startup when one of these is true:
49
67
50
68
* The environment variable `MEMPROF_PROFILE` is non-empty
51
69
*`$_GET["MEMPROF_PROFILE"]` is non-empty
52
70
*`$_POST["MEMPROF_PROFILE"]` is non-empty
53
71
54
-
Once profiling is enabled, the program must call ``memprof_dump_callgrind`` or
55
-
one it its variants".
72
+
### 2. Dumping the profile
73
+
74
+
Once profiling is enabled, the program must call ``memprof_dump_callgrind()`` or
75
+
one it its variants to dump the memory profile.
76
+
77
+
This can be done at anytime during the program, ideally when the leak is large,
78
+
so that it will be more visible in the profile.
56
79
57
-
Example:
80
+
This can be done multiple times during the same execution, if necessary.
81
+
82
+
### 3. Visualizing the profile
83
+
84
+
The profile can be visualised with Kcachegrind, Qcachegrind, Google Perftools,
85
+
or with custom tools. See the documentation of ``the memprof_dump_callgrind()``
86
+
and variants.
87
+
88
+
### Usage example
58
89
59
90
```
60
91
<?php // test.php
@@ -66,27 +97,33 @@ if (function_exists('memprof_enabled') && memprof_enabled()) {
66
97
}
67
98
```
68
99
100
+
When ran on the command line, profiling can be enabled by setting the `MEMPROF_PROFILE` environment variable:
101
+
69
102
```
70
103
MEMPROF_PROFILE=1 php test.php
71
104
```
72
105
73
-
Or:
106
+
When ran in a web context, profiling can be enabled by setting the `MEMPROF_PROFILE` query string parameter or POST field:
74
107
75
108
```
76
109
curl http://127.0.0.1/test.php?MEMPROF_PROFILE=1
77
110
```
78
111
79
-
Whem using ``memprof_dump_callgrind``, the profile can be visualized with
0 commit comments