什么时候在游戏中调用System.gc()?[javaME]

简介:
不知道大家有否看过“反编译的西伯利亚攻击源代码”的代码,okay,不知道它的权威性有多大,不过西伯利亚攻击这个游戏还是有名气的,也许值得一看。
它的主引擎就是声明一个静态的图片数组:
class  MainEngine  extends  FullCanvas
    
implements  Runnable
{
。。。。
    
static Image imgs[];
    
public static void initImage() {
      
if (!isLoadImg) {
        
if(imgs == null)
            imgs 
= new Image[GameData.imgName.length];
        
for (int i = 0; i < GameData.imgName.length - 1; i++{
          
if (imgs[i] == null{
            
try {
              imgs[i] 
= Image.createImage("/" + GameData.imgName[i]);
            }

            
catch (Exception ex) {
              System.out.println(
"i:"+i);
              ex.printStackTrace();
            }

          }

          percent 
= (i * 100/ (GameData.imgName.length - 1);
          Sib.engine.flushGraphic();
        }

        isLoadImg 
= true;
      }

    }

而且我搜索了它的5个java文件,它在什么情况下调用System.gc()呢?
下面三种情况:
用户返回游戏时(可能之前去接听电话了?或者去设置里改参数了?);
用户选完飞机后,这可能是一个选项菜单,选飞机时所用到的资源可能以后在游戏中没必要用,所以及时回收一下;
显示过关界面时,这时候有卡的现象,欢喜的用户也不会在意的。

除此之外就没有了。

Nokia 的文档Known Issues In The Nokia 6600 MIDP 2.0 Implementation v1.7说:
2.15.1 Garbage collecting
Description
Calling the System.gc() method results in extreme slowness and jamming. In Monty 1.0 VM, garbage 
collection is different and every time System.gc is called, the entire memory is really cleared. 
This is an extremely slow process!
Solution
Do not call the System.gc method at all, or call the System.gc() garbage collecting method only 
in non-time-critical situations, such as screen transitions, state transitions, pause states, 
etc. If the System.gc() method is used, it is recommended to add a short delay (~20-50 ms) after 
the method call to ensure the sufficient time for the garbage collection, as in the following 

example
System.gc();
Thread.sleep(delay); delay = 20-50 ms

目录
相关文章
|
5月前
|
Java
滚雪球学Java(17):探索循环控制:JavaSE中的break与continue秘技
【4月更文挑战第6天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
60 1
滚雪球学Java(17):探索循环控制:JavaSE中的break与continue秘技
|
2月前
|
Java 容器
【Azure Function App】Java Function在运行中遇见内存不足的错误
【Azure Function App】Java Function在运行中遇见内存不足的错误
|
3月前
|
Java API
Java演进问题之如果程序不满足"closed-world"假设,Substrate VM如何解决
Java演进问题之如果程序不满足"closed-world"假设,Substrate VM如何解决
|
3月前
|
Java UED
Java面试题:描述JVM中垃圾收集的Stop-The-World现象及其影响
Java面试题:描述JVM中垃圾收集的Stop-The-World现象及其影响
43 1
|
安全 Java
灵魂拷问:你真的理解System.out.println()打印原理吗?
灵魂拷问:你真的理解System.out.println()打印原理吗?
116 0
App Inventor 2 模拟sleep函数
App Inventor 2 原生没有 sleep 及相关函数,需要模拟实现,经过测试这里给出一个既简单又相对高效率的实现方案,经过测试,该方法有效,能正常 sleep 指定的毫秒数,但是它是阻塞式的,也就是说等待的过程中其他界面操作均无法进行,不过好在CPU消耗并不高,也算是一种不错的实现方式。
180 0
App Inventor 2 模拟sleep函数
调用 System.gc() 后究竟发生了什么?
调用 System.gc() 后究竟发生了什么?
|
存储 监控 算法
System.gc()与Runtime.gc()的区别
System.gc()与Runtime.gc()的区别
186 0
|
缓存 Java Linux
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
203 0
|
缓存 Java
JVM相关 - SafePoint 与 Stop The World 全解)(上)
JVM相关 - SafePoint 与 Stop The World 全解)(上)
JVM相关 - SafePoint 与 Stop The World 全解)(上)