Skip to content

Commit a75bdef

Browse files
authored
Update README.md
1 parent dc06d40 commit a75bdef

File tree

1 file changed

+59
-23
lines changed

1 file changed

+59
-23
lines changed

README.md

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,21 @@
22

33
![Supported PHP versions: 7.0 ... 8.x](https://img.shields.io/badge/php-7.0%20...%208.x-blue.svg)
44

5-
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.
620

721
## Install
822

@@ -41,20 +55,37 @@ Or permanently, in php.ini:
4155

4256
extension=memprof.so
4357

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.
4559

4660
## Usage
4761

62+
Using the extension is done in three steps:
63+
64+
### 1. Enabling profile
65+
4866
Profiling is enabled at request startup when one of these is true:
4967

5068
* The environment variable `MEMPROF_PROFILE` is non-empty
5169
* `$_GET["MEMPROF_PROFILE"]` is non-empty
5270
* `$_POST["MEMPROF_PROFILE"]` is non-empty
5371

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.
5679

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
5889

5990
```
6091
<?php // test.php
@@ -66,27 +97,33 @@ if (function_exists('memprof_enabled') && memprof_enabled()) {
6697
}
6798
```
6899

100+
When ran on the command line, profiling can be enabled by setting the `MEMPROF_PROFILE` environment variable:
101+
69102
```
70103
MEMPROF_PROFILE=1 php test.php
71104
```
72105

73-
Or:
106+
When ran in a web context, profiling can be enabled by setting the `MEMPROF_PROFILE` query string parameter or POST field:
74107

75108
```
76109
curl http://127.0.0.1/test.php?MEMPROF_PROFILE=1
77110
```
78111

79-
Whem using ``memprof_dump_callgrind``, the profile can be visualized with
80-
Kcachegrind or Qcachegrind (see bellow).
112+
Setting a POST field works as well:
113+
114+
```
115+
curl -d MEMPROF_PROFILE=1 http://127.0.0.1/test.php
116+
```
117+
118+
## Functions documentation
81119

82120
### memprof_enabled()
83121

84-
Returns whether memprof is enabled.
122+
Returns whether memory profiling is currently enabled (see above).
85123

86124
### memprof_dump_callgrind(resource $stream)
87125

88-
The memprof_dump_callgrind function dumps the current profile to a stream
89-
in callgrind format. The file can then be read with tools such as
126+
Dumps the current profile in callgrind format. The result can be visualized with tools such as
90127
[KCacheGrind][2] or [QCacheGrind][6].
91128

92129
``` php
@@ -100,15 +137,14 @@ Here is a QcacheGrind screenshot:
100137

101138
### memprof_dump_pprof(resource $stream)
102139

103-
The memprof_dump_pprof function dumps the current memory usage to a stream in
104-
[pprof][4] format.
140+
Dumps the current profile in [pprof][4] format.
105141

106142
``` php
107143
<?php
108144
memprof_dump_pprof(fopen("profile.heap", "w"));
109145
```
110146

111-
The file can be visualized using [google-perftools][5]'s [``pprof``][4] tool.
147+
The file can be visualized with [google-perftools][5]'s [``pprof``][4] tool.
112148

113149
Display annotated call-graph in web browser or in ``gv``:
114150

@@ -118,7 +154,7 @@ $ # or:
118154
$ pprof --gv profile.heap
119155
```
120156

121-
![pprof call-graph screenshot](http://img707.imageshack.us/img707/7697/screenshot3go.png)
157+
![pprof call-graph screenshot](https://i.stack.imgur.com/EAnGC.png)
122158

123159
Output one line per function, sorted by own memory usage:
124160

@@ -128,17 +164,19 @@ $ pprof --text profile.heap
128164

129165
### memprof_dump_array()
130166

167+
Returns an array representing the current profile.
168+
131169
``` php
132170
<?php
133171
$dump = memprof_dump_array();
134172
```
135173

136-
The dump exposes the following information:
174+
The array exposes the following information:
137175

138-
* Inclusive and exclusive memory usage of functions (counting only the memory
139-
that has is still in use when memprof_dump_array is called)
140-
* Inclusive and exclusive blocks count of functions (number of allocated;
141-
counting only the blocks that are still in use when memprof_dump_array is
176+
* Inclusive and exclusive memory leaked by functions (counting only the memory
177+
that has is still not freed when memprof_dump_array is called)
178+
* Inclusive and exclusive blocks count of functions (number of allocations;
179+
counting only the blocks that are still not freed when memprof_dump_array is
142180
called)
143181
* The data is presented in call stacks. This way, if a function is called from
144182
multiple places, it is possible to see which call path caused it to leak the
@@ -213,12 +251,10 @@ Example output:
213251
)
214252
)
215253

216-
## PHP 7, PHP 8
254+
## PHP versions
217255

218256
The current branch supports PHP 7 and PHP 8.
219257

220-
## PHP 5
221-
222258
The php5 branch supports PHP 5.
223259

224260
## How it works

0 commit comments

Comments
 (0)