Hi,

I am facing OutOfMemory exception in Java, Kindly help me to resolve this issue, really appreciate your help

ERROR: java.lang.OutOfMemoryError: Java heap space

CODE:

public Blob getBlob(String databaseInstance, InputStream inputStream) throws SQLException, Exception
{
            applog.info("Entering getBlob");
            InputStream stream = null;
            applog.info("inputStream : " + inputStream);
            stream = inputStream;
            applog.info("stream : " + stream);                                   

            byte[] tmp = new byte[50000000];
            byte[] blobValue = null;
            int sz, len = 0;            
		while ((sz = stream.read(tmp)) != -1) {
                  if (blobValue == null) {
                        len = sz;
                        blobValue = tmp;
                  } else {
                        byte[] narr;
                        int nlen;
                        nlen = len + sz;
                        narr = new byte[nlen];
                        System.arraycopy(blobValue, 0, narr, 0, len);
                        System.arraycopy(tmp, 0, narr, len, sz);
                        blobValue = narr;
                        len = nlen;
                        narr = null;
                  }
            }
            if(null != blobValue) {
                  if (len != blobValue.length) {
                        byte[] narr = new byte[len];     

                        System.arraycopy(blobValue, 0, narr, 0, len);
                        blobValue = narr;
                        narr = null;
                  }
                  applog.info("blobValue len : " + blobValue.length + " toString : " + blobValue.toString());
            }
            Blob newBlob = null;
            newBlob = oracle.sql.BLOB.createTemporary(globalMetaCon, true, oracle.sql.BLOB.DURATION_SESSION);
            if (blobValue != null) {
                  try {
                        applog.info("newBlob Before: " + newBlob.toString() + " len : " + newBlob.length());
                        if (newBlob != null) {
                              ((oracle.sql.BLOB) newBlob).putBytes(1, blobValue);
                              applog.info("inside put bytes : " + blobValue);
                        }
                        applog.info("newBlob After: " + newBlob.toString() + " len : " + newBlob.length());
 
                  } catch (SQLException e) {
                        applog.error("ComponentScheduleDAO | getblob | sql exception" + e);
                  } catch (Exception e) {
                        applog.error("ComponentScheduleDAO | getblob | general exception" + e);
                  } finally {
                        if(null != stream)
                        {
                              stream.close();
                        }
                  }
           }
            applog.info("Exiting getBlob in ComponentScheduleDAO");
            tmp = null;
            blobValue = null;
            return newBlob;
      }

Thanks in advance...

Recommended Answers

It looks like tmp is a 50MByte array which you copy at least once, so you're probably just exceeding the default memory allocation for the JVM. This link is a decent summary of what you need to know...
[url]http://javarevisited.blogspot.com/2011/05/java-heap-space-memory-size-jvm.html[/url]

Jump to Post

All 4 Replies

Please edit that and put the code in code tags, properly indented, so we can read it.

Please edit that and put the code in code tags, properly indented, so we can read it.

Done.. thank you

I have resolved this issue, removed below entries...

byte[] tmp = new byte[50000000];            byte[] blobValue = null;            int sz, len = 0;            		while ((sz = stream.read(tmp)) != -1) {                  if (blobValue == null) {                        len = sz;                        blobValue = tmp;                  } else {                        byte[] narr;                        int nlen;                        nlen = len + sz;                        narr = new byte[nlen];                        System.arraycopy(blobValue, 0, narr, 0, len);                        System.arraycopy(tmp, 0, narr, len, sz);                        blobValue = narr;                        len = nlen;                        narr = null;                  }            }            if(null != blobValue) {                  if (len != blobValue.length) {                        byte[] narr = new byte[len];                              System.arraycopy(blobValue, 0, narr, 0, len);                        blobValue = narr;                        narr = null;                  }