8

I have a java app deployed on tomcat 7 running on ubuntu 10.04. There's been an issue during opening a server socket which I couldn't reproduce so far:

java.net.SocketException: Cannot allocate memory
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at org.subethamail.smtp.server.SMTPServer.createServerSocket(SMTPServer.java:338)
at org.subethamail.smtp.server.SMTPServer.start(SMTPServer.java:291)

All I've been able to find out is that this happens on some specific version of MacOS which is not relevant for me, and also on OpenJDK, which is not relevant either (I'm using Oracle JRE 1.7.0_17). Another possible reason is a virtualization environment, but in my case this happens on a hardware box.

So, the question is, has anyone ever faced the same problem and what could be a possible solution.

Update There's been also this thing: tomcat consumed almost all of the heap, approximately 700mb, it's been caused by a memory leak in my code. But as far as I understand, the exception tells about a socket buffer on system level, so it doesn't seem to be related to java heap. However, this is the only explanation I've got so far and it's very illusive in my opinion.

Update 2 Eventually we've been able to reproduce the issue several times, so this was not about memory leaks. I was considering authbind as a possible source of the problem when I faced it for the first time, but unfortunately I haven't paid much attention to it. When I got another hardware box affected by the problem, I tried to bind non-priveleged port and succeeded, while attempts to bind priveleged ports lead to exceptions. So, eventually I've replaced authbind with iptables.

Basically, fady taher's answer points to authbind, but Danny Thomas's answer provides very interresting information about connections between forking and "Cannot allocate memory", actually we also use process builder to run bash scripts, so there is a great chance that the problem could be caused by it.

6
  • Is it a 32bit or 64bit JDK? Commented Jul 25, 2013 at 10:23
  • It's a 32bit jre (not jdk, my fault) Commented Jul 25, 2013 at 11:22
  • And have you tried a 64bit JRE or JDK insteadN Commented Jul 25, 2013 at 11:50
  • Unfortunately I'm not able to, 64 bit JRE doesn't contain sun security classes that I'm using Commented Jul 25, 2013 at 11:58
  • And even if I tried it wouldn't prove anything because I cannot reproduce the issue) It happened just once on integration environment. Eventually I marked the issue as Resolved with the explanation mentioned above (exhausted heap), but I wouldn't say that I'm completely sure) Commented Jul 25, 2013 at 12:00

5 Answers 5

4
+100

Sounds like you have insufficient physical memory or swap - on the systems affected, check memory and swap.

Does your application happen to execute external commands - fork/exec could be contributing. You might consider allowing memory overcommit, if that's the case:

http://bryanmarty.com/blog/2012/01/14/forking-jvm/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Danny Thomas. It turned out that authbind we're using is also performing fork() under the hood. On memory usage peaks rebind can cause the error described above, although heap is not fully exhausted yet. So we got rid of authbind and the problem is solved.
1

Please also check the following items:

  • Run a memory test to eliminate bad memory blocks
  • Run disk check on swap partition (the equivalent of which on Mac OS)
  • Check user resource limits (ulimit)

2 Comments

thanks for response. This issue is observed on multiple hardware installations (at least 10), so I doubt all of them has their ram or disc broken
@JK1: You would be amazed at the kinds of hardware failure coincidences you could have...
0

dunno if it helps, but check

Memory error when trying to change Apache Tomcat port from 8080 to 80

Comments

0
  • You can extend java heap space check for it.
  • If the project you are developing has been created by another java version, that issue might happen.

Comments

0

Try reducing the memory allocated to Tomcat (the -Xmx parameter in catalina.sh). Also increase the maximum heap size of tomcat. If it doesn’t solve it, you have to find memory leaks in your code, one such tool is java melody , use it and find the memory leakage to solve the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.