PHP 的内存问题 (5)

发布于 2024-09-12 04:13:26 字数 93 浏览 11 评论 0原文

调用 unset() 是否释放与该对象关联的内存?在某些情况下,我发现自己正在处理大型关联数组,并且我想在完成后删除它们(释放内存以创建新数组)。

Does calling unset() free the memory that was associated with that object? There are a couple cases where I find myself handling large associative arrays and I would like to remove them when done (freeing up memory to create new ones).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

2024-09-19 04:13:26

调用unset()是否释放内存
与该对象相关联?

是的。

使用 memory_get_usage()

echo memory_get_usage() . "<br />";
unset($array['key']);
unset($array['key2']);
unset($array['key3']);
echo memory_get_usage();

更多资源:

引用后面的链接:

PHP 执行垃圾回收的时间为
三个主要关口:

  1. 当你告诉它
  2. 当您离开某个功能时
  3. 脚本结束时

当您使用时会出现情况1
unset()、mysql_free_result() 或其他
破坏资源的功能
在你之后明确清理
变量。情况 2 清楚
隐式资源 - 任何变量
留下了范围,即不再是
适用,已为您清理。
最后,情况 3 释放了所有
隐式与脚本相关的资源。

Does calling unset() free the memory
that was associated with that object?

Yes it does.

Check for yourself using memory_get_usage():

echo memory_get_usage() . "<br />";
unset($array['key']);
unset($array['key2']);
unset($array['key3']);
echo memory_get_usage();

More Resources:

Quoting from later link:

PHP performs garbage collection at
three primary junctures:

  1. When you tell it to
  2. When you leave a function
  3. When the script ends

Situation 1 occurs when you use
unset(), mysql_free_result(), or other
resource-destroying functions that
explicitly clear up after your
variables. Situation 2 clears up
resources implicitly - any variable
that leaves scope, i.e. is no longer
applicable, gets cleared up for you.
Finally, situation 3 frees up all
script-related resources implicitly.

别再吹冷风 2024-09-19 04:13:26

unset() 在 php 中“释放”内存,但它让垃圾收集器决定何时实际释放所述内存。因此,内存会根据需要或方便地释放(在 PHP 耗尽可用内存之前)。

一个主要的警告是注意您没有尝试在本地范围内 unset() 全局变量。即使是通过引用传入的变量,当在函数的语言环境中执行此操作时,也只会取消设置其本地引用。为了真正使内存可用,任何 unset() 都应该在该变量的适当范围内完成。

unset() does "free" memory in php, but it lets the garbage collector decide when to actually release said memory. Thus, memory is freed on an as-needed or as-convenient basis (prior to PHP running out of available memory).

A major caveat is to watch that you're not attempting to unset() global variables within a local scope. Even variables passed in by reference will only have their local references unset when this is performed in a function's locale. To truly make memory available, any unset() should be done in that variable's appropriate scope.

一绘本一梦想 2024-09-19 04:13:26

它不会立即释放内存,但它允许垃圾收集器执行此操作。

It doesn't free the memory immediately, but it allows the garbage collector to do it.

享受孤独 2024-09-19 04:13:26

通过取消设置变量,您将其“引用计数”设置为 0。这允许 Zend 引擎在内存中移动内容,并且它知道它可以覆盖内存中的变量区域,因为它的引用计数为 0。

这发生在之前甚至还想到了垃圾收集。现在你已经知道了,unset() 实际上可以在应用程序的生命周期中为你提供帮助。

By unsetting a variable you're setting it's "refcount" to 0. This allows for the Zend Engine to shift things about in memory and it knows that it can overwrite your variables area in memory as it has a refcount of 0.

This occurs before garbage collection is even thought of. So there you have it, unset() does actually help you out during the lifecycle of your app.

穿越时光隧道 2024-09-19 04:13:26

不,它不一定会释放任何东西。它只是减少了引用计数。

示例:

//object has reference count 1 because it has one variable referencing 1
$a = new BigObject;
//object still has reference count 2, but no new object was allocated in memory
$b = $a;
//object has reference count 1
unset($a);
//the object is still in memory

挑剔角落:从技术上讲,这里有两个引用计数——对象的和 zval 的。在整个程序中,对象的引用计数只有 1,实际上是 zval 的引用计数在赋值和调用 unset 中发生了变化。

No, it doesn't necessarily free anything. It just decreases the reference count.

Example:

//object has reference count 1 because it has one variable referencing 1
$a = new BigObject;
//object still has reference count 2, but no new object was allocated in memory
$b = $a;
//object has reference count 1
unset($a);
//the object is still in memory

Nitpick corner: technically, there are two reference counts in play here -- the object's and the zval's. The reference count of the object is only 1 throughout the program, it's actually the reference count of the zval that's changed in the assignment and in the call to unset.

指尖上得阳光 2024-09-19 04:13:26

In PHP >5.3.0, the gc_collect_cycles() function forces collection of any existing garbage cycles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文