1. 普通写法
首先,以一个例子来说明,重复new一个对象的内存占用情况:
<?php
class A {
function test()
{
var_dump("dsfsdflsd");
}
function testB()
{
$a = ["就是","哈哈哈","哈哈哈哈","哈哈哈哈","就是","哈哈哈","哈哈哈哈","哈哈哈哈","就是","哈哈哈","哈哈哈哈","哈哈哈哈"];
var_dump($a);
}
}
$a = new A();
echo "1 ";
var_dump(memory_get_usage());
$a->test();
echo "2 ";
var_dump(memory_get_usage());
$a->testB();
echo "3 ";
var_dump(memory_get_usage());
$b = new A();
echo "4 ";
var_dump(memory_get_usage());
$c = new A();
echo "5 ";
var_dump(memory_get_usage());
$d = new A();
echo "6 ";
var_dump(memory_get_usage());
$e = new A();
echo "7 ";
var_dump(memory_get_usage());
上述例子的执行结果:
从上述结果,很明显的看到,没new 对象,对应的内存使用率随之增长;
2. 普通单例模式写法
现在,使用单例模式:
<?php
class A {
private static $instance;
private function __construct()
{
}
public static function getInstance()
{
if (!(self::$instance instanceof self)) {
self::$instance = new self();
}
return self::$instance;
}
public function test()
{
var_dump("dsfsdflsd");
}
public function testB()
{
$a = ["就是","哈哈哈","哈哈哈哈","哈哈哈哈","就是","哈哈哈","哈哈哈哈","哈哈哈哈","就是","哈哈哈","哈哈哈哈","哈哈哈哈"];
var_dump($a);
}
//克隆方法私有化,防止复制实例
private function __clone(){}
}
$a = A::getInstance();
echo "1 ";
var_dump(memory_get_usage());
$a->test();
echo "2 ";
var_dump(memory_get_usage());
$a->testB();
echo "3 ";
var_dump(memory_get_usage());
$b = A::getInstance();;
echo "4 ";
var_dump(memory_get_usage());
$c = A::getInstance();;
echo "5 ";
var_dump(memory_get_usage());
$d = A::getInstance();;
echo "6 ";
var_dump(memory_get_usage());
$e = A::getInstance();;
echo "7 ";
var_dump(memory_get_usage());
以下是代码执行结果: ?
?从上述执行结果可以看出,内存占用量一直保持不变,说明使用的是同一对象,没有新增重复对象
3. 并发情况下单例模式
内存仍然保持不变;压力测试命令:ab -n 1000 -c 10 url(-c 并发请求数,-n总请求数)
日志结果(内存使用量0表示new对象之前的内存占用量): [2022-02-18 16:24:54] dev.INFO: 16451726941831862993 内存使用量0: 1889832 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:54] dev.INFO: 16451726941831862993 内存使用量1: 1987344 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:54] dev.INFO: 16451726941831862993 内存使用量2: 1987344 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:54] dev.INFO: 16451726941831862993 内存使用量3: 1987344 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:54] dev.INFO: 16451726941831862993 内存使用量4: 1987344 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:54] dev.INFO: 16451726941831862993 内存使用量5: 1987344 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:54] dev.INFO: 16451726941831862993 内存使用量6: 1987344 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:54] dev.INFO: 16451726941831862993 内存使用量7: 1987344 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 16451726951695474085 内存使用量0: 1888920 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 16451726951695474085 内存使用量1: 1986752 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 16451726951695474085 内存使用量2: 1986752 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 16451726951695474085 内存使用量3: 1986752 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 16451726951695474085 内存使用量4: 1986752 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 16451726951695474085 内存使用量5: 1986752 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 16451726951695474085 内存使用量6: 1986752 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 16451726951695474085 内存使用量7: 1986752 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 1645172695791050071 内存使用量0: 1888920 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 1645172695791050071 内存使用量1: 1986752 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 1645172695791050071 内存使用量2: 1986752 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 1645172695791050071 内存使用量3: 1986752 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 1645172695791050071 内存使用量4: 1986752 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 1645172695791050071 内存使用量5: 1986752 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 1645172695791050071 内存使用量6: 1986752 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 1645172695791050071 内存使用量7: 1986752 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 16451726951046930012 内存使用量0: 1888920 {"api":"/x/x/test2","data":[],"code":0}? [2022-02-18 16:24:55] dev.INFO: 16451726951046930012 内存使用量1: 1986752 {"api":"/x/x/test2","data":[],"code":0}?
?从日志记录情况看,内存基本上保持一致
|