mysql - PHP偷了我的内存

WBOY
Release: 2016-06-06 20:48:54
Original
1158 people have browsed it

<code>$m1 = memory_get_usage();
var_dump($m1);
$arr = array_fill(0, 100000, 'phpinfo');
unset($arr);

$m2 = memory_get_usage();
var_dump($m2);


var_dump("消耗内存:".($m2-$m1)/1000);
</code>
Copy after login
Copy after login

输出结果为:string(21) "消耗内存:64.688";

我很不解,我已经对大数组使用unset了,这占用的64.688K内存是从哪跑出来的,求解

回复内容:

<code>$m1 = memory_get_usage();
var_dump($m1);
$arr = array_fill(0, 100000, 'phpinfo');
unset($arr);

$m2 = memory_get_usage();
var_dump($m2);


var_dump("消耗内存:".($m2-$m1)/1000);
</code>
Copy after login
Copy after login

输出结果为:string(21) "消耗内存:64.688";

我很不解,我已经对大数组使用unset了,这占用的64.688K内存是从哪跑出来的,求解

>
http://www.laruence.com/2011/03/04/1894.html

PHP并不是简单的向OS要内存, 而是会像OS要一个大块的内存, 然后把其中的一块分配给申请者, 这样当再有逻辑来申请内存的时候,
就不再需要向OS申请内存了, 避免了频繁的系统调用.

在我们调用efree释放内存的时候, PHP也不会把内存还给OS, 而会把这块内存, 归入自己维护的空闲内存列表. 而对于小块内存来说,
更可能的是, 把它放到内存缓存列表中去(后记, 某些版本的PHP, 比如我验证过的PHP5.2.4, 5.2.6, 5.2.8,
在调用get_memory_usage()的时候, 不会减去内存缓存列表中的可用内存块大小, 导致看起来, unset以后内存不变

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!