2011-08-21 103 views
2

我有一個內存泄漏與字節[],我想了解更多關於此,以防止它發生在未來。如何避免字節[]內存泄漏?

這裏是我的Java代碼:

package server.world; 

import java.io.RandomAccessFile; 
import java.nio.MappedByteBuffer; 
import java.nio.channels.FileChannel; 

public class WalkingHandler { 

    public static final int WIDTH = 12000; 
    public static final int HEIGHT = 9900; 

    private final TiledMap map; 

    private WalkingHandler() { 
     this.map = new TiledMap(WIDTH, HEIGHT); 
    } 

    private static class SingletonContainer { 
     private static final WalkingHandler SINGLETON = new WalkingHandler(); 
    } 

    public static WalkingHandler getSingleton() { 
     return SingletonContainer.SINGLETON; 
    } 

    public boolean traversable(int x, int y, int direction) { 
     int flag = map.getFlag(x, y); 
     //System.out.println(direction); 
     if (direction == 0 && (flag == 1 || flag == 4 || flag == 6 || flag == 7 || flag == 9 || flag == 11 || flag == 13 || flag == 14)) { 
      return false; 
     } else if (direction == 4 && (flag == 1 || flag == 7 || flag == 15 || flag == 10 || flag == 11 || flag == 12 || flag == 14 || flag == 5)) { 
      return false; 
     } else if (direction == 8 && (flag == 1 || flag == 2 || flag == 3 || flag == 4 || flag == 5 || flag == 6 || flag == 7 || flag == 12)) { 
      return false; 
     } else if (direction == 12 && (flag == 1 || flag == 3 || flag == 6 || flag == 9 || flag == 10 || flag == 11 || flag == 12 || flag == 8)) { 
      return false; 
     } else if(flag > 0 && flag < 15) { 
      return false; 
     } 
     return true; 
    } 

    public void initialize() throws Exception { 
     long delta = System.currentTimeMillis(); 
     RandomAccessFile raf = new RandomAccessFile("data/lolmap.bin", "r"); 
     FileChannel channel = raf.getChannel(); 
     MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); 
     int length = buffer.getInt(); 
     for(int i = 0; i < length; i++) { 
      int x = buffer.getShort(); 
      int y = buffer.getShort(); 
      byte flag = buffer.get(); 
      map.flag(x, y, flag); 
     } 
     System.out.println("Loaded clipmap in " + (System.currentTimeMillis() - delta) + "ms."); 
    } 

    private static class TiledMap { 

     private final byte[] plane; 

     public TiledMap(int width, int height) { 
      this.plane = new byte[width * 10000 + height]; 
     } 

     public int getFlag(int x, int y) { 
      return plane[x * 10000 + y]; 
     } 

     public void flag(int x, int y, byte flag) { 
      this.plane[x * 10000 + y] = flag; 
     } 

    } 

} 
請問

有人想替指出我在做什麼錯誤?

+2

請將此代碼粘貼到此處,而不是鏈接到pastebin上。 –

+0

@Aleksandr:你實例化你的TiledMap多少次?除此之外,「x * 10000 + y」是違反直覺的。大多數遊戲設計師都會使用「y * 1000 + x」在包含2D事物的1維數組中存儲/定位「事物」。另外,如果方向/標誌*東西可以肯定地以更好的方式重寫,那麼你的雜亂*。 – SyntaxT3rr0r

+1

你在找什麼內存泄漏?你分配內存並使用它。只要你不需要任何其他的內存,就算我沒有看到,它也不會被垃圾收集,即使它沒有被引用,所有的內存都在你的情況下。所以請解釋一下,如果你需要幫助,你會在哪裏看到哪個問題。 –

回答

2

您要創建一個數組大小12000 * 10000 + 9900是120_009_900字節(這甚至錯誤地初始化:你應該分配12000米* 9900米的空間,並與X *高+ Y讓他們)

private static class TiledMap { 

    private final byte[] plane; 
    private final int width,height; 

    public TiledMap(int width, int height) { 
     this.plane = new byte[width * height]; 
     this.width = width; 
     this.height = height; 
    } 

    public int getFlag(int x, int y) { 
     return plane[x * height + y]; 
    } 

    public void flag(int x, int y, byte flag) { 
     this.plane[x * height + y] = flag; 
    } 

} 

但是,您最好從文件中獲取首先需要多少空間,然後分配

+0

我注意到它實際上不是內存泄漏,但它使用byte []很糟糕。查看服務器的快照。 http://i55.tinypic.com/2vv9z5s.png – Aleksandr

+0

@akek是的,這就是所有這一個數組 –

+0

如何減少它的任何建議?大聲笑 我可能會用一種新的方法來讀取地圖哈哈。 – Aleksandr