我正在使用netty进行项目的数据传输。这部分项目基本上是发送和接收数据包。每个数据包基本上都是一个封装一些元数据和实际文件块的对象。当我尝试传输大文件时,比如说5GB,不知何故,内存使用率会上升,并且在传输文件后不会释放。对于5gb文件,内存使用量从~1gb开始,最高可达15GB,并保持不变直到我退出程序。
这是我的包对象:
public class BlockPacket
{
private String requestId;
private String blockId;
private int packetSeqNo=0;
private byte[] data;
public BlockPacket(){
}
public String toString()
{
return "BlockPacket [requestId=" + requestId + ", blockId=" + blockId + ", packetSeqNo=" + packetSeqNo + "]";
}
public String getRequestId()
{
return requestId;
}
public void setRequestId(String requestId)
{
this.requestId = requestId;
}
public String getBlockId()
{
return blockId;
}
public void setBlockId(String blockId)
{
this.blockId = blockId;
}
public int getPacketSeqNo()
{
return packetSeqNo;
}
public void setPacketSeqNo(int no)
{
this.packetSeqNo = no;
}
public byte[] getData()
{
return data;
}
public void setData(byte[] data)
{
this.data = data;
}
}
这是ServerHandler上的channelRead
@Override
public void channelRead(ChannelHandlerContext ctx, Object obj)
{
if (obj instanceof BlockPacket)
{
BlockPacket packet = (BlockPacket) obj;
//Do something with packet
}else if (obj instanceof Block){
Block block = (Block) obj;
//Do something with block
}else{
//Do something else
}
}
这是我的自定义自定义编码和解码
@Override
protected void encode(ChannelHandlerContext ctx, Object in, ByteBuf out) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
Output output = new Output(outStream);
kryo.writeClassAndObject(output, in);
output.flush();
byte[] outArray = outStream.toByteArray();
out.writeInt(outArray.length);
out.writeBytes(outArray);
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() < 4)
return;
in.markReaderIndex();
boolean read = false;
if(len == 0){
read = true;
len = in.readInt();
}
if ((in.readableBytes() < len && read) || (in.readableBytes() < len+4 && !read)) {
in.resetReaderIndex();
read = false;
return;
}
if (!read)
in.readInt();
byte[] buf = new byte[len];
in.readBytes(buf);
Input input = new Input(buf);
Object object = kryo.readClassAndObject(input);
out.add(object);
}
我经历了Reference counted object,但我无法解决我的问题。本教程中的示例通常用于ByteBuf,它具有release()方法。但是,我的Packet对象是一个简单的Java对象。任何帮助表示赞赏。