通过JavaScript对象用于释放内存 [英] Deallocating memory used by Javascript objects

查看:130
本文介绍了通过JavaScript对象用于释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我自己释放分配的内存,或者是有一种垃圾收集的?

Should I free the allocated memory by myself, or is there a kind of garbage collector?

什么情况下可以使用下面的code在JavaScript中?

Is it okay to use the following code in JavaScript?

function fillArray()
{
  var c = new Array;
  c.push(3);
  c.push(2);
  return c;
}

var arr = fillArray();
var d = arr.pop()

感谢

推荐答案

在<带引号的href=\"http://developer.apple.com/DOCUMENTATION/ScriptingAutomation/Conceptual/JSCodingGuide/Advanced/Advanced.html\"相对=nofollow>苹果JavaScript编码准则:

使用delete语句。每当你
  使用新创建的对象
  声明,其中包括删除配对
  声明。这将确保所有的
  与对象相关联的存储器,
  包括它的属性名,是
  可进行垃圾回收。该
  delete语句讨论更
  解放出来的对象。

Use delete statements. Whenever you create an object using a new statement, pair it with a delete statement. This ensures that all of the memory associated with the object, including its property name, is available for garbage collection. The delete statement is discussed more in "Freeing Objects."

这会建议你使用删除命令然后让垃圾回收器释放分配给你的阵列当你使用它完成后。该删除语句只删除一个参考的一点是值得注意的,因为它从C / C ++的行为,在没有垃圾收集和删除立即释放占用的内存。

This would suggest that you use a delete command to then allow the garbage collector to free the memory allocated for your Array when you're finished using it. The point that the delete statement only removes a reference is worth noting in that it differs from the behaviour in C/C++, where there is no garbage collection and delete immediately frees up the memory.

这篇关于通过JavaScript对象用于释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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