1. 程式人生 > >java.lang.OutOfMemoryError: unable to create new native thread解決方法

java.lang.OutOfMemoryError: unable to create new native thread解決方法

我們使用Mysql,偶爾會出現OutOfMemoryError,這時候jvm堆中還有很多記憶體,下面是出錯的堆疊資訊:

java.lang.OutOfMemoryError: unable to create newnative thread
    at java.lang.Thread.start0(NativeMethod)
    at java.lang.Thread.start(Thread.java:597)
    at com.microsoft.sqlserver.jdbc.TimeoutTimer.start(UnknownSource)
    at com.microsoft
.sqlserver.jdbc.TDSCommand.startResponse(UnknownSource)     at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(UnknownSource)     at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(UnknownSource)     at com.microsoft.sqlserver.jdbc
.TDSCommand.execute(UnknownSource)     at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(UnknownSource)     at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(UnknownSource)     at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(UnknownSource)     at com
.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(UnknownSource)     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)     at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)     at org.hibernate.loader.Loader.getResultSet(Loader.java:1808)     at org.hibernate.loader.Loader.doQuery(Loader.java:697)     at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)     at org.hibernate.loader.Loader.doList(Loader.java:2228)     at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)     at org.hibernate.loader.Loader.list(Loader.java:2120)     at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)     at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1596)     at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)

解決方案:

sqljdbc在setQueryTimeout時會導致executeQuery開啟一個新的執行緒。這會導致產生大量的執行緒。這些執行緒需要的堆疊空間都不會太大,這種情況下你可以修改jvm引數降低每個執行緒的堆疊記憶體,來避免這個問題。

例如在虛擬機器引數中新增:-Xss128k來將堆疊記憶體設定為128K(預設是1M)。