問(wèn)題描述
有什么方法可以控制 Javascript 何時(shí)執(zhí)行垃圾回收?我想讓它在特定時(shí)間執(zhí)行垃圾收集,以確保我的網(wǎng)站順利運(yùn)行
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 沒(méi)有顯式的內(nèi)存管理,它是瀏覽器決定何時(shí)清理它.有時(shí),由于垃圾收集暫停,您可能會(huì)遇到 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.
您可以應(yīng)用許多技術(shù)來(lái)克服由垃圾回收 (GC) 引起的故障.應(yīng)用越多,探索越多.假設(shè)你有一個(gè)用 JavaScript 編寫(xiě)的游戲,并且每一秒你都在創(chuàng)建一個(gè)新對(duì)象,那么很明顯,在一定時(shí)間后,GC 會(huì)發(fā)生,為你的應(yīng)用程序騰出更多空間.
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.
對(duì)于像游戲這樣需要大量空間的實(shí)時(shí)應(yīng)用程序,您可以做的最簡(jiǎn)單的事情就是重復(fù)使用相同的內(nèi)存.這取決于您如何構(gòu)建代碼.如果它產(chǎn)生大量垃圾,那么它可能會(huì)帶來(lái)不穩(wěn)定的體驗(yàn).
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.
通過(guò)簡(jiǎn)單的程序: 眾所周知,new 關(guān)鍵字表示分配.只要有可能,您可以嘗試通過(guò)添加或修改屬性每次重用相同的對(duì)象.這也稱(chēng)為對(duì)象回收
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
在數(shù)組的情況下,分配 [] 通常用于清除數(shù)組,但您應(yīng)該記住,它也會(huì)創(chuàng)建一個(gè)新數(shù)組并丟棄舊數(shù)組.要重用相同的塊,您應(yīng)該使用 arr.length = 0
這具有相同的效果,但它重用了相同的數(shù)組對(duì)象而不是創(chuàng)建新的對(duì)象.
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.
在函數(shù)的情況下:有時(shí)我們的程序需要通過(guò)使用 setInterval 或 setTimeout 來(lái)調(diào)用特定函數(shù)更多時(shí)間或在特定時(shí)間間隔內(nèi).
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);
您可以通過(guò)將函數(shù)分配給永久變量而不是每次定期生成來(lái)優(yōu)化上述代碼.
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);
其他可能的事情是,數(shù)組 slice() 方法返回一個(gè)新數(shù)組(基于原始數(shù)組中的范圍,可以保持不變),字符串的 substr 也返回一個(gè)新字符串(基于原始字符串,可以保持不變),等等.如果沒(méi)有正確地重用,調(diào)用這些函數(shù)會(huì)產(chǎn)生垃圾.
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 中完全避免垃圾是非常困難的,你可以說(shuō)不可能.這取決于您如何重用對(duì)象和變量以避免垃圾.如果您的代碼結(jié)構(gòu)良好且經(jīng)過(guò)優(yōu)化,您可以最大限度地減少開(kāi)銷(xiāo).
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.
這篇關(guān)于Javascript 和垃圾收集的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!