I’m trying to build a simple forwarding server in java that allocates only a minimal amount of memory and hits gc rarely if ever. I can use preallocated buffers to copy the data to and from the output and input streams, but each new connection seems to allocate around 10k of memory. Is there a simple way to reuse Socket objects after their connections are closed to avoid this (this is assuming Sockets do not internally need to allocate additional memory if they are reused)?
To clarify, by ‘Socket’ I am referring to the java class, not the generic notion of a socket. And by ‘reuse’, I mean that I want to reuse the memory that was allocated by the object to avoid having to reclaim that memory through garbage collection. The motivation for this is to create a simple, highly-responsive server that will not need to pause for garbage collection (I know I could use C/C++ or a proprietary JVM, but I’m trying to explore this solution as a possibility).
No, a Socket cannot be reused in Java once it is closed.