java.lang.OutOfMemoryError:unable to create native thread

来源:百度文库 编辑:神马文学网 时间:2024/06/12 23:58:56

Problem
The java.lang.OutOfMemoryError is sometimes used by Java? to indicate that a resource is constrained. It does not always mean that the Java heap is unable to fulfill a heap allocation request.

The following java.lang.OutOfMemoryError is issued by Java because it cannot create a new Java thread as requested.

[14/03/03 14:55:55:508 GMT-07:00] 5062d9 WebGroup X Servlet Error: unable to create new native thread: java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start(Native Method)
at com.sun.jndi.ldap.Connection.(Connection.java:240)
at com.sun.jndi.ldap.LdapClient.(LdapClient.java:113)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2307)
at com.sun.jndi.ldap.LdapCtx.(LdapCtx.java:211)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:79)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:668)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:246)
at javax.naming.InitialContext.init(InitialContext.java:222)
at javax.naming.InitialContext.(InitialContext.java:198)
at javax.naming.directory.InitialDirContext.(InitialDirContext.java:83)
at com.com.utilities.ldap.LDAPManipulate.getContext(LDAPManipulate.java:393)

When this error occurs, the verbose garbage collection (GC) data does not likely indicate a memory leak is occurring. It might show that there is plenty of heap memory available.
 
Cause
In this case, the java.lang.OutOfMemoryError is caused by excessive threads being created. There are various reasons why the number of threads is excessive. Here are some possible causes for this exception:
 
Solution
There are a few things to do if you encounter this exception.
Use the lsof -p PID command (Unix? platforms) to see how many threads are active for this process.


Determine if there is a maximum number of threads per process defined by the operating system. If the limit is too low for the application, try raising the per-process thread limit.


Examine the application code to determine if there is code that is creating threads or connections (such as LDAP connections) and not destroying them. You could dump the Java? threads to see if there are an excessive number has been created.


If you find that too many connections are opened by the application, make sure that any thread that the application creates is destroyed.

An enterprise application (.ear) or Web application (.war) runs under a long-running JVM?. Just because the application is finished does not mean that the JVM process ends. It is imperative that an application free any resources that it allocates.

Another solution would be for the application to use a thread pool to manage the threads needed.