1,105,380 Community Members

ERROR: java.lang.OutOfMemoryError: Java heap space

Member Avatar
vinithktp
Light Poster
30 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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

Member Avatar
vinithktp
Light Poster
30 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Done.. thank you

Member Avatar
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

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

Member Avatar
vinithktp
Light Poster
30 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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;                  }
Question Answered as of 2 Years Ago by JamesCherrill
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: