java.io.IOException: Cannot allocate memory

I am running jboss 4.0.2 with two applications deployed. I have manually removed services that are not needed to save memory.

The offending code:

String command =new String("/usr/local/bin/executeCreateMailDir " + ULV.getLoginId());

try{

Runtime.getRuntime().exec(command);

}catch (IOException e){

System.out.println(Runtime.getRuntime().freeMemory());

System.out.println(Runtime.getRuntime().totalMemory());

thrownew CreateException(e.getMessage());

}

This is nothing more than a shell script which creates and sets permissions on a couple directories and runs courier's maildirmake. This code will throw the exeception every time with -Xms128m -Xmx128m. If, however, I set it to -Xms135m -Xmx135m. It will take a day or two before throwing it. The output of those two println's with Xmx128m is:

2005-09-20 22:35:16,812 INFO [STDOUT] 91970128

2005-09-20 22:35:16,812 INFO [STDOUT] 132775936

Here's the output of free immediately after the exception is thrown:

totalusedfreesharedbufferscached

Mem:1912361881003136 0125248524

-/+ buffers/cache:13832452912

Swap:13106424568106496

Any ideas?

[1507 byte] By [kbogerta] at [2007-10-2 0:22:30]
# 1
Another interesting fact, setting -Xms96m -Xmx96m and the code works too. Memory use reported by free: totalusedfreesharedbufferscachedMem:1912361885562680 0154052396-/+ buffers/cache:13462056616Swap:13106424472106592
kbogerta at 2007-7-15 16:28:42 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

If it is a shell script, you could run it with

{"sh" , "-c" . ""/usr/local/bin/executeCreateMailDir", ULV.getLoginId() }

The stdout and stderr should be read.

BIJ001a at 2007-7-15 16:28:42 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

Ok, now with the new code: (couldn't get the array to work, script wasn't taking the param)

String command = new String("/usr/local/bin/executeCreateMailDir " + ULV.getLoginId());

try {

Process p = Runtime.getRuntime().exec(command);

try {

p.waitFor();

} catch (InterruptedException e) {

return;

}

new BufferedInputStream(p.getErrorStream()).close();

new BufferedInputStream(p.getInputStream()).close();

} catch (IOException e) {

System.out.println(Runtime.getRuntime().freeMemory());

System.out.println(Runtime.getRuntime().totalMemory());

throw new CreateException(e.getMessage());

}

with:

-Xms127m -Xmx127m- Works

-Xms128m -Xmx128m- Exception 2nd time executed

-Xms129m -Xmx129m- Works

?

kbogerta at 2007-7-15 16:28:42 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

After reading this article: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

I came up with the following code:

String [] command = new String [] {"/usr/bin/sudo", "-S", "/usr/local/bin/createMaildir", ULV.getLoginId()};

Process p = null;

try {

p = Runtime.getRuntime().exec(command);

p.getOutputStream().flush(); // don't need to send anything to stdin

p.getOutputStream().close();

StringBuffer stderr = new StringBuffer();

StreamGobbler errorGobbler = new

StreamGobbler(p.getErrorStream(), stderr);

StringBuffer stdout = new StringBuffer();

StreamGobbler outputGobbler = new

StreamGobbler(p.getInputStream(), stdout);

errorGobbler.start();

outputGobbler.start();

try {

p.waitFor();

} catch (InterruptedException e) {

Logger logger = Logger.getLogger(UserBean.class);

logger.error("Interrupted while waiting for createMailDir script to finish", e);

p.destroy();

}

if (stderr.toString().length() > 0) { // log stderr

Logger logger = Logger.getLogger(UserBean.class);

logger.error(stderr);

}

if (stdout.toString().length() > 0) { // log stdout

Logger logger = Logger.getLogger(UserBean.class);

logger.debug(stdout);

}

} catch (IOException e) {

if (p != null)

p.destroy();

throw new CreateException(e.getMessage());// This is line 210

}

}

class StreamGobbler extends Thread

{

InputStream is;

StringBuffer buffer;

StreamGobbler(InputStream theIs, StringBuffer theBuffer)

{

is = theIs;

buffer = theBuffer;

}

public void run()

{

try

{

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String line=null;

while ( (line = br.readLine()) != null)

buffer.append(line).append("\n");

}

catch (IOException ioe)

{

// do nothing

}

}

}

Once again, -Xmx129m works fine, -Xmx128m causes the following error (first try too)

javax.ejb.CreateException: java.io.IOException: Cannot allocate memory

at com.rad300.ejb.cmp.UserBean.ejbPostCreate(UserBean.java:210)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:585)

at org.jboss.ejb.plugins.CMPPersistenceManager.postCreateEntity(CMPPersistenceManager.java:251)

at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.postCreateEntity(CachedConnectionIntercepto r.java:274)

at org.jboss.ejb.EntityContainer.postCreateLocalHome(EntityContainer.java:625)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:585)

at org.jboss.invocation.Invocation.performCall(Invocation.java:345)

at org.jboss.ejb.EntityContainer$ContainerInterceptor.invoke(EntityContainer.java:1159)

at org.jboss.ejb.plugins.cmp.jdbc.JDBCRelationInterceptor.invoke(JDBCRelationInterceptor.java:72)

at org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invoke(EntitySynchronizationInterceptor.java:273)

at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185 )

at org.jboss.ejb.plugins.EntityReentranceInterceptor.invoke(EntityReentranceInterceptor.java:111)

at org.jboss.ejb.plugins.EntityInstanceInterceptor.invoke(EntityInstanceInterceptor.java:242)

at org.jboss.ejb.plugins.EntityLockInterceptor.invoke(EntityLockInterceptor.java:89)

at org.jboss.ejb.plugins.EntityCreationInterceptor.invokeHome(EntityCreationInterceptor.java:43)

at org.jboss.ejb.plugins.CallValidationInterceptor.invokeHome(CallValidationInterceptor.java:41)

at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:109)

at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:335)

at org.jboss.ejb.plugins.TxInterceptorCMT.invokeHome(TxInterceptorCMT.java:146)

at org.jboss.ejb.plugins.SecurityInterceptor.invokeHome(SecurityInterceptor.java:116)

at org.jboss.ejb.plugins.LogInterceptor.invokeHome(LogInterceptor.java:121)

at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invokeHome(ProxyFactoryFinderInterceptor.java:93)

at org.jboss.ejb.EntityContainer.internalInvokeHome(EntityContainer.java:508)

at org.jboss.ejb.Container.invoke(Container.java:894)

at org.jboss.ejb.plugins.local.BaseLocalProxyFactory.invokeHome(BaseLocalProxyFactory.java:342)

at org.jboss.ejb.plugins.local.LocalHomeProxy.invoke(LocalHomeProxy.java:118)

at $Proxy127.create(Unknown Source)

at com.rad300.ejb.session.MaintainBowlerBean.createBowler(MaintainBowlerBean.java:628)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:585)

at org.jboss.invocation.Invocation.performCall(Invocation.java:345)

at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:214)

at org.jboss.ejb.plugins.SecurityProxyInterceptor.invoke(SecurityProxyInterceptor.java:175)

at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185 )

at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:13 0)

at org.jboss.webservice.server.ServiceEndpointInterceptor.invoke(ServiceEndpointInterceptor.java:51)

at org.jboss.ejb.plugins.CallValidationInterceptor.invoke(CallValidationInterceptor.java:48)

at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:105)

at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:335)

at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:166)

at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:139)

at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:192)

at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)

at org.jboss.ejb.SessionContainer.internalInvoke(SessionContainer.java:624)

at org.jboss.ejb.Container.invoke(Container.java:873)

at sun.reflect.GeneratedMethodAccessor96.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:585)

at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:141)

at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)

at org.jboss.mx.server.Invocation.invoke(Invocation.java:72)

at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:249)

at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)

at org.jboss.invocation.local.LocalInvoker$MBeanServerAction.invoke(LocalInvoker.java:155)

at org.jboss.invocation.local.LocalInvoker.invoke(LocalInvoker.java:104)

at org.jboss.invocation.InvokerInterceptor.invokeLocal(InvokerInterceptor.java:179)

at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:165)

at org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.java:46)

at org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:55)

at org.jboss.proxy.ejb.StatelessSessionInterceptor.invoke(StatelessSessionInterceptor.java:97)

at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:86)

at $Proxy269.createBowler(Unknown Source)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:585)

at com.rad300.web.ejb.DynamicEJBDelegate.invoke(DynamicEJBDelegate.java:80)

at $Proxy314.createBowler(Unknown Source)

at com.rad300.web.action.user.BowlerCRUD.create(BowlerCRUD.java:349)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:585)

at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)

at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)

at com.rad300.web.action.BaseDispatchAction.execute(BaseDispatchAction.java:41)

at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)

at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)

at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)

at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)

at com.planetj.servlet.filter.compression.CompressingFilter.handleDoFilter(CompressingFilter.java:196)

at com.planetj.servlet.filter.compression.CompressingFilter.doFilter(CompressingFilter.java:186)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)

at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)

at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)

at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:407)

at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)

at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:365)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)

at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)

at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)

at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)

at java.lang.Thread.run(Thread.java:595)

kbogerta at 2007-7-15 16:28:42 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

Some more information:

uname -a

Linux rad300.com 2.6.11.12-xenU-rimu1 #2 Thu Sep 8 03:26:41 UTC 2005 i686 GNU/Linux

java -version

java version "1.5.0_04"

Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)

Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)

kbogerta at 2007-7-15 16:28:42 > top of Java-index,Java HotSpot Virtual Machine,Specifications...