2 Replies Latest reply on Jan 25, 2011 8:26 PM by 832582

    MimeMessage: java.lang.OutOfMemoryError: Java heap space

      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(JarRsrcLoader.java:56)
      Caused by: java.lang.OutOfMemoryError: Java heap space
      at java.util.Arrays.copyOf(Unknown Source)
      at java.io.ByteArrayOutputStream.write(Unknown Source)
      at com.sun.mail.util.ASCIIUtility.getBytes(ASCIIUtility.java:256)
      at javax.mail.internet.MimeMessage.parse(MimeMessage.java:335)
      at javax.mail.internet.MimeMessage.<init>(MimeMessage.java:205)
      at ca.gc.ccra.eics.application.p7mclient.P7MClientMain.processInboundP7MFile(P7MClientMain.java:751)
      at ca.gc.ccra.eics.application.p7mclient.P7MClientMain.processRemove(P7MClientMain.java:456)
      at ca.gc.ccra.eics.application.p7mclient.ECCRYPTP7MClient.processApplyOrRemove(ECCRYPTP7MClient.java:130)
      at ca.gc.ccra.eics.application.p7mclient.ECCRYPTP7MClient.main(ECCRYPTP7MClient.java:71)


      Edited by: user13502256 on Jan 20, 2011 7:29 AM
        • 1. Re: MimeMessage: java.lang.OutOfMemoryError: Java heap space
          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
            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..."