1
1
# php-memprof
2
2
3
- ![ Supported PHP versions: 7.x] ( https://img.shields.io/badge/php-7.x-blue.svg )
3
+ ![ Supported PHP versions: 7.0 ... 8. x] ( https://img.shields.io/badge/php-7.0%20...%208 .x-blue.svg )
4
4
5
- php-memprof profiles memory usage of PHP scripts, and especially can tell which
6
- function has allocated every single byte of memory currently allocated.
5
+ php-memprof is a memory profiler for PHP that can be used to detect memory leaks.
7
6
8
- This is different from measuring the memory usage before and after a
9
- function call:
10
-
11
- ``` php
12
- <?php
13
-
14
- // script 1
15
-
16
- function a() {
17
- $data = file_get_contents("huge-file");
18
- }
19
-
20
- a();
21
-
22
- $profile = memprof_dump_array();
23
-
24
- ```
25
-
26
- In script 1, a before/after approach would designate file_get_contents() as huge
27
- memory consumer, while the memory it allocates is actually freed quickly after
28
- it returns. When dumping the memory usage after a() returns, the memprof
29
- approach would show that file_get_contents() is a small memory consumer since
30
- the memory it allocated has been freed at the time memprof_dump_array() is
31
- called.
32
-
33
-
34
- ``` php
35
- <?php
36
-
37
- // script 2
38
-
39
- function a() {
40
- global $cache;
41
- $cache = file_get_contents("huge-file");
42
- }
43
-
44
- a();
45
-
46
- $profile = memprof_dump_array();
47
- ```
48
-
49
- In script 2, the allocated memory remains allocated after file_get_contents()
50
- and a() return, and when memprof_dump_array() is called. This time a() and
51
- file_get_contents() are shown as huge memory consumers.
52
-
53
- ## How it works
54
-
55
- See [ INTERNALS.md] [ 7 ]
7
+ ## Install
56
8
57
- ## Dependencies
9
+ ### Dependencies
58
10
59
- * [ Judy Library ] [ 3 ] (e.g. libjudy-dev or judy package)
60
- * C Library with [ malloc hooks ] [ 1 ] (optional; allows to track persistent allocations too)
11
+ php-memprof depends on libjudy. On Debian or Ubuntu, the dependency can be
12
+ installed with:
61
13
62
- ## Install
14
+ # install libjudy dependency:
15
+ apt instal libjudy-dev
63
16
64
17
### Using PECL
65
18
19
+ Make sure to install dependencies, and then:
20
+
66
21
pecl install memprof
67
22
68
23
### Manually
69
24
25
+ Make sure to install dependencies, and then:
26
+
70
27
Download the source and run the following commands in the source directory:
71
28
72
29
phpize
@@ -86,44 +43,47 @@ Or permanently, in php.ini:
86
43
87
44
## Usage
88
45
89
- Memprof can be enabled during script execution by calling `` memprof_enable() `` .
46
+ Profiling is enabled at request startup when one of these is true:
47
+
48
+ * The environment variable ` MEMPROF_PROFILE ` is non-empty
49
+ * ` $_GET["MEMPROF_PROFILE"] ` is non-empty
50
+ * ` $_POST["MEMPROF_PROFILE"] ` is non-empty
90
51
91
- Then the memory usage can be dumped by calling one of the `` memprof_dump_ ``
92
- functions. Both tell which functions allocated all the currently allocated
93
- memory.
52
+ Once profiling is enabled, the program must call `` memprof_dump_callgrind `` or
53
+ one it its variants".
94
54
95
55
Example:
96
56
97
57
```
98
- <?php
99
-
100
- if (function_exists('memprof_enable')) {
101
- memprof_enable();
102
- }
58
+ <?php // test.php
103
59
104
60
do_some_work();
105
61
106
- if (function_exists('memprof_enable' )) {
62
+ if (function_exists('memprof_enabled') && memprof_enabled( )) {
107
63
memprof_dump_callgrind(fopen("/tmp/callgrind.out", "w"));
108
64
}
109
65
```
110
66
111
- ### memprof_enabled()
67
+ ```
68
+ MEMPROF_PROFILE=1 php test.php
69
+ ```
112
70
113
- Returns whether memprof is enabled.
71
+ Or:
114
72
115
- ### memprof_enable()
73
+ ```
74
+ curl http://127.0.0.1/test.php?MEMPROF_PROFILE=1
75
+ ```
116
76
117
- Enables memprof and start tracking memory allocations. Note: any memory
118
- allocation made before this call is ignored .
77
+ Whem using `` memprof_dump_callgrind `` , the profile can be visualized with
78
+ Kcachegrind or Qcachegrind (see bellow) .
119
79
120
- ### memprof_disable ()
80
+ ### memprof_enabled ()
121
81
122
- Disables memprof and forget previous allocations .
82
+ Returns whether memprof is enabled .
123
83
124
84
### memprof_dump_callgrind(resource $stream)
125
85
126
- The memprof_dump_callgrind function dumps the current memory usage to a stream
86
+ The memprof_dump_callgrind function dumps the current profile to a stream
127
87
in callgrind format. The file can then be read with tools such as
128
88
[ KCacheGrind] [ 2 ] or [ QCacheGrind] [ 6 ] .
129
89
@@ -251,18 +211,17 @@ Example output:
251
211
)
252
212
)
253
213
254
- ## PHP 7
214
+ ## PHP 7, PHP 8
255
215
256
- The current branch supports PHP 7 only .
216
+ The current branch supports PHP 7 and PHP 8 .
257
217
258
218
## PHP 5
259
219
260
220
The php5 branch supports PHP 5.
261
221
262
- ## Todo
222
+ ## How it works
263
223
264
- * Support for tracking persistent (non-zend-alloc) allocations when libc
265
- doesn't have malloc hooks
224
+ See [ INTERNALS.md] [ 7 ]
266
225
267
226
[ 1 ] : https://www.gnu.org/software/libc/manual/html_node/Hooks-for-Malloc.html#Hooks-for-Malloc
268
227
[ 2 ] : https://kcachegrind.github.io/html/Home.html
@@ -271,4 +230,3 @@ The php5 branch supports PHP 5.
271
230
[ 5 ] : https://github.com/gperftools/gperftools
272
231
[ 6 ] : https://www.google.com/search?q=qcachegrind
273
232
[ 7 ] : https://github.com/arnaud-lb/php-memory-profiler/blob/master/INTERNALS.md
274
-
0 commit comments