Web应用程序似乎已启动一个名为但未能停止的线程。这很可能造成内存泄漏

时间:2017-07-20 10:58:36

标签: java multithreading tomcat twitter web-applications

我在java中使用hosebird客户端使用twitter streaming api获取实时推文。

运行它几个小时后给出以下异常。

18-Jul-2017 15:35:50.034 WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [hosebird-client-io-thread-0] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.net.SocketInputStream.socketRead0(Native Method)
 java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
 java.net.SocketInputStream.read(SocketInputStream.java:171)
 java.net.SocketInputStream.read(SocketInputStream.java:141)
 sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
 sun.security.ssl.InputRecord.read(InputRecord.java:503)
 sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
 sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:940)
 sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
 org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:166)
 org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:90)
 org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:281)
 org.apache.http.impl.conn.LoggingSessionInputBuffer.readLine(LoggingSessionInputBuffer.java:115)
 org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:251)
 org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:209)
 org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:171)
 org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:138)
 java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:238)
 java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
 java.util.zip.GZIPInputStream.read(GZIPInputStream.java:117)
 com.twitter.hbc.common.DelimitedStreamReader.readLine(DelimitedStreamReader.java:72)
 com.twitter.hbc.common.DelimitedStreamReader.readLine(DelimitedStreamReader.java:56)
 com.twitter.hbc.core.processor.StringDelimitedProcessor.processNextMessage(StringDelimitedProcessor.java:53)
 com.twitter.hbc.core.processor.StringDelimitedProcessor.processNextMessage(StringDelimitedProcessor.java:26)
 com.twitter.hbc.core.processor.AbstractProcessor.process(AbstractProcessor.java:46)
 com.twitter.hbc.httpclient.Connection.processResponse(Connection.java:51)
 com.twitter.hbc.httpclient.ClientBase.processConnectionData(ClientBase.java:244)
 com.twitter.hbc.httpclient.ClientBase.run(ClientBase.java:144)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 java.lang.Thread.run(Thread.java:748)

我试图将hosebird-client-thread更改为守护程序线程here,但仍然没有运气。

public static Thread getThreadByName(String threadName) {
    for (Thread t : Thread.getAllStackTraces().keySet()) {
        System.out.println("Thread is: "+ t.getName());
        if (t.getName().contains(threadName)) return t;
    }
    return null;
}

getThreadByName("hosebird-client-io-thread").setDaemon(true);

任何领导都会非常感激!

1 个答案:

答案 0 :(得分:0)

通过在线程不再需要时停止并重新启动来修复线程的内存泄漏。

if(hosebirdClient.isDone())
        {
            hosebirdClient.stop();
            updateSystmeParameters(0, getCurrentTimeStamp(), "NA");
            Search.startFirehoseFilter(); // this function restart the thread 

        }