yoast造成php内存过大_PHP内存溢出Allowed memory size of 解决办法

本文详细分析了PHP内存溢出错误'Allowed memory size of...exhausted'的原因,解释了memory_limit和allocated_memory的概念,并提供了解决此类问题的方法,包括设置PHP配置文件中的memory_limit或在代码中使用ini_set。此外,文章还讨论了PHP的内存管理和垃圾回收机制,特别关注了对象引用导致的内存泄漏问题及其解决方案,强调了在适当时候使用unset()和__destruct()的重要性。
摘要由CSDN通过智能技术生成

============================Allowed memory size of  xxx bytes

以前追踪过这个问题,但是那个时候工具用的不太好,没看的这么细,这次搞的比较细,修正了偶以前的看法

.于是写小文一篇总结一下.

PHP偶尔会爆一下如下 错误Allowed memory size of  xxx bytes exhausted at xxx:xxx (tried to

allocate xxx bytes)

不想看原理的,直接跳到最后看总结.

这个报错信息的意思是是说,若ini配置的memory_limit(内存限制) 大于 AG(allocated_memory),就报错

AG(allocated_memory) += rs;

if (AG(memory_limit)

int php_mem_limit = AG(memory_limit);

AG(allocated_memory) -= rs;

if (EG(in_execution) && AG(memory_limit)+1048576 > AG(allocated_memory)){

AG(memory_limit) = AG(allocated_memory) + 1048576;

if (file) {

zend_error(E_ERROR,"Allowed memory size of %d bytes exhausted

at %s:%d (tried to allocate %d bytes)", php_mem_limit, file, lineno, s);

} else {

zend_error(E_ERROR,"Allowed memory size of %d bytes exhausted

(tried to allocate %d bytes)", php_mem_limit, s);

}

} else {

if (file) {

fprintf(stderr, "Allowed memory size of %d bytes exhausted

at %s:%d (tried to allocate %d bytes)n", php_mem_limit, file, lineno, s);

} else {

fprintf(stderr, "Allowed memory size of %d bytes exhausted

(tried to allocate %d bytes)n", php_mem_limit, s);

}

exit(1);

}

}

memory_limit很简单,就是PHP可用的内存..AG(allocated_memory)是什么呢?是不是已经使用的内存,恩,

我们用代码验证一下

PHP_FUNCTION(memory_get_usage) {

RETURN_LONG(AG(allocated_memory));

}

这下就清晰明了,还不懂的,查php手册,看memory_get_usage的说明

到底什么时候设置AG(allocated_memory)呢,具体代码就不贴了,太繁琐,是在emalloc函数中调用了第一段

代码,看第一行代码,那里的rs就是每次tried to allocate %d bytes对应的s变量(你要申请的实际空间)

的align对齐

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值