PHP100 2019-03-27
首先让我们看一个问题: 如下代码的输出,
代码如下:
var_dump(memory_get_usage()); $a = "laruence"; var_dump(memory_get_usage()); unset($a); var_dump(memory_get_usage()); 输出(在我的个人电脑上, 可能会因为系统,PHP版本,载入的扩展不同而不同): int(90440) int(90640) int(90472)
代码如下:
emalloc(size_t size); efree(void *ptr); ecalloc(size_t nmemb, size_t size); erealloc(void *ptr, size_t size); estrdup(const char *s); estrndup(const char *s, unsigned int length);
代码如下:
<?php var_dump(memory_get_usage(TRUE)); //注意获取的是real_size $a = "laruence"; var_dump(memory_get_usage(TRUE)); unset($a); var_dump(memory_get_usage(TRUE)); 输出: int(262144) int(262144) int(262144)
代码如下:
<?php var_dump("I am Laruence, From http://www.laruence.com"); var_dump(memory_get_usage()); $a = "laruence"; var_dump(memory_get_usage()); unset($a); var_dump(memory_get_usage()); 输出: string(43) "I am Laruence, From http://www.laruence.com" int(90808) //赋值前 int(90976) int(90808) //是的, 内存正常释放了 90808-90808 = 0, 正常了, 也就是说这32个字节是被输出函数给占用了(严格来说, 是被输出的Header占用了)
代码如下:
var_dump("I am Laruence, From http://www.laruence.com"); var_dump(memory_get_usage()); $array = array_fill(1, 100, "laruence"); foreach ($array as $key => $value) { ${$value . $key} = NULL; } var_dump(memory_get_usage()); foreach ($array as $key=> $value) { unset(${$value . $key}); } var_dump(memory_get_usage());
代码如下:
string(43) "I am Laruence, From http://www.laruence.com" int(93560) int(118848) int(104448)
<?php. if (!empty($_POST)) {. $data1 = $_POST["data1"];$data2 = $_POST["data2"];$fuhao = $_POST["fuh