Menu

#16 out of memory error when using tcpserver (xSocket 2.8.13)

closed
grro
core (10)
5
2018-11-19
2010-09-15
armer song
No

i wrote a simple tcpserver. Use a simple tcpclient to send much data( biterate>600 Mbps) to tcpserver. some errors happened. Even though there are 1024 MB memory.
error message:
org.xsocket.connection.AbstractMemoryManager newBuffer
Warn: out of memory exception occured by trying to allocated non-direct memory java.lang.OutOfMemoryError: Java heap space

Since uploading sourcecode to outside server from my company is denied, i paste the codes below:

//TcpServer.java
import org.xsocket.connection.*;
public class TcpServer {

/** Choose your favorite port number. */
private static final int m_listenPort = TcpServerSimple.m_listenPort;

public static void main(String[] args) throws Exception {
// creates the server by passing over the port number & handler
IServer srv = new Server(m_listenPort, new TcpHandler());
// run it within the current thread.
try {
srv.start(); // returns after the server has been started
System.out.println("server" + srv.getLocalAddress() + ":" + m_listenPort);
} catch (Exception e) {
System.out.println(e);
}
}
}

//TcpHandler.java
import java.nio.channels.ClosedChannelException;
import org.xsocket.connection.*;
import org.xsocket.*;
import java.nio.*;
import java.io.*;
import java.util.*;
public class TcpHandler implements IDataHandler, IConnectHandler, IDisconnectHandler {

long m_recvBytes = 0;
ByteBuffer m_buff = ByteBuffer.allocate(Utils.PKGSIZE);

@Override
public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException, ClosedChannelException, MaxReadSizeExceededException {
int len = connection.read(m_buff);
if ((m_recvBytes + len) / Utils.MB != m_recvBytes / Utils.MB) {
System.out.printf("recv %d KB\r", m_recvBytes / Utils.KB);
}
m_recvBytes += len;
return true;
}

@Override
public boolean onConnect(INonBlockingConnection connection) throws IOException, BufferUnderflowException, MaxReadSizeExceededException {
System.out.printf("client connected\n");
return true;
}

@Override
public boolean onDisconnect(INonBlockingConnection connection) throws IOException {
System.out.printf("client disconnected\n");
return true;
}
}

//Utils.java
public class Utils {
public static final int PKGSIZE = 1300;
public static final int KB = 1024;
public static final int MB = 1024 * KB;
}

//TcpClientSimple.java
import java.net.*;
import java.nio.*;
import java.io.*;
public class TcpClientSimple {
public static void main(String[] args) throws Exception {
//handle parameters
int bitRate = 100 * Utils.MB;
String hostIp = "127.0.0.1";
if (args.length >= 1) {
hostIp = args[0];
}
if (args.length >= 2)
{
bitRate = Integer.parseInt(args[1]) * Utils.MB;
}

//convert to Bps
double byteRate = bitRate / 8.0;
System.out.printf("bitrate %d, bytrate %d\n", bitRate, (long) byteRate);

int seqNo = 0;
int curSentBytes = 0;
final int pkgSize = Utils.PKGSIZE;

// TODO Auto-generated method stub
Socket s = new Socket();
s.setSendBufferSize(1 * Utils.MB);
s.connect(new InetSocketAddress(hostIp, TcpServerSimple.m_listenPort));

int sentBytes = 0;
long startTime = System.currentTimeMillis();
double curRate = 0.0;
OutputStream out = s.getOutputStream();
while (sentBytes >= 0) {
ByteBuffer buff = ByteBuffer.allocate(Utils.PKGSIZE);
buff.putInt(0, seqNo++);
out.write( buff.array());
//statisitc
if ((sentBytes + buff.limit() ) / (10*Utils.MB) != sentBytes /(10*Utils.MB) ) {
System.out.printf("sent %d MB, curRate %.3f\r", sentBytes / Utils.MB, curRate * 8 / Utils.MB);
}
sentBytes += buff.limit();
//speed control
long curTime = System.currentTimeMillis();
if (curTime > startTime) {
curRate = sentBytes * 1000.0 / (curTime - startTime);
if (curRate > byteRate) {
Thread.sleep(1);
}
}
}
}
}

