``新`没有'删除`在同一个变量的Javascript [英] `new` without `delete` on same variable in Javascript

查看:135
本文介绍了``新`没有'删除`在同一个变量的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数mygetTime()
{
var d = new Date();
return(d.getTime());
}

函数wasteSomeMemory()
{
var temp; (var count = 0; count< 1000000; count + = 1)

{
temp = mygetTime();




$ b

将调用 wasteSomeMemory() 导致内存泄漏?



这是怎么回事:

  function wasteSomeMemory2()
{
var temp; (var count = 0; count< 1000000; count + = 1)

{
temp = new Date();




$ b $ wasteSomeMemory2() 导致内存泄漏?我应该在for循环结尾处使用 delete temp; 吗?

  function wasteSomeMemory2()
{
var temp; (var count = 0; count< 1000000; count + = 1)

{
temp = new Date();
删除temp;
}
}


解决方案

new delete 在JavaScript中没有任何关系(尽管它们与其他语言中完全不同的结构相似)。不必担心创建对象( new )而不显式清理它们,这就是垃圾收集器的工作。



new 用于通过构造函数创建对象。另一方面, delete 用于从对象中移除属性。除了副作用之外,它没有 与从内存中移除对象有关(例如,如果该对象的唯一未完成引用来自您删除的属性)。



正确使用 delete 的示例:

  var obj = {}; 
obj.foo =bar; //现在`obj`有一个名为`foo`的属性
delete obj.foo; //现在它不是

您的 getmyTime 功能非常好。在函数返回时(无论是 回收完全由实现决定), Date 对象将立即被回收。它不会导致内存泄漏,除了一个错误的实现。



您的 wasteSomeMemory2 类似地不会导致内存泄漏,实际上你不能调用 delete temp;  —您只能删除属性,而不是变量。






是需要帮助的时间垃圾收集器出来了,但那些通常不会(以我的经验)与对象属性有关,因此不涉及 delete 。当你创建函数实例时,它们只会真的出现(如果你正在设置事件处理程序或定时器函数等,那就很常见了)。例如,考虑:

  function foo(){
var listOfThings = / * ...获取事情... * /;

// ...使用`listOfThings`做一些事情...

setInterval(function(){
// ...做一些* doesn 't * need`listOfThings` ...
},1000);





$ b因为你通过<$ c $分配给定时器的匿名函数c> setInterval
将在该函数调用中存活下来,它保留对该函数调用期间所有内容(不管它是否使用它)的实时引用。这保留了 listOfThings 指向内存的列表。如果计时器功能不需要该列表,那就是一个问题。通过分配 undefined ,如果知道该函数不需要它,可以释放列表 listOfThings 指向的列表。或 null 或其他任何对 listOfThings 完成的操作:

 函数foo(){

var listOfThings = / * ...获取事物列表... * /;

// ...用`listOfThings`做些事情......

listOfThings = undefined; //完成它< ==新位

setInterval(function(){
// ...做一些*不需要`listOfThings` ...
},1000);

$ / code>

事件处理函数等也是如此。每当你创建一个函数,它关闭(保持活动引用)任何在它定义的范围内的东西。所以,如果你不需要这些东西,你可以通过清除对它们的引用来确保它们不被记忆。 (更多: 闭包不复杂


Is it OK to do this?:

function mygetTime()
{
    var d = new Date();
    return(d.getTime());
}

function wasteSomeMemory()
{
    var temp;
    for(var count = 0; count < 1000000; count += 1)
    {
        temp = mygetTime();
    }
}

Will calling wasteSomeMemory() cause a memory leak?

What about this:

function wasteSomeMemory2()
{
    var temp;
    for(var count = 0; count < 1000000; count += 1)
    {
        temp = new Date();
    }
}

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 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 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).

Example of correct use of delete:

var obj = {};
obj.foo = "bar"; // Now `obj` has a property called `foo`
delete obj.foo;  // Now it doesn't

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.

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.


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);
}

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