gpt4 book ai didi

php - 什么是吃内存的 PHP 脚本

转载 作者:行者123 更新时间:2023-11-29 09:03:38 25 4
gpt4 key购买 nike

多年来,我一直在阅读有关编写 php 脚本时节省内存的方法。但我应该真正关心什么类型的脚本。哪些代码/脚本/调用占用的内存最多?

编辑

这是一个问题,请不要用问题来回答

编辑

这又怎样呢。这样的东西会吃内存吗?它需要完成,但应该如何释放它?

$manufacture = htmlspecialchars($_REQUEST["manufacture"]);
$manufacture = preg_replace("/[^0-9]/","", $manufacture);



$SQL = "SELECT * FROM STORE_MANUFACTURERS WHERE manufacturers_id = '$manufacture'";
$result = mysql_query( $SQL );
while( $row = mysql_fetch_array( $result ) ) {
$manufacturers_name = $row['manufacturers_name'];
}

最佳答案

当您寻求 100% 完美的内存泄漏外观时,需要考虑很多事情。我所做的就是遵循编码标准和模式。如果我遇到内存泄漏问题,即使在那之后我应该用 profiling 来解决这个问题像XDebug这样的工具或Kcachegrind .

类似 Exception safety应该好好维护

您可以使用类似 memory_get_peak_usage() 的工具, memory_get_usage()找出罪魁祸首。你可以看看这个post regarding Memory Manager .

像 PHP 这样的脚本语言使用自动垃圾收集,例如您不必自己解除分配。垃圾收集器会为您完成此操作(使用引用计数)。但如果你想自己做,你可以使用 unset() 。但据报道,在某些情况下,即使未设置也无法为您完成此操作。也报告了一些与此相关的错误。 bug#33945 bug#33487

reference countinga:Ab:B 都有循环引用时会遇到问题,因此您需要一个实例来获取所有权并析构所提到的对象打破圈子来面对这个问题。

strtotime() memory leak bug#47285 有一些问题

您在编辑中发布的代码不应担心内存泄漏。我会在评论中引用@GolezTrol

All memory allocated by the script is freed as soon as it terminates, which is at the end of each request. You shouldn't really have any concerns unless you are building special scripts or have a website with a very high load. If you have to have this question like this, you probably have neither, so don't worry about it.

关于php - 什么是吃内存的 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7758672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com