Javascript和垃圾收集 [英] Javascript and Garbage collection

查看:100
本文介绍了Javascript和垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以控制Javascript何时执行垃圾收集?我想让它在某些时候执行垃圾收集,以确保我的网站能够顺利运行。

解决方案

Javascript doesn'没有明确的内存管理,它是决定何时清理它的浏览器。由于垃圾收集暂停,有时可能会发生JavaScript不平滑呈现。



有很多技巧可以用来克服由垃圾回收引起的故障(GC)。更多你申请更多你探索。假设你有一个用JavaScript编写的游戏,并且每秒你都在创建一个新的对象,那么很明显,在一定的时间之后,GC将会为你的应用程序创造更多的空间。



对于像游戏这样需要大量空间的实时应用程序,最简单的方法是重复使用相同的内存。这取决于你如何构建你的代码。如果它产生大量垃圾,那么它可能会带来波折的体验。



通过使用简单的过程:这很清楚新关键字表示分配。只要有可能,您可以尝试每次通过添加或修改属性来重复使用同一个对象。这也被称为对象的回收

在数组的情况下,通常使用[]来清除数组,但你应该记住,它也创造了一个新的阵列,并且摒弃了旧阵列。要重复使用相同的块,您应该使用 arr.length = 0 这有相同的效果,但它会重复使用同一个数组对象,而不是创建新对象。



在函数的情况下:有时我们的程序需要使用setInterval或setTimeout来更多的时间或以特定的时间间隔调用特定函数。

  ex:setTimeout(function(){doSomething()},10); 

您可以通过将函数分配给永久变量而不是每次在常规间隔。

 例如:var myfunc = function(){doSomething()} 
setTimeout(myfunc,10);

其他可能的事情是,数组slice()返回一个新数组(基于范围在原始数组中,可以保持原状),字符串的substr也返回一个新的字符串(基于原始字符串中的一个字符范围,可以保持不变),依此类推。调用这些函数会产生垃圾,如果不能正确使用。



为了避免JavaScript中的垃圾非常困难,可以说不可能。它依赖于,如何重用对象和变量以避免垃圾。如果您的代码结构合理并经过优化,您可以将开销降到最低。


Is there any way to control when Javascript performs garbage collection? I would like to enable it to perform garbage collection at certain times to ensure the smooth operation of my web site

解决方案

Javascript doesn't have explicit memory management, it's the browser which decides when to clean it up. Sometimes it may happen that you experience un-smooth rendering of JavaScript due to a garbage collection pause.

There are many techniques that you can apply to overcome glitches caused by garbage collection (GC). More you apply more you explore. Suppose you have a game written in JavaScript , and every second you are creating a new object then its obvious that at after certain amount of time GC will occur to make further space for your application.

For real time application like games, which requires lot of space the simplest thing you can do is to reuse the same memory. It depends on you how you structure your code. If it generates lots of garbage then it might give choppy experience.

By using simple procedures: This is well know that new keyword indicates allocation. Wherever possible you can try to reuse the same object by each time by adding or modifying properties. This is also called recycling of object

In case of Arrays, assigning [] is often used to clear array, but you should keep in mind that it also creates a new array and garbages the old one. To reuse the same block you should use arr.length = 0 This has the same effect but it reuses the same array object instead of creating new one.

In case of functions: Sometimes our program needed to call a specific function more time or on certain intervals by using setInterval or setTimeout.

ex: setTimeout(function() { doSomething() }, 10);

You can optimize the above code by assigning the function to a permanent variable rather than spawning each time at regular intervals.

    ex : var myfunc = function() { doSomething() }
    setTimeout(myfunc, 10);

Other possible thing is that, the array slice() method returns a new array (based on a range in the original array,that can remain untouched), string's substr also returns a new string (based on a range of characters in the original string, that can remain untouched), and so on. Calling these functions creates garbage if not reutilized properly.

To avoid garbage completely in JavaScript is very difficult, you could say impossible. Its depends, how you reuse the objects and variables to avoid garbage. If your code is well structured and optimized you can minimize the overhead.

这篇关于Javascript和垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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