zoukankan      html  css  js  c++  java
  • 什么时候在游戏中调用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

  • 相关阅读:
    通过Java获取图片的信息
    防止浏览器缓冲
    InitializingBean,DisposableBean
    php 直接跳出嵌套循环
    个推IGt.BaseTemplate.php,不仅有bug,还有bom头,好恶心!
    【洛谷】2473:[SCOI2008]奖励关【期望DP(倒推)】
    【BZOJ】4720: [Noip2016]换教室
    【BZOJ】4767: 两双手【组合数学】【容斥】【DP】
    【BZOJ】4318: OSU!【期望DP】
    【BZOJ】4985: 评分【DP】
  • 原文地址:https://www.cnblogs.com/zhengyun_ustc/p/systemgc.html
Copyright © 2011-2022 走看看