在使用Java开始游戏之前等待GC完成吗? [英] Waiting for GC to finish before starting game with Java?

查看:87
本文介绍了在使用Java开始游戏之前等待GC完成吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个快节奏的实时 Android游戏,并且一切正常,但是游戏开始的最初几秒钟非常缓慢,因为加载后垃圾收集器正在清理线.当然,玩家可以等待几秒钟(例如10秒钟以上),因为完成后它会开始完美平稳地运行,但这看上去确实很丑陋,而且感觉像是越野车.

I'm making a fast-paced realtime Android game, and everything works great, but the first couple of seconds when the game starts are very laggy because the garbage collector is cleaning up after the loading thread. Sure, the player could wait a few seconds (like 10+ sec) because after its done it starts running perfectly smooth, but that looks really ugly and feels like the game is buggy.

是否有一种方法(或技术)来告知何时安全启动游戏,以使垃圾收集器不会在实时部分开始时就发疯了?不中断工作就无法减少大量的脂肪装载线程.

Is there a way (or technique) to tell when its safe to start the game so the garbage collector won't start going crazy as soon as the real-time part begins? the big fat loading thread can't be reduced much without breaking things.

推荐答案

这似乎是System.gc()可能会有所帮助的情况.它告诉系统这将是收集垃圾的好时机.根据文档,

This seems like a case where System.gc() might help. It tells the system that this would be a good time to collect garbage. According to the documentation,

当控件从方法调用中返回时,Java虚拟机将尽最大努力从所有丢弃的对象中回收空间.

When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

不过,文档还说,该方法建议"收集对象-即,不能保证它会有所帮助,尤其是如果您仍然遗漏了一些引用时.如果能够正常运行,它将仅从运行时代码(包括运行时)中收集根本无法访问的对象. (例如,该加载器线程不合格,直到它完成运行 为止,并且您的代码中不再有对其的引用.)

The docs also say, though, that the method "suggests" that objects be collected -- that is, there's no guarantee it'll help, particularly if you still have some references squirreled away. If it does work, it'll only collect objects that are not reachable at all from the running code, including the runtime. (That loader thread, for example, is not eligible for collection til it's finished running and your code has no more references to it.)

这篇关于在使用Java开始游戏之前等待GC完成吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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