{"id":13700084,"url":"https://github.com/BitOne/php-meminfo","last_synced_at":"2025-05-04T18:34:11.869Z","repository":{"id":13943615,"uuid":"16643456","full_name":"BitOne/php-meminfo","owner":"BitOne","description":"PHP extension to get insight about memory usage","archived":false,"fork":false,"pushed_at":"2024-02-18T23:21:49.000Z","size":881,"stargazers_count":1106,"open_issues_count":40,"forks_count":81,"subscribers_count":31,"default_branch":"master","last_synced_at":"2025-04-08T14:11:07.126Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BitOne.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-02-08T13:11:01.000Z","updated_at":"2025-04-07T12:43:46.000Z","dependencies_parsed_at":"2024-10-29T18:24:16.764Z","dependency_job_id":null,"html_url":"https://github.com/BitOne/php-meminfo","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BitOne%2Fphp-meminfo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BitOne%2Fphp-meminfo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BitOne%2Fphp-meminfo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BitOne%2Fphp-meminfo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BitOne","download_url":"https://codeload.github.com/BitOne/php-meminfo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252382897,"owners_count":21739237,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T20:00:48.355Z","updated_at":"2025-05-04T18:34:09.141Z","avatar_url":"https://github.com/BitOne.png","language":"PHP","funding_links":[],"categories":["PHP","代码分析"],"sub_categories":[],"readme":"MEMINFO\n=======\nPHP Meminfo is a PHP extension that gives you insights on the PHP memory content.\n\nIts main goal is to help you understand memory leaks: by looking at data present in memory, you can better understand your application behaviour.\n\nOne of the main sources of inspiration for this tool is the Java jmap tool with the -histo option (see `man jmap`).\n\n![Build](https://github.com/BitOne/php-meminfo/workflows/Build%20the%20extension%20and%20test%20the%20analyzers/badge.svg)\n\nCompatibility\n-------------\nPHP 7.x, 8.0.\n\nFor older versions of PHP, you can use the following releases:\n - 5.6: PHP Meminfo v1.1\n - 5.5: PHP Meminfo v1.0.5 (may work with PHP 5.3 and PHP 5.4 but not tested)\n\nCompilation instructions\n------------------------\n## Compilation\nFrom the root of the `extension` directory:\n\n```bash\n$ phpize\n$ ./configure --enable-meminfo\n$ make\n$ make install\n```\n\n## Enabling the extension\nAdd the following line to your `php.ini`:\n\n```ini\nextension=meminfo.so\n```\n\nInstalling analyzers\n--------------------\nAnalyzers allow to analyze a memory dump (see below).\n\n```bash\n$ cd analyzer\n$ composer install\n```\n\nUsage\n-----\n## Dumping memory content\n\n```php\nmeminfo_dump(fopen('/tmp/my_dump_file.json', 'w'));\n\n```\n\nThis function generates a dump of the PHP memory in a JSON format. This dump can be later analyzed by the provided analyzers.\n\nThis function takes a stream handle as a parameter. It allows you to specify a file (ex `fopen('/tmp/file.txt', 'w')`, as well as to use standard output with the `php://stdout` stream.\n\n## Displaying a summary of items in memory\n```bash\n$ bin/analyzer summary \u003cdump-file\u003e\n\nArguments:\n  dump-file             PHP Meminfo Dump File in JSON format\n```\n\n### Example\n```bash\n$ bin/analyzer summary /tmp/my_dump_file.json\n+----------+-----------------+-----------------------------+\n| Type     | Instances Count | Cumulated Self Size (bytes) |\n+----------+-----------------+-----------------------------+\n| string   | 132             | 7079                        |\n| MyClassA | 100             | 7200                        |\n| array    | 10              | 720                         |\n| integer  | 5               | 80                          |\n| float    | 2               | 32                          |\n| null     | 1               | 16                          |\n+----------+-----------------+-----------------------------+\n```\n\n## Displaying a list of objects with the largest number of children\n```bash\n$ bin/analyzer top-children [options] [--] \u003cdump-file\u003e\n\nArguments:\n  dump-file             PHP Meminfo Dump File in JSON format\n\nOptions:\n  -l, --limit[=LIMIT]   limit [default: 5]\n```\n\n### Example\n```bash\n$ bin/analyzer top-children /tmp/my_dump_file.json\n+-----+----------------+----------+\n| Num | Item ids       | Children |\n+-----+----------------+----------+\n| 1   | 0x7ffff4e22fe0 | 1000000  |\n| 2   | 0x7fffe780e5c8 | 11606    |\n| 3   | 0x7fffe9714ef0 | 11602    |\n| 4   | 0x7fffeab63ca0 | 3605     |\n| 5   | 0x7fffd3161400 | 2400     |\n+-----+----------------+----------+\n\n```\n\n## Querying the memory dump to find specific objects\n```bash\n$ bin/analyzer query [options] [--] \u003cdump-file\u003e\n\nArguments:\n  dump-file              PHP Meminfo Dump File in JSON format\n\nOptions:\n  -f, --filters=FILTERS  Filter on an attribute. Operators: =, ~. Example: class~User (multiple values allowed)\n  -l, --limit=LIMIT      Number of results limit (default 10).\n  -v                     Increase the verbosity\n```\n\n### Example\n\n```bash\n$ bin/analyzer query -v -f \"class=MyClassA\" -f \"is_root=0\" /tmp/php_mem_dump.json\n+----------------+-------------------+------------------------------+\n| Item ids       | Item data         | Children                     |\n+----------------+-------------------+------------------------------+\n| 0x7f94a1877008 | Type: object      | myObjectName: 0x7f94a185cca0 |\n|                | Class: MyClassA   |                              |\n|                | Object Handle: 1  |                              |\n|                | Size: 72 B        |                              |\n|                | Is root: No       |                              |\n+----------------+-------------------+------------------------------+\n| 0x7f94a1877028 | Type: object      | myObjectName: 0x7f94a185cde0 |\n|                | Class: MyClassA   |                              |\n|                | Object Handle: 2  |                              |\n|                | Size: 72 B        |                              |\n|                | Is root: No       |                              |\n+----------------+-------------------+------------------------------+\n| 0x7f94a1877048 | Type: object      | myObjectName: 0x7f94a185cf20 |\n|                | Class: MyClassA   |                              |\n...\n\n```\n\n## Displaying the reference path\nThe reference path is the path between a specific item in memory (identified by its\npointer address) and all the intermediary items up to the one item that is attached\nto a variable still alive in the program.\n\nThis path shows which items are responsible for the memory leak of the specific item\nprovided.\n\n```bash\n$ bin/analyzer ref-path \u003citem-id\u003e \u003cdump-file\u003e\n\nArguments:\n  item-id               Item Id in 0xaaaaaaaa format\n  dump-file             PHP Meminfo Dump File in JSON format\n\nOptions:\n  -v                     Increase the verbosity\n```\n\n### Example\n\n```bash\n$ bin/analyzer ref-path -v 0x7f94a1877068 /tmp/php_mem_dump.json\nFound 1 paths\nPath from 0x7f94a1856260\n+--------------------+\n| Id: 0x7f94a1877068 |\n| Type: object       |\n| Class: MyClassA    |\n| Object Handle: 4   |\n| Size: 72 B         |\n| Is root: No        |\n| Children count: 1  |\n+--------------------+\n         ^\n         |\n         3\n         |\n         |\n+---------------------+\n| Id: 0x7f94a185cb60  |\n| Type: array         |\n| Size: 72 B          |\n| Is root: No         |\n| Children count: 100 |\n+---------------------+\n         ^\n         |\n    second level\n         |\n         |\n+--------------------+\n| Id: 0x7f94a185ca20 |\n| Type: array        |\n| Size: 72 B         |\n| Is root: No        |\n| Children count: 1  |\n+--------------------+\n         ^\n         |\n    first level\n         |\n         |\n+---------------------------+\n| Id: 0x7f94a1856260        |\n| Type: array               |\n| Size: 72 B                |\n| Is root: Yes              |\n| Execution Frame: \u003cGLOBAL\u003e |\n| Symbol Name: myRootArray  |\n| Children count: 1         |\n+---------------------------+\n```\n\nA workflow to find and understand memory leaks using PHP Meminfo\n----------------------------------------------------------------\n\n[Hunting down memory leaks](doc/hunting_down_memory_leaks.md)\n\nOther memory debugging tools for PHP\n-------------------------------------\n - XDebug (https://xdebug.org/)\nWith the trace feature and the memory delta option (tool see XDebug documentation), you can trace function memory usage. You can use the provided script to get an aggregated view (TODO link)\n\n - PHP Memprof (https://github.com/arnaud-lb/php-memory-profiler)\nProvides aggregated data about memory usage by functions. Far less resource intensive than a full trace from XDebug.\n\nTroubleshooting\n---------------\n## \"A lot of memory usage is reported by the `memory_usage` entry, but the cumulative size of the items in the summary is far lower than the memory usage\"\n\nA lot of memory is used internally by the Zend Engine itself to compile PHP files, to run the virtual machine, to execute the garbage collector, etc... Another part of the memory is usually taken by PHP extensions themselves. And the remaining memory usage comes from the PHP data structures from your program.\n\nIn some cases, several hundred megabytes can be used internally by some PHP extensions. Examples are the PDO extension and MySQLi extension.\nBy default, when executing a SQL query they will buffer all the results inside the PHP memory:\nhttp://php.net/manual/en/mysqlinfo.concepts.buffering.php\n\nIn case of very large number of results, this will consume a lot of memory, and this memory usage is not caused by the data you have in your objects or array manipulated by your program, but by the way the extension works.\n\nThis is only one example, but the same can happen with image manipulation extensions, that will use a lot of memory to transform images.\n\nAll the extensions are using the Zend Memory Manager, so that they will not exceed the maximum memory limit set for the PHP process. So their memory usage is included in the information provided by `memory_get_usage()`.\n\nBut PHP Meminfo is only able to get information on memory used by the data structure from the PHP program, not from the extensions themselves.\n\nHence the difference between those numbers, which can be quite big.\n\n## \"Call to undefined function\" when calling `meminfo_dump`\nThis means the extension is not enabled.\n\nCheck the PHP Info output and look for the MemInfo data.\n\nTo see the PHP Info output, just create a page calling the `phpinfo();` function, and load it from your browser, or call `php -i` from the command line.\n\n## Why most tests are \"skipped\"?\n\nWhile doing a `make test`, some tests will need JSON capabilities. But the\ncompilation system generates a clean env by removing all configuration\ndirectives that load extensions.\nSo if JSON capabilites are packaged as a separate extension (instead of\nbeing compiled directly in the PHP runtime), the tests will be skipped.\n\nYou may run them with the `run-tests.php` generated after the `make test`\ncommand, by providing the `php` executable:\n\n```bash\n$ TEST_PHP_EXECUTABLE=$(which php) $(which php) run-tests.php -d extension=$PWD/modules/meminfo.so\n\n```\nIn this case your tests will run with your local PHP configuration,\nincluding the loading of the JSON extension.\n\nPlease note this is not required when working with PHP 8 as the JSON functions are now\nusually complied in PHP directly.\n\nCredits\n-------\nThanks to Derick Rethans for his inspirational work on the essential XDebug. See http://www.xdebug.org/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBitOne%2Fphp-meminfo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FBitOne%2Fphp-meminfo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBitOne%2Fphp-meminfo/lists"}