With MALLOC_DECOMMIT defined, jemalloc will decommit memory it doesn't think it needs. When DECOMMIT is not defined, jemalloc madvise(MADV_FREE / MADV_DONTNEED)'s memory it doesn't need. AFAICT, a page is MADV_FREE'd iff it would be decommitted, were DECOMMIT defined. For the purposes of jemalloc_stats.committed, a page is "committed" even after it's been MADV_FREE'd. But OS issues aside (I understand this may not be the case on Mac), a MADV_FREE'd page should be quickly returned to the operating system. So I propose modifying jemalloc to report a new field, call it jemalloc_stats.live, which is equal to jemalloc_stats.committed when DECOMMIT is defined, and, when DECOMMIT is not defined, is equal to what jemalloc_stats.committed would be if DECOMMIT were defined. That is, it treats MADV_FREE'd memory as "not live". I think this should be pretty simple to do, because the machinery is already there to support DECOMMIT. jemalloc_stats.live would allow us to measure how much of the live heap is fragmented. Right now, we can only do this meaningfully on Windows. Bug 674290 is an alternate way to get a similar result, assuming that jemalloc makes the vast majority of anonymous mappings. But I think it would be good to measure this directly in jemalloc. This work would of course be overwritten if and when we update to the new version of jemalloc.
This is important because it lets us compute how much fragmentation there is in jemalloc on *nix. Currently, the only way I know of to compute the amount of wasted memory in jemalloc is to compute resident/anonymous - (heap-allocated + js-gc-heap). This gives you the amount of resident memory that isn't due to live heap allocations or the js gc heap. This value includes all of the heap fragmentation, but it may include other anonymous mappings. We can't really tell right now.
Created attachment 565638 [details] [diff] [review] Patch v1 Note the dependency on bug 683597.
I wonder if we can test this somehow, or if we need to resort to prayer.