問題描述
這樣可以嗎?:
function mygetTime()
{
var d = new Date();
return(d.getTime());
}
function wasteSomeMemory()
{
var temp;
for(var count = 0; count < 1000000; count += 1)
{
temp = mygetTime();
}
}
調用 wasteSomeMemory()
會導致內存泄漏嗎?
Will calling wasteSomeMemory()
cause a memory leak?
這個呢:
function wasteSomeMemory2()
{
var temp;
for(var count = 0; count < 1000000; count += 1)
{
temp = new Date();
}
}
調用 wasteSomeMemory2()
會導致內存泄漏嗎?我應該在 for 循環的末尾使用 delete temp;
嗎?
Will calling wasteSomeMemory2()
cause a memory leak? Should I use delete temp;
at the end of the for-loop?
function wasteSomeMemory2()
{
var temp;
for(var count = 0; count < 1000000; count += 1)
{
temp = new Date();
delete temp;
}
}
推薦答案
new
和 delete
在 JavaScript 中彼此沒有任何關系(盡管它們與其他語言中完全不同的結構).不要擔心在沒有明確清理的情況下創建對象(new
),這是垃圾收集器的工作.
new
and delete
have nothing whatsoever to do with each other in JavaScript (despite their confusing similarity to completely different constructs in other languages). Don't worry about creating objects (new
) without explicitly cleaning them up, that's the garbage collector's job.
new
用于通過構造函數創建對象.另一方面,delete
用于從對象中刪除屬性.它與從內存中刪除對象無關,除了作為副作用(例如,如果對該對象的唯一未完成引用來自您刪除的屬性).
new
is for creating objects via constructor functions. delete
, on the other hand, is for removing properties from objects. It has nothing to do with removing an object from memory, other than as a side effect (e.g., if the only outstanding reference to that object was from the property that you removed).
delete
的正確使用示例:
var obj = {};
obj.foo = "bar"; // Now `obj` has a property called `foo`
delete obj.foo; // Now it doesn't
您的 getmyTime
功能非常好.Date
對象將有資格在函數返回后立即被回收(是否被回收完全取決于實現).它不會導致內存泄漏,除非有錯誤的實現.
Your getmyTime
function is perfectly fine. The Date
object will become eligible to be reclaimed immediately upon function return (whether it is reclaimed is completely down to the implementation). It does not cause a memory leak, except on a buggy implementation.
您的 wasteSomeMemory2
同樣不會導致內存泄漏,實際上您不能調用 delete temp;
—你只能刪除屬性,不能刪除變量.
Your wasteSomeMemory2
similarly doesn't cause a memory leak, and in fact you can't call delete temp;
— you can only delete properties, not vars.
有 次您必須幫助垃圾收集器,但這些通常(根據我的經驗)與對象屬性無關,因此不涉及 delete代碼>.它們只有在您創建函數實例時才會真正出現(如果您正在設置事件處理程序或計時器函數等,這很常見).例如,考慮:
There are times when you have to help the garbage collector out, but those usually don't (in my experience) have to do with object properties and so don't involve delete
. They only really come up when you're creating function instances (which is fairly often, if you're setting up event handlers or timer functions, etc.). For instance, consider:
function foo() {
var listOfThings = /* ...get a list of things... */;
// ...do something with `listOfThings`...
setInterval(function() {
// ...do something that *doesn't* need `listOfThings`...
}, 1000);
}
因為您通過 setInterval
分配給計時器的匿名函數將在函數調用中繼續存在,它會保持對該函數調用期間范圍內所有內容的實時引用(無論它是否使用它)或不).這會將 listOfThings
指向的事物列表保存在內存中.如果計時器功能不需要該列表,那是一個問題.如果您知道該函數不需要它,則可以通過分配 undefined
或 null
或其他任何內容來釋放 listOfThings
指向的列表listOfThings
完成后:
Because your anonymous function you've assigned to a timer via setInterval
will survive the function call, it keeps a live reference to everything that was in-scope during that function call (whether it uses it or not). This keeps the list of things that listOfThings
points to in memory. If the timer function doesn't need that list, that's a concern. You can release the list that listOfThings
points to if you know that the function doesn't need it, by assigning undefined
or null
or whatever to listOfThings
when you're done with it:
function foo() {
var listOfThings = /* ...get a list of things... */;
// ...do something with `listOfThings`...
listOfThings = undefined; // Done with it <== The new bit
setInterval(function() {
// ...do something that *doesn't* need `listOfThings`...
}, 1000);
}
事件處理函數等也是如此.每當您創建一個函數時,它都會關閉"(保持對它的定義范圍內的任何內容的實時引用).因此,如果您不需要這些東西,您可以通過清除對它們的引用來確保它們不會保留在內存中.(更多:閉包并不復雜)
The same is true for event handler functions, etc. Whenever you create a function, it "closes over" (keeps a live reference to) anything in scope where it was defined. So if you don't need those things, you can ensure they're not kept in memory by clearing the references to them. (More: Closures are not complicated)
這篇關于Javascript中同一變量上的“new"沒有“delete"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!