• <bdo id='4K02g'></bdo><ul id='4K02g'></ul>
    <tfoot id='4K02g'></tfoot>

      1. <legend id='4K02g'><style id='4K02g'><dir id='4K02g'><q id='4K02g'></q></dir></style></legend>

      2. <i id='4K02g'><tr id='4K02g'><dt id='4K02g'><q id='4K02g'><span id='4K02g'><b id='4K02g'><form id='4K02g'><ins id='4K02g'></ins><ul id='4K02g'></ul><sub id='4K02g'></sub></form><legend id='4K02g'></legend><bdo id='4K02g'><pre id='4K02g'><center id='4K02g'></center></pre></bdo></b><th id='4K02g'></th></span></q></dt></tr></i><div id='4K02g'><tfoot id='4K02g'></tfoot><dl id='4K02g'><fieldset id='4K02g'></fieldset></dl></div>

        <small id='4K02g'></small><noframes id='4K02g'>

        Javascript 和垃圾收集

        时间:2023-10-12
      3. <tfoot id='moj7R'></tfoot>
            <tbody id='moj7R'></tbody>

          1. <legend id='moj7R'><style id='moj7R'><dir id='moj7R'><q id='moj7R'></q></dir></style></legend>
                <bdo id='moj7R'></bdo><ul id='moj7R'></ul>
              • <small id='moj7R'></small><noframes id='moj7R'>

                <i id='moj7R'><tr id='moj7R'><dt id='moj7R'><q id='moj7R'><span id='moj7R'><b id='moj7R'><form id='moj7R'><ins id='moj7R'></ins><ul id='moj7R'></ul><sub id='moj7R'></sub></form><legend id='moj7R'></legend><bdo id='moj7R'><pre id='moj7R'><center id='moj7R'></center></pre></bdo></b><th id='moj7R'></th></span></q></dt></tr></i><div id='moj7R'><tfoot id='moj7R'></tfoot><dl id='moj7R'><fieldset id='moj7R'></fieldset></dl></div>
                  本文介绍了Javascript 和垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有什么方法可以控制 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 没有显式的内存管理,它是浏览器决定何时清理它.有时,由于垃圾收集暂停,您可能会遇到 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.

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

                  上一篇:在 Google Chrome 中强制进行垃圾收集 下一篇:减少 Javascript 中垃圾收集器活动的最佳实践

                  相关文章

                  最新文章

                  <i id='rFDFJ'><tr id='rFDFJ'><dt id='rFDFJ'><q id='rFDFJ'><span id='rFDFJ'><b id='rFDFJ'><form id='rFDFJ'><ins id='rFDFJ'></ins><ul id='rFDFJ'></ul><sub id='rFDFJ'></sub></form><legend id='rFDFJ'></legend><bdo id='rFDFJ'><pre id='rFDFJ'><center id='rFDFJ'></center></pre></bdo></b><th id='rFDFJ'></th></span></q></dt></tr></i><div id='rFDFJ'><tfoot id='rFDFJ'></tfoot><dl id='rFDFJ'><fieldset id='rFDFJ'></fieldset></dl></div>

                1. <legend id='rFDFJ'><style id='rFDFJ'><dir id='rFDFJ'><q id='rFDFJ'></q></dir></style></legend>

                    <tfoot id='rFDFJ'></tfoot>
                      <bdo id='rFDFJ'></bdo><ul id='rFDFJ'></ul>

                    <small id='rFDFJ'></small><noframes id='rFDFJ'>