使用JavaScript卸载HTML资源 [英] Unloading Resources on HTML with JavaScript

查看:117
本文介绍了使用JavaScript卸载HTML资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作HTML 5游戏,它已经在线了,但它目前很小,一切都还可以。



事情是,随着它的增长,它是将加载许多,许多图像,音乐,音效等。在玩游戏15分钟后,至少有100种不同的资源可能已经加载。由于它是一个HTML5应用程序,它在游戏过程中永远不会刷新页面,因此它们都在后台堆叠。



我注意到我加载的每个资源 - 在WebKit上至少使用Web Inspector - 一旦我删除了< img> < link> 到CSS和其他。我猜它仍然在内存中,只是没有被使用,对吧?

这最终会消耗大量的RAM,并导致性能下降在iOS和Android手机上(我稍微注意到了当前版本),其资源比台式电脑更有限。



我的问题是: strong>是否可以通过JavaScript完全卸载资源,释放RAM中的空间?无需刷新整个页面来清除。



最坏的情况:使用框架是否有帮助,通过删除框架来释放这些框架的资源? / p>

谢谢!

解决方案

您的描述意味着您已完全删除所有对资源的引用。然后,您所看到的行为只是没有被调用来清理空间的垃圾收集器,这在javascript实现中很常见,直到必要。设置为 null 或调用 delete 通常不会更好。



作为一种常见情况,您通常可以在场景加载/卸载过程中调用 CollectGarbage()来强制收集进程。这通常是数据加载游戏阶段时的最佳解决方案,因为这个时间不是时间关键。您通常不希望收集器在游戏过程中调用,除非它不是一个非常实时的游戏。



如果您想要保留特定资源,框架通常是一个困难的解决方案围绕共同的游戏控制。您需要考虑是否刷新整个资源或只是某些资源。


I'm working on a HTML 5 game, it is already online, but it's currently small and everything is okay.

Thing is, as it grows, it's going to be loading many, many images, music, sound effects and more. After 15 minutes of playing the game, at least 100 different resources might have been loaded already. Since it's an HTML5 App, it never refreshes the page during the game, so they all stack in the background.

I've noticed that every resource I load - on WebKit at least, using the Web Inspector - remains there once I remove the <img>, the <link> to the CSS and else. I'm guessing it's still in memory, just not being used, right?

This would end up consuming a lot of RAM eventually, and lead to a downgrade in performance specially on iOS and Android mobiles (which I slightly notice already on the current version), whose resources are more limited than desktop computers.

My question is: Is it possible to fully unload a Resource, freeing space in the RAM, through JavaScript? Without having to refresh the whole page to "clean it".

Worst scenario: Would using frames help, by deleting a frame, to free those frames' resources?.

Thank you!

解决方案

Your description implies you have fully removed all references to the resources. The behavior you are seeing, then, is simply the garbage collector not having been invoked to clean the space, which is common in javascript implementations until "necessary". Setting to null or calling delete will usually do no better.

As a common case, you can typically call CollectGarbage() during scene loads/unloads to force the collection process. This is typically the best solution when the data will be loaded for game "stages", as that is a time that is not time critical. You usually do not want the collector to invoke during gameplay unless it is not a very real-time game.

Frames are usually a difficult solution if you want to keep certain resources around for common game controls. You need to consider whether you are refreshing entire resources or just certain resources.

这篇关于使用JavaScript卸载HTML资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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