Memory increases with Java UDP Server
Posted
by Trevor
on Stack Overflow
See other posts from Stack Overflow
or by Trevor
Published on 2010-06-08T19:15:10Z
Indexed on
2010/06/08
19:22 UTC
Read the original article
Hit count: 174
I have a simple UDP server that creates a new thread for processing incoming data. While testing it by sending about 100 packets/second I notice that it's memory usage continues to increase. Is there any leak evident from my code below?
Here is the code for the server.
public class UDPServer
{
public static void main(String[] args)
{
UDPServer server = new UDPServer(15001);
server.start();
}
private int port;
public UDPServer(int port)
{
this.port = port;
}
public void start()
{
try
{
DatagramSocket ss = new DatagramSocket(this.port);
while(true)
{
byte[] data = new byte[1412];
DatagramPacket receivePacket = new DatagramPacket(data, data.length);
ss.receive(receivePacket);
new DataHandler(receivePacket.getData()).start();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Here is the code for the new thread that processes the data. For now, the run() method doesn't do anything.
public class DataHandler extends Thread
{
private byte[] data;
public DataHandler(byte[] data)
{
this.data = data;
}
@Override
public void run()
{
System.out.println("run");
}
}
© Stack Overflow or respective owner