I'm hoping to improve performance by improving small lags caused by garbage collection while receiving packets. I have a simple DatagramSocket
that receives data like so:
DatagramSocket sock;
try {
sock = new DatagramSocket(port);
sock.setReuseAddress(true);
} catch (SocketException e) { }
DatagramPacket pack = null;
byte[] buff = new byte[SIZE];
while(!sock.isClosed()){
try{
pack = new DatagramPacket(buff, buff.length());
sock.receive(pack);
} catch (IOException e) { }
}
The code above works well except that there ends up being a lot of calls to collect garbage (as seen in my LogCat viewer). I know that in order to avoid garbage collection you want to try not to allocate memory, but does anyone see a good way to do that with the above code and still achieve the same results?
I looked at this thread and this thread but am not sure how they would map to my particular situation. I also saw this suggestion to call GC but in this case GC is already being executed many times in the interim.
Any suggestions would be greatly appreciated!