I am running a server, and I have an arraylist of clients.

When a client connects, I create a new object in the arraylist(all running on their own threads)

When a client disconnects, the server closes the Socket. And the thread terminates

My problem is when a client disconnects, it closes the Socket, but it doesn't delete its spot in the arraylist, and shift everything down one. So the arraylist is constantly growing bigger.

What can I do/use to fix this problem?

I will update with more detailed information/code if need be.

  • \$\begingroup\$ Depends on what you use your list for. If it is just for iterating through the clients you can use a linked list. If it is possible for you can also consider using just threading. \$\endgroup\$
    – floAr
    Commented Oct 13, 2013 at 9:25
  • \$\begingroup\$ Could you go a little more in depth with what you mean by threading? Currently the Arraylist has client objects that are all running on their own thread (for each client) \$\endgroup\$
    – Loligans
    Commented Oct 13, 2013 at 23:42
  • \$\begingroup\$ Yes, why are you keeping the clients stored in an array list? Are you performing some operations on them? If you keep them in the list without actually doing something you could rely only on threading .So you create a thread when a new client connects and after disconnecting you clean up and stop / dispose the thread. \$\endgroup\$
    – floAr
    Commented Oct 14, 2013 at 6:00
  • \$\begingroup\$ How about removing client from the list? Btw I dont know if I understand what are you doing with threads, but don't forget that you can't modify ArrayList from different threads without some sort of locking. \$\endgroup\$
    – Kikaimaru
    Commented Oct 14, 2013 at 8:16

2 Answers 2


You should catch the close event ( it is trivial to do in the client thread (that lies on the server) that is listening from the client ) and remove the client from the ArrayList. You could also use a LinkedList in order to make the removal of a client faster.


Weather you can "simply" delete your record in the connection "array-list" depends on how you use it. Especially, if you store and use the index to connections in the array-list somewhere, you must make sure to update them too. Those will change if an item is removed from the list.

Instead of referencing by index you should reference by a direct reference to the connection thread object. But you should implement a mechanism to tell holders of a reference, when it's getting invalid... You could use a callback function for this purpose, that must be given when acquiring a reference to the connection. This callback is then stored in a list inside the connection thread object and the callbacks of all reference owners is called, when the connection closes. This way all objects can safely remove the reference and react accordingly, before the connection reference is finally removed from the connection "array-list".

Note, that we cannot give you more detailed answers until you give us more detailed information about your problem.


Not the answer you're looking for? Browse other questions tagged .