0

I have a server written in jav running at 100MB of RAM or so on startup(info gotten from viewing the process explorer). However, when a client connects to it to transfer a big file, the RAM usage goes up until around 160MB or so and I got the error below. I have tried setting -Xmx128m, -Xmx256m, -Xmx512m, -Xmx1024m to no avail. What should I do to prevent this from happening. This only happens when a client tries to get a big file from the server

java.lang.OutOfMemoryError: unable to create new native thread
java.lang.Thread.start0(Native Method)
java.lang.Thread.start(Unknown Source)
com.sun.jndi.ldap.Connection.<init>(Unknown Source)
com.sun.jndi.ldap.LdapClient.<init>(Unknown Source)
com.sun.jndi.ldap.LdapClient.getInstance(Unknown Source)
com.sun.jndi.ldap.LdapCtx.connect(Unknown Source)
com.sun.jndi.ldap.LdapCtx.<init>(Unknown Source)
com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(Unknown Source)
com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(Unknown Source)
com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(Unknown Source)
com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(Unknown Source)
javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
javax.naming.InitialContext.init(Unknown Source)
javax.naming.ldap.InitialLdapContext.<init>(Unknown Source)
shawn
  • 4,063
  • 7
  • 37
  • 54
  • possible duplicate of [Java: Unable to create new native thread](http://stackoverflow.com/questions/5253072/java-unable-to-create-new-native-thread) – NPE May 03 '12 at 09:58
  • possible duplicate of [java.lang.OutOfMemoryError: unable to create new native thread](http://stackoverflow.com/questions/3500773/java-lang-outofmemoryerror-unable-to-create-new-native-thread) – assylias May 03 '12 at 09:58
  • it seems to me that yuo are setting -Xmx in wrong place, if -Xmx1024M is set, RAM usage grows over let say 700M ? – Betlista May 03 '12 at 09:59
  • can you give some more statistics, like how big is the file? how much is the ram available on server (total and free)? how often to you request that file? and does it fail the first time you request the file? – Jo Dar Gaya Woh Mar Gaya May 03 '12 at 09:59
  • More information is required. For example, how large is the file? How many concurrent connections? It's even better if you can show some of the server code (only the file transfer) part. – James Gan May 03 '12 at 09:59
  • Fails the first time using only 1 connection, its a VM with 2GB RAM, Free RAM at time of failed run is around 1GB – shawn May 03 '12 at 10:03
  • 1
    can you add the code that shows how you are reading the file? possibly you are reading a lot more bytes that you should so rather then streaming the file it sends the whole lot together. – Jo Dar Gaya Woh Mar Gaya May 03 '12 at 10:06
  • Yes, it sends the whole lot together, I have profiled it and it seems to create a whole lot of threads when the file is generated from the connection to ldap, does java do that? – shawn May 03 '12 at 10:28

1 Answers1

2

The problem is that you are unable to create another thread. Often this is because you cannot allocate more stack space, but there is often an OS limitation in 32-bit OSes which set this limit lower.

How many threads do you have when you get this error?

You can print

System.out.println("thread.count=" + Thread.getAllStackTraces().size());

BTW: Increasing the heap size can reduce the amount of virtual memory free to create stack space for new threads. (This is only a problem on 32-bit JVMs) This doesn't appear to be your problem, but it can be surprising that in this case, the first thing to try is to reduce your heap space.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130