php中unset只是清除掉一个变量,并不意味着内存会被立即清理,会交给GC来处理。 如果把一个变量置为null,意味着覆写了变量,内存会被更快清理,但是会导致cpu运算,导致执行时间变长。 参考
unset() does just what it’s name says - unset a variable. It does not force immediate memory freeing. PHP’s garbage collector will do it when it see fits - by intention as soon, as those CPU cycles aren’t needed anyway, or as late as before the script would run out of memory, whatever occurs first. If you are doing $whatever = null; then you are rewriting variable’s data. You might get memory freed / shrunk faster, but it may steal CPU cycles from the code that truly needs them sooner, resulting in a longer overall execution time.