且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Javascript 和垃圾收集

更新时间:2023-09-26 19:30:04

Javascript 没有显式的内存管理,它是浏览器决定何时清理它.有时,由于垃圾收集暂停,您可能会遇到 JavaScript 渲染不流畅的情况.

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.

您可以应用许多技术来克服由垃圾回收 (GC) 引起的故障.应用越多,探索越多.假设你有一个用 JavaScript 编写的游戏,并且每一秒你都在创建一个新对象,那么很明显,在一定时间后,GC 会发生,为你的应用程序腾出更多空间.

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.

通过简单的程序: 众所周知,new 关键字表示分配.只要有可能,您可以尝试通过添加或修改属性每次重用相同的对象.这也称为对象回收

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

在数组的情况下,分配 [] 通常用于清除数组,但您应该记住,它也会创建一个新数组并丢弃旧数组.要重用相同的块,您应该使用 arr.length = 0 这具有相同的效果,但它重用了相同的数组对象而不是创建新的对象.

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.

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

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

其他可能的事情是,数组 slice() 方法返回一个新数组(基于原始数组中的范围,可以保持不变),字符串的 substr 也返回一个新字符串(基于原始字符串,可以保持不变),等等.如果没有正确地重用,调用这些函数会产生垃圾.

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.

在 JavaScript 中完全避免垃圾是非常困难的,你可以说不可能.这取决于您如何重用对象和变量以避免垃圾.如果您的代码结构良好且经过优化,您可以最大限度地减少开销.

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.