pib icon indicating copy to clipboard operation
pib copied to clipboard

Memroy leak

Open oraoto opened this issue 6 years ago • 3 comments

How to reproduce:

Run this code about 31 times:

<?php

exit(0);

oraoto avatar Jun 04 '18 08:06 oraoto

From the memory trace, 2 big chunk of memroy is not free:

image

Some printf before mmap and munmap:

mmap addr: 6684672, size: 2097152
munmap addr: 6684672, size: 2097152
mmap addr: 6684672, size: 4190208
munmap addr: 6684672, size: 1703936
munmap addr: 10485760, size: 389120
shutdown_memory_manager
munmap addr: 8388608, size: 2097152

oraoto avatar Jun 04 '18 11:06 oraoto

Current workaround is to not use mmap/munmap: https://github.com/oraoto/pib/commit/ac79a58db95fa188f00139a50d5e43d009ae0170

oraoto avatar Jun 05 '18 07:06 oraoto

An alternative approach is replace the calls to replace php-src/Zend/zend_alloc.c to use mmap/munmap with stubs. https://github.com/TysonAndre/phan-demo/blob/79d6df9950789ebf54cf3c798f772df8a76e887e/zend_alloc.c#L423-L458 seems to work - large php applications run successfully and no memory leaks were seen

  • This pretends that attempts to shrink/expand mmaped blocks always fails. When something attempts to mmap an aligned piece of memory, it instead allocates a block large enough to contain an aligned piece of memory of the requested size, plus the pointer to the original malloced block (which may be wasteful - I don't know how small the alignment actually needs to be for every php api)

emscripten also has the problem that it doesn't support extending (or shrinking) existing blocks that were allocated with mmap

EDIT: never mind, it worked for a small script but still runs out of memory after 3 runs of a large php application

TysonAndre avatar Jul 23 '19 21:07 TysonAndre