This discussion is archived
2 Replies Latest reply: Jan 25, 2011 12:26 PM by 832582 RSS

MimeMessage: java.lang.OutOfMemoryError: Java heap space

832582 Newbie
Currently Being Moderated
Hi all,

I'm running into an issue that I hope someone can help with.

Any file that is greater than 2G before encryption (a S/MIME file (.p7m)) causes an out of memory error when trying to decrypt. I have tried increasing the JVM and still the same result. Interesting enough when I do set the heap space, the process seems to be grabbing more that what is set.

When tracing my code, it always appears to fail at the mimeMessage method.

Here is a snippet of my code:

          // Create the decoder object
          PKCS7DecodeStream decoder = null;

          try {
               decoder = new PKCS7DecodeStream(loggedCredentialEntry.loggedInUser,

               MimeMessage mimeMessage = new MimeMessage(null, decoder);

               dump(loggedCredentialEntry.loggedInUser, mimeMessage,


Below is the stack trace:

xception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at Source)
at com.sun.mail.util.ASCIIUtility.getBytes(
at javax.mail.internet.MimeMessage.parse(
at javax.mail.internet.MimeMessage.<init>(
at ca.gc.ccra.eics.application.p7mclient.P7MClientMain.processInboundP7MFile(
at ca.gc.ccra.eics.application.p7mclient.P7MClientMain.processRemove(
at ca.gc.ccra.eics.application.p7mclient.ECCRYPTP7MClient.processApplyOrRemove(
at ca.gc.ccra.eics.application.p7mclient.ECCRYPTP7MClient.main(


Edited by: user13502256 on Jan 20, 2011 7:29 AM
  • 1. Re: MimeMessage: java.lang.OutOfMemoryError: Java heap space
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    Since 2 gigs is a array limit size the limit would seem apparent to me.

    Solutions would be break it into pieces, find a different decoder or figure out if the decoder you are using will deal with files (streams).
  • 2. Re: MimeMessage: java.lang.OutOfMemoryError: Java heap space
    832582 Newbie
    Currently Being Moderated
    Thanks for your response jcshell.

    What/How exactly do you mean break it into pieces? I came across another reading that stated the below but not clear on how to implement it.

    ".....major problem with using the MimeMessage(Session, InputStream) constructor. When you use this over 2x the size of the actual message worth of memory will be allocated which can be a major problem if you have large attachment sizes. The reason this is happening is down in the ASCIIUtility.getBytes(InputStream) method. What happens is it reads the inputstream fully into a ByteArrayOutputStream, which is fine. The problem then comes is that when you call .getBytes() on that output stream it creates a copy of the byte[], instead of just returning the internal byte[], thus it is allocating 2x the memory. To fix this you have two options, create your own OutputStream and give access to its internal byte[], or you could subclass ByteArrayOutputStream and add an accessor to the buf byte array since its protected, and return that..."


  • Correct Answers - 10 points
  • Helpful Answers - 5 points