Skip to content

Commit e1318f5

Browse files
committed
Refresh README.md
1 parent 0158724 commit e1318f5

File tree

1 file changed

+37
-79
lines changed

1 file changed

+37
-79
lines changed

README.md

Lines changed: 37 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,29 @@
11
# php-memprof
22

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)
44

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

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
568

57-
## Dependencies
9+
### Dependencies
5810

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:
6113

62-
## Install
14+
# install libjudy dependency:
15+
apt instal libjudy-dev
6316

6417
### Using PECL
6518

19+
Make sure to install dependencies, and then:
20+
6621
pecl install memprof
6722

6823
### Manually
6924

25+
Make sure to install dependencies, and then:
26+
7027
Download the source and run the following commands in the source directory:
7128

7229
phpize
@@ -86,44 +43,47 @@ Or permanently, in php.ini:
8643

8744
## Usage
8845

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
9051

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".
9454

9555
Example:
9656

9757
```
98-
<?php
99-
100-
if (function_exists('memprof_enable')) {
101-
memprof_enable();
102-
}
58+
<?php // test.php
10359
10460
do_some_work();
10561
106-
if (function_exists('memprof_enable')) {
62+
if (function_exists('memprof_enabled') && memprof_enabled()) {
10763
memprof_dump_callgrind(fopen("/tmp/callgrind.out", "w"));
10864
}
10965
```
11066

111-
### memprof_enabled()
67+
```
68+
MEMPROF_PROFILE=1 php test.php
69+
```
11270

113-
Returns whether memprof is enabled.
71+
Or:
11472

115-
### memprof_enable()
73+
```
74+
curl http://127.0.0.1/test.php?MEMPROF_PROFILE=1
75+
```
11676

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).
11979

120-
### memprof_disable()
80+
### memprof_enabled()
12181

122-
Disables memprof and forget previous allocations.
82+
Returns whether memprof is enabled.
12383

12484
### memprof_dump_callgrind(resource $stream)
12585

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
12787
in callgrind format. The file can then be read with tools such as
12888
[KCacheGrind][2] or [QCacheGrind][6].
12989

@@ -251,18 +211,17 @@ Example output:
251211
)
252212
)
253213

254-
## PHP 7
214+
## PHP 7, PHP 8
255215

256-
The current branch supports PHP 7 only.
216+
The current branch supports PHP 7 and PHP 8.
257217

258218
## PHP 5
259219

260220
The php5 branch supports PHP 5.
261221

262-
## Todo
222+
## How it works
263223

264-
* Support for tracking persistent (non-zend-alloc) allocations when libc
265-
doesn't have malloc hooks
224+
See [INTERNALS.md][7]
266225

267226
[1]: https://www.gnu.org/software/libc/manual/html_node/Hooks-for-Malloc.html#Hooks-for-Malloc
268227
[2]: https://kcachegrind.github.io/html/Home.html
@@ -271,4 +230,3 @@ The php5 branch supports PHP 5.
271230
[5]: https://github.com/gperftools/gperftools
272231
[6]: https://www.google.com/search?q=qcachegrind
273232
[7]: https://github.com/arnaud-lb/php-memory-profiler/blob/master/INTERNALS.md
274-

0 commit comments

Comments
 (0)