-1

Friends,

I am building a JAVA TCP listener, where it can handle 6000 incoming request at a time. I am creating a socket connection and accepting data, after accepting data, i am doing some operation over it by creating a thread, but i am not killing this thread, since device will send data in every two minutes, so i am only making thread to sleep mode for 30 seconds.

But after running system for five minute, my application which is running under tomcat6.0 giving error - "The web application appears to have started a thread named [Thread-214] but has failed to stop it. This is very likely to create a memory leak."

Please help me to understand where i am doing wrong?

Thanks in advance.

geekIndiana
  • 87
  • 4
  • 14
  • possible duplicate of [Memory Leak in Tomcat 6.0 application](http://stackoverflow.com/questions/9753120/memory-leak-in-tomcat-6-0-application) – duffymo Mar 18 '12 at 14:45
  • 1
    Tomcat is a servlet container and is used to handle requests and threads on its own. You are using it in the wrong way. What you are trying to achieve should be done in a standalone app. As commented in the other question, you are keeping the thread alive only so the user can come back and use the same thread. Instead of this, use some in-memory cache or persistence. Your overall design is flawed. – Sean Mar 18 '12 at 15:16
  • Post the code you are using to create and start the thread. – Java42 Mar 18 '12 at 15:18
  • @Sean, I am using tomcat because i have created my TCP listner as ServletContextListner, so once my tomcat starts , listener class start, but my tomcat does not reach in startup mode, since my TCP listener is active and waiting for incoming request coming from Device. thanks – geekIndiana Mar 18 '12 at 15:42

5 Answers5

2

If you have many sockets, instead of using thread per channel. Try using One thread that go over all sockets. look at the Java Selector http://www.exampledepot.com/egs/java.nio/NbClient.html

Horonchik
  • 477
  • 2
  • 11
  • Could you please explain how could this link will useful for me? – geekIndiana Mar 18 '12 at 15:09
  • In your case you need to handle many sockets that are in idle state most of the time. If you will give each socket you will ran out of memory very quickly. The selector give you an iterator that in an infinite loop you receive events on sockets that want to perform an action (send data every 2 minutes). you can read the data and act accordingly. – Horonchik Mar 18 '12 at 19:33
1

You should be aware that your operating system can not handle that much threads. Moreover, memory is allocated for each thread, so you will fill up your heap very quickly.

As I don't know what your trying to achieve, I am only guessing that you have a design flaw in your application, usually threads are reused to handle requests.

remi dupuis
  • 119
  • 3
  • +1 - " I am only guessing that you have a design flaw in your application, usually threads are reused to handle requests." this is absolutely correct. – duffymo Mar 18 '12 at 15:12
  • @Remi, I know the basic rule and i know the advantage of thread re-useability, But as i discussed in my scenario that i don not want to lose connection with GPS device which is in vehicle and vehicle is running in field, so loosing connection and again creating an connection, is difficult. Hence i am creating a separate thread to make a connection and keep alive always and reading data using same thread. Tell me where i am wrong? Thanks – geekIndiana Mar 18 '12 at 15:36
  • I don't see why it is necessary to create a thread for each connection, can't you have one heartbeat-thread that handle a list of connection ? As Sean said are you sure you need Tomcat ? – remi dupuis Mar 18 '12 at 17:21
0

I think Selector may help. You might want to read a short introduction about Selector in this link http://tutorials.jenkov.com/java-nio/selectors.html "A Selector is a Java NIO component which can examine one or more NIO Channel's, and determine which channels are ready for e.g. reading or writing. This way a single thread can manage multiple channels, and thus multiple network connections."

Thang Mai
  • 314
  • 3
  • 10
0

If you need to handle many TCP connections with Java - you should use NIO. But programming bare NIO (Selector) is hard - so use Netty, it's designed specially for such tasks. Also Netty works fine inside Tomcat.

alexkasko
  • 4,855
  • 1
  • 26
  • 31
-2

may be you should use thread pool

http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html

Perry Monschau
  • 883
  • 8
  • 22