本文介绍了使用插座时,为了避免垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的游戏项目中,我尽我所能来避免对象的创建,从而$ ​​P $运行pventing垃圾收集器。

这是一个网络游戏,我主要是发送数据的字节数组,但也有一些其他的对象如int数组。

分析Eclipse中的内存分配,有很多的方式在我的程序创建的字节数组我写/从插座读数/时,我已经注意到了。

  OOS =新的ObjectOutputStream(新的BufferedOutputStream(link.getOutputStream()));
OIS =新的ObjectInputStream(新的BufferedInputStream(link.getInputStream()));

我如何读/接到插座/写(主要是字节数组),而在后台创建任何对象?

还有什么会是这样做网络通信的最快方法?我需要SQUEEZ的性能每一点这份申请的。

修改

我已经改变了我的code了一下,我现在只能通过送OOS宽松字节整数。
还有分配完成的。

阅读code

  @覆盖
公共无效的run(){
    super.run();
    字节的包= -1;
    而(连接){
        尝试{
            包= ois.readByte();
            handlePacket(包);
        }赶上(OptionalDataException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }赶上(ClassNotFoundException的E){
            // TODO自动生成catch块
            e.printStackTrace();
        }赶上(IOException异常五){
            连接= FALSE;
        }
    }}@覆盖
公共无效handlePacket(BYTE B)抛出OptionalDataException,ClassNotFoundException的,IOException异常{
    super.handlePacket(二);
    // TODO添加更多的数据包
    开关(二){
    案例Packet.SERVER_SEND_PLAYER_LOCATIONS:
        this.locations =(INT [])ois.readObject();
        打破;
    案例Packet.SERVER_SEND_PLAYER_COLORS:
        this.colors =(INT [])ois.readObject();
        打破;
    案例Packet.SERVER_SEND_WORM_WIDTH:
        wormWidth = ois.read();
        打破;
    案例Packet.SERVER_SEND_PLAYER_NUMBER:
        numberOfPlayers = ois.read();
        打破;
    案例Packet.CS_SEND_TURN:
        gp.addTurn(ois.read(),ois.read(),ois.readByte());
        打破;
    }
}

书写

 公共无效发送(字节值){
    尝试{
        oos.write(值);
        oos.flush();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }}
公共无效发送(int值){
    尝试{
        oos.write(值);
        oos.flush();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
}


解决方案

只要确保有这些数组的引用!

你的一些code,有利于

In a game project of mine i'm trying my best to avoid the creation of objects and thus preventing the garbage collector from running.

It is a network game and I am mostly sending byte arrays of data but also some other objects like int arrays.

I have noticed when analyzing the memory allocation in eclipse that there are alot of byte arrays created in my program by the way I'm writing/reading to/from the sockets.

oos=new ObjectOutputStream(new BufferedOutputStream(link.getOutputStream()));
ois=new ObjectInputStream(new BufferedInputStream(link.getInputStream()));

How can I read / write (mostly byte arrays) from / to sockets without creating any more objects in the background?

Also what would be the fastest way to do this network communication? I need to squeez every bit of performance out of this application.

EDIT

I have changed my code a bit and i now only send loose bytes and ints through the oos.Still there is allocations done.

Allocations screenshot

Reading code

@Override
public void run() {
    super.run();
    byte packet = -1;
    while(connected){
        try {
            packet = ois.readByte();
            handlePacket(packet);
        } catch (OptionalDataException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            connected=false;
        }
    }

}

@Override
public void handlePacket(byte b) throws OptionalDataException, ClassNotFoundException, IOException {
    super.handlePacket(b);
    //TODO add more packets 
    switch(b){
    case Packet.SERVER_SEND_PLAYER_LOCATIONS:
        this.locations=(int[]) ois.readObject();
        break;
    case Packet.SERVER_SEND_PLAYER_COLORS:
        this.colors= (int[]) ois.readObject();
        break;
    case Packet.SERVER_SEND_WORM_WIDTH:
        wormWidth = ois.read();
        break;
    case Packet.SERVER_SEND_PLAYER_NUMBER:
        numberOfPlayers = ois.read();
        break;
    case Packet.CS_SEND_TURN:
        gp.addTurn(ois.read(),ois.read(),ois.readByte());
        break;
    }
}

Writing

    public void send(byte value){
    try {
        oos.write(value);
        oos.flush();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
public void send(int value){
    try {
        oos.write(value);
        oos.flush();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
解决方案

just make sure there's no references to those arrays !!!

some of your code will help

这篇关于使用插座时,为了避免垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 06:47