0

I am working with socket application. I have a JAVA TCP listener as a part of web application. I have defined my TCP Listener as a servlet context listener inside web.xml, so once I start my tomcat my TCP listener become active and wait for incoming request coming from GPS device.

Once my tomcat6.0 starts then TCP listener is able to read all incoming request but, just after 5 minute, I get an error like this "The web application appears to have started a thread named but has failed to stop it. This is very likely to create a memory leak.".

Please help me how to fix this issue. As I am creating simultaneously many threads to handle the incoming request since we have 10K GPS device which communicates with my TCP listener. We have business reason to create a single thread for single device and process incoming request.

Please help me to figure out actual issue.

Thanks in advance.

dgw
  • 13,418
  • 11
  • 56
  • 54
geekIndiana
  • 87
  • 4
  • 14
  • Dear Irfy, please help me , i don't know from where i must go and accept the answer, i am not finding the link, would you please help me. thanks – geekIndiana Mar 17 '12 at 19:57

2 Answers2

2
The web application appears to have started a thread named but has failed to stop it.

Please help me how to fix this issue.

Well, maybe stop that thread? Looks like your error message is incomplete, it typically has a thread name (or maybe your threads have empty ("") name?

This error in Tomcat is not critical, but you should investigate why it appears, have a look at: Is this very likely to create a memory leak in Tomcat? and especially at Tomcat 6 memory leaks log entries which targets threads.

In order to stop a thread check out: How to stop a thread that is running forever without any use.

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Dear Tomasz, I am getting an error like this "The web application appears to have started a thread named [Thread-1302] but has failed to stop it." As i told you i need to create separate thread for each device. So i am having more than 10K threads running. Please help me what would be the issue. thanks – geekIndiana Mar 17 '12 at 20:03
  • @user405575: the total number of threads does not matter. The errors says: *you have started a thread, please stop it (interrupt it) before shutting down the application*. I don't know how are you creating these threads, but you must shut them down gracefully. – Tomasz Nurkiewicz Mar 17 '12 at 20:05
  • Dear Tomasz, I can not stop the thread, i want to keep alive, since once connection established with GPS device, device will keep sending data on same thread. This is the reason that i have all 10K active threads. Please suggest me the other way. thanks – geekIndiana Mar 17 '12 at 20:10
  • @user405575 I am not telling you to stop these threads while the application is running. I am saying you should stop them when application is undeployed/shut down. This error only appears in Tomcat during shutdown/redeployment. I have never seen it popping up while the application is running. Are you sure you aren't redeploying or the application isn't dying silently? – Tomasz Nurkiewicz Mar 17 '12 at 20:18
  • No Tomasz, my tomcat never reach on startup, since i have written my TCP listener as servlet context listener and it's run once my tomcat starts, Once my TCP up, it waits for incoming request and my code is written inside while(true), so tomcat never reach to start mode. – geekIndiana Mar 17 '12 at 20:23
  • @user405575: and are you sure [`ServletContextListener.contextDestroyed()`](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html#contextDestroyed(javax.servlet.ServletContextEvent)) is not called by the container for some reason? – Tomasz Nurkiewicz Mar 17 '12 at 20:31
  • No contextDestroyed() is not getting call. It is running fine for just 5 minutes after that errors starts- which i mentioned- and my code come out from while(true) code, where i am creating threads, once it comes out, my tomcat shows tomcat starts with 212353ms. It should never come out from while loop. – geekIndiana Mar 17 '12 at 20:37
  • @user405575: please examine all log files under `/logs` subdirectory in Tomcat and console output. I am pretty sure some exception is being thrown from your loop. Also surround your `while(true)` loop with `try/catch` and log exception. I am 99% sure your application throws some exception killing the whole web-app but it gets swallowed. Finally, you aren't performing the blocking `while(true)` directly in `contextInitialized()`? This method should return fairly fast and certainly not block. – Tomasz Nurkiewicz Mar 17 '12 at 20:42
  • Never? Not even when the device is no longer connected? Can the GPS issues a "disconnect" request? Is there a timeout? – duffymo Mar 17 '12 at 20:43
  • Let me check and update you in morning (According to Indian TIme), I will do exactly suggested by you, please help me to solve this issue. thanks – geekIndiana Mar 17 '12 at 20:44
  • @ duffymo, GPS device can not issue disconnect and no timeout. That's why i am using sleep method and wait for next packet to come from same device instead of killing threads, since i want to connect with device always via same thread. – geekIndiana Mar 17 '12 at 20:48
  • Your way of thinking guarantees that you'll always run out of memory, because nothing ever disconnects or goes away. This is not how servers act. You need to do a better job of understanding your requirements. I'm voting to close this. It's not appropriate for SO. You need a consultant, not a Q&A forum. – duffymo Mar 18 '12 at 00:00
0

I'll assume that you've checked the JVM settings with which you start Tomcat. Maybe your memory is too low.

I'd recommend that you download Visual VM 1.3.3, install all the plugins, start your Tomcat application, and attached Visual VM to the JVM. It'll show you what's happening in memory, CPU, objects created, threads, etc.

You showed no code, and you have no data. You'll never solve this problem this way. No one here can help you much, either. Your code is creating too many objects. You need to find out why and stop it.

We have business reason to create a single thread for single device and process incoming request.

You might have a business reason, but your technical reasons might not be sound. Each HTTP request that comes in has a single thread. Isn't that what you want? When a GPS posts a request you have a single thread to handle it, right? Why must you create many threads with each request? What are those threads doing?

Another approach might be to refrain from creating threads. When a request comes in from a device, find some unique tag like a device id and add that message to the session history for that device. I haven't heard any justification for creating threads so far.

I don't know if you're taken the trouble to write your own server, but I'd recommend that you look into using Netty. It's an all-Java server based on non-blocking IO that's born to handle high traffic. They know how to handle threads. Maybe you can just use it and add your processing logic to it.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Dear duffymo, GPS device communicates through internet with our TCP listener and it contains data packet in all request, service provider charges much for internet uses, hence we have individual thread for each device, so it does not take much time to create again and again connection with device. Suppose if i change my perm-gen to 1 GB, will it work? – geekIndiana Mar 17 '12 at 20:08
  • It won't work if you continue to play fast and loose with resources and memory. It might just take 20 minutes to shut down instead of 5. Your implementation is seriously flawed. I'd rethink it. – duffymo Mar 17 '12 at 20:10
  • Can i use sleep method to make my thread sleep, so it won't use more resources and my connection with thread will also good. actually i don not want to loose connection with my device once it gets connected, since device keep sending data in every minute and i can not create a thread and stop thread in every minute, please suggest me. thanks – geekIndiana Mar 17 '12 at 20:14
  • 2
    I would suggest that you get somebody who knows how to write a server and multi-threaded code to do it for you. If you have to ask here, you shouldn't be doing it. – duffymo Mar 17 '12 at 20:16
  • You realize, of course, that Tomcat is a servlet/JSP engine, and HTTP servlets are HTTP listeners. Are those GPS devices making HTTP GET/POST requests to your listener? You keep saying TCP. – duffymo Mar 17 '12 at 20:40
  • I used tomcat to only run my TCP listener class, since i dont want to run using commond prompt – geekIndiana Mar 17 '12 at 20:52
  • Dear Duffymo, at least you could help me to design a server in a such way where i can able to handle many requests, or at least tell me how could netty will be useful for me. Thanks for listening me patiently , Please help me. thanks – geekIndiana Mar 18 '12 at 05:21
  • No thanks, this is not a consulting or design service. It's Q&A. Do some research on Netty and see where it leads you. – duffymo Mar 18 '12 at 12:53