Discussion

  • armer song

    armer song - 2010-09-15

    out of memory exception occured by trying to allocated non-direct memory java.lang.OutOfMemoryError: Java heap space
    at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:39)
    at java.nio.ByteBuffer.allocate(ByteBuffer.java:312)
    at org.xsocket.connection.AbstractMemoryManager.newBuffer(AbstractMemoryManager.java:270)
    at org.xsocket.connection.AbstractMemoryManager.newBuffer(AbstractMemoryManager.java:251)
    at org.xsocket.connection.IoUnsynchronizedMemoryManager.preallocate(IoUnsynchronizedMemoryManager.java:132)
    at org.xsocket.connection.IoSocketHandler.checkPreallocatedReadMemory(IoSocketHandler.java:625)
    at org.xsocket.connection.IoSocketHandler.onReadableEvent(IoSocketHandler.java:301)
    at org.xsocket.connection.IoSocketDispatcher.onReadableEvent(IoSocketDispatcher.java:301)
    at org.xsocket.connection.IoSocketDispatcher.handleReadWriteKeys(IoSocketDispatcher.java:278)
    at org.xsocket.connection.IoSocketDispatcher.run(IoSocketDispatcher.java:230)
    at java.lang.Thread.run(Thread.java:619)

     
  • grro

    grro - 2010-09-15

    did you try to set the max/initial heap size of the vm? Further more xsocket supports direct buffer. This will be activated the the system property (default is vm heap memory)

    org.xsocket.connection.client.readbuffer.usedirect=true
    org.xsocket.connection.server.readbuffer.usedirect=true

    Gregor

     
  • grro

    grro - 2010-09-15
    • assigned_to: nobody --> grro
     
  • armer song

    armer song - 2010-09-16

    i adjust heap size of the vm by -Xmx1024m. no improvement.
    i will try : org.xsocket.connection.server.readbuffer.usedirect=true

     
  • armer song

    armer song - 2010-09-16

    sorry, how to config as what you said:
    org.xsocket.connection.client.readbuffer.usedirect=true
    org.xsocket.connection.server.readbuffer.usedirect=true

    i try to change AbstractMemoryManager.newBuffer method and force isUseDirect to true. also error:
    armer@armer-desktop ~/tmp $ java -Xmx1024m -cp .:xSocket-example.jar:xSocket.jar TcpServer
    服务器0:0:0:0:0:0:0:0/0:0:0:0:0:0:0:0:3490
    2010-9-16 9:45:32 org.xsocket.connection.Server$LifeCycleHandler onConnected
    信息: server listening on 0:0:0:0:0:0:0:0:3490 (xSocket 2.8.13)
    client connected
    client connected
    client connected
    2010-9-16 9:45:54 org.xsocket.connection.AbstractMemoryManager newBuffer
    警告: out of memory exception occured by trying to allocated direct memory java.lang.OutOfMemoryError: Direct buffer memory
    at java.nio.Bits.reserveMemory(Bits.java:633)
    at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:95)
    at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:288)
    at org.xsocket.connection.AbstractMemoryManager.newBuffer(AbstractMemoryManager.java:266)
    at org.xsocket.connection.AbstractMemoryManager.newBuffer(AbstractMemoryManager.java:253)
    at org.xsocket.connection.IoUnsynchronizedMemoryManager.preallocate(IoUnsynchronizedMemoryManager.java:132)
    at org.xsocket.connection.IoSocketHandler.checkPreallocatedReadMemory(IoSocketHandler.java:625)
    at org.xsocket.connection.IoSocketHandler.onReadableEvent(IoSocketHandler.java:301)
    at org.xsocket.connection.IoSocketDispatcher.onReadableEvent(IoSocketDispatcher.java:301)
    at org.xsocket.connection.IoSocketDispatcher.handleReadWriteKeys(IoSocketDispatcher.java:278)
    at org.xsocket.connection.IoSocketDispatcher.run(IoSocketDispatcher.java:230)
    at java.lang.Thread.run(Thread.java:619)

    2010-9-16 9:45:54 org.xsocket.connection.AbstractMemoryManager newBuffer
    警告: out of memory exception occured by trying to allocated direct memory java.lang.OutOfMemoryError: Direct buffer memory
    at java.nio.Bits.reserveMemory(Bits.java:633)
    at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:95)
    at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:288)
    at org.xsocket.connection.AbstractMemoryManager.newBuffer(AbstractMemoryManager.java:266)
    at org.xsocket.connection.AbstractMemoryManager.newBuffer(AbstractMemoryManager.java:253)
    at org.xsocket.connection.IoUnsynchronizedMemoryManager.preallocate(IoUnsynchronizedMemoryManager.java:132)
    at org.xsocket.connection.IoSocketHandler.checkPreallocatedReadMemory(IoSocketHandler.java:625)
    at org.xsocket.connection.IoSocketHandler.onReadableEvent(IoSocketHandler.java:301)
    at org.xsocket.connection.IoSocketDispatcher.onReadableEvent(IoSocketDispatcher.java:301)
    at org.xsocket.connection.IoSocketDispatcher.handleReadWriteKeys(IoSocketDispatcher.java:278)
    at org.xsocket.connection.IoSocketDispatcher.run(IoSocketDispatcher.java:230)
    at java.lang.Thread.run(Thread.java:619)

     
  • grro

    grro - 2010-09-17

    Hi,

    have you already anaylze your app for memory leaks? Please use a memory profiler

    Gregor

     
  • grro

    grro - 2010-09-26
    • status: open --> closed
     
  • Pushkar Deole

    Pushkar Deole - 2018-11-19

    Hello,

    can someone update me on the current status of this bug?
    Is this fixed? If yes then in which version the fix for the issue is available ?

     

Log in to post a comment.

MongoDB Logo MongoDB