• <legend id='1TXRV'><style id='1TXRV'><dir id='1TXRV'><q id='1TXRV'></q></dir></style></legend>

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

        • <bdo id='1TXRV'></bdo><ul id='1TXRV'></ul>

        <tfoot id='1TXRV'></tfoot>

        <small id='1TXRV'></small><noframes id='1TXRV'>

      1. Javascript强制GC收集?/强制释放对象?

        时间:2023-10-12

        1. <tfoot id='BJYMn'></tfoot>
          <i id='BJYMn'><tr id='BJYMn'><dt id='BJYMn'><q id='BJYMn'><span id='BJYMn'><b id='BJYMn'><form id='BJYMn'><ins id='BJYMn'></ins><ul id='BJYMn'></ul><sub id='BJYMn'></sub></form><legend id='BJYMn'></legend><bdo id='BJYMn'><pre id='BJYMn'><center id='BJYMn'></center></pre></bdo></b><th id='BJYMn'></th></span></q></dt></tr></i><div id='BJYMn'><tfoot id='BJYMn'></tfoot><dl id='BJYMn'><fieldset id='BJYMn'></fieldset></dl></div>
          • <legend id='BJYMn'><style id='BJYMn'><dir id='BJYMn'><q id='BJYMn'></q></dir></style></legend>
            1. <small id='BJYMn'></small><noframes id='BJYMn'>

                <tbody id='BJYMn'></tbody>

                • <bdo id='BJYMn'></bdo><ul id='BJYMn'></ul>
                  本文介绍了Javascript强制GC收集?/强制释放对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个 js 函数,用于使用音频接口播放任何给定的声音(为每个调用创建一个新实例).

                  I have a js function for playing any given sound using the Audio interface (creating a new instance for every call).

                  这工作得很好,直到大约第 32 次调用(有时更少).此问题与 Audio 实例的发布直接相关.我知道这一点是因为我已经让 Chromium 中的 GC 有时间运行,它可以让我再次播放另外 32 种左右的声音.

                  This works quite well, until about the 32nd call (sometimes less). This issue is directly related to the release of the Audio instance. I know this because I've allowed time for the GC in Chromium to run and it will allow me to play another 32 or so sounds again.

                  这是我正在做的一个例子:

                  Here's an example of what I'm doing:

                  <html><head>
                  <script type="text/javascript">
                  function playSound(url) {
                      var snd = new Audio(url);
                      snd.play();
                      snd = null;
                  }
                  </script>
                  </head>
                  
                  <body>
                  <a href="#" onclick="playSound('blah.mp3');">Play sound</a>
                  </body></html>
                  

                  我也有这个,它适用于少于 32 个 playSound 调用的页面:

                  I also have this, which works well for pages that have less than 32 playSound calls:

                  var AudioPlayer = {
                      cache: {},
                      play: function(url) {
                          if (!AudioPlayer.cache[url])
                              AudioPlayer.cache[url] = new Audio(url);
                          AudioPlayer.cache[url].play();
                      }
                  };
                  

                  但这不适用于我想做的事情(用其他内容(来自单独的文件)动态替换 div,这些内容有更多的声音 - 1. 内存使用量很容易飙升,2. 很多声音永远不会玩).

                  But this will not work for what I want to do (dynamically replace a div with other content (from separate files), which have even more sounds on them - 1. memory usage would easily skyrocket, 2. many sounds will never play).

                  我需要一种立即释放声音的方法.是否有可能做到这一点?我没有找到音频接口的免费/关闭/卸载方法.

                  I need a way to release the sound immediately. Is it possible to do this? I have found no free/close/unload method for the Audio interface.

                  页面将在本地查看,因此声音的持续加载根本不是一个重要因素(而且大多数声音都很短).

                  The pages will be viewed locally, so the constant loading of sounds is not a big factor at all (and most sounds are rather short).

                  推荐答案

                  这不是一个详尽的答案,而是针对有没有办法强制chrome js引擎进行垃圾收集?"的问题,一个 chromium.org 的人回复了:

                  This is not an exhaustive answer, but to the question "Is there any way to force the chrome js engine to do garbage collection?", a chromium.org guy replied:

                  一般来说,不,设计使然. 出于测试目的,您可以通过一个标志在命令行上启用 javascript 命令window.gc()"强制垃圾收集.

                  In general, no, by design. For testing purposes there is a flag you can pass on the command line to enable a javascript command "window.gc()" to force garbage collection.

                  --js-flags '--expose_gc'

                  <小时>

                  更新:但是,正如@plash 在下面的评论中指出的那样,此标志将仅适用于调试版本.


                  UPDATE: However, as @plash noted in a comment below, this flag will only work in debug builds.

                  这篇关于Javascript强制GC收集?/强制释放对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我可以触发 JavaScript 的垃圾回收吗? 下一篇:如何销毁 THREEJS 场景?

                  相关文章

                  最新文章

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

                  • <bdo id='FwE3F'></bdo><ul id='FwE3F'></ul>

                    <legend id='FwE3F'><style id='FwE3F'><dir id='FwE3F'><q id='FwE3F'></q></dir></style></legend>

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