954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ERROR: java.lang.OutOfMemoryError: Java heap space

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...

vinithktp
Light Poster
30 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
Please edit that and put the code in code tags, properly indented, so we can read it.

Done.. thank you

vinithktp
Light Poster
30 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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...
http://javarevisited.blogspot.com/2011/05/java-heap-space-memory-size-jvm.html

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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;                  }
vinithktp
Light Poster
30 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: