13 Replies Latest reply: Nov 21, 2006 7:31 AM by 796254 RSS

    OutOfMemoryError in file handling

    807607
      I am getting the file from the database around 10 MB size. While iterating result set in first iteration its working properly. But in second iteration it�s showing ERROR as java.lang.OutOfMemoryError.

      I had tried following things

      1. Closed result set, statement, connection... in finally block.
      2. And also I made clob as null after every iteration.

      I came to know about this solution during the run time I want to increase the heap memory but I am not sure it�s correct solution or not can you any one help me?

      CODE :

                     pStmt = dbConnection.prepareStatement(SQLReader.getValue(FileServiceDAOConstants.SQL_RETRIEVE_FILE));
                     pStmt.setTimestamp(1, TimeStampConverter.toTimeStamp(fileHeaderId));
                     res = pStmt.executeQuery();
                     Clob clob = null;
                     while(res.next()){
                          clob =res.getClob(1);
                     }
                     
                     BufferedReader br = new BufferedReader(clob.getCharacterStream());
                     
                     clob = null;
                     

                     xmlDatafile = br.readLine();
                     br = null;
                     br.close();






      Error : java.lang.OutOfMemoryError
        • 1. Re: OutOfMemoryError in file handling
          807607
          Can you any one help me for this questions?
          • 2. Re: OutOfMemoryError in file handling
            796254
            You waited only two hours. There's no guarantee that anyone will answer your question. No one owes you an answer. That's the way volunteer forums work.

            %
            • 3. Re: OutOfMemoryError in file handling
              807607
              It looks like you are fairly certain you don't have a memory leak or references to any large object instances unintentionally left hanging around.

              Use the "-Xmx" run time flag to increase the upper limit for heap allocation, like
              java -Xmx256m MyClass
              • 4. Re: OutOfMemoryError in file handling
                796254
                I am getting the file from the database around 10 MB
                size. While iterating result set in first iteration
                its working properly. But in second iteration it�s
                showing ERROR as java.lang.OutOfMemoryError.
                Why do you have to iterate? Do you mean walking through the ResultSet? So there are several files in memory at once? Are all the files 10MB? Or are some larger than others?

                >
                I had tried following things

                1. Closed result set, statement, connection... in finally block.
                That's good, but not helpful here.
                2. And also I made clob as null after every iteration.
                Don't see what this buys you. What did you do to it before you set it to null?
                I came to know about this solution during the run
                time I want to increase the heap memory but I am not
                sure it�s correct solution or not can you any one
                help me?
                That's one thing you can try. Set -Xms=1024m -Xmx=1024m and see if that helps. You might also look at other GC settings, like perm space size.

                Profile your code to see where the memory is being used.

                Download JVMStat and hook it into your process.

                Don't know if it helps:
                package cruft;
                
                import java.io.BufferedReader;
                import java.io.IOException;
                import java.sql.Connection;
                import java.sql.PreparedStatement;
                import java.sql.ResultSet;
                import java.sql.SQLException;
                import java.sql.Timestamp;
                
                /**
                 * Created by IntelliJ IDEA.
                 * User: Michael
                 * Date: Nov 21, 2006
                 * Time: 8:24:01 AM
                 * To change this template use File | Settings | File Templates.
                 */
                public class TossThis
                {
                    public String getXml(Connection dbConnection)
                    {
                        String xml = "";
                        
                        PreparedStatement pStmt = null;
                        ResultSet res = null;
                        BufferedReader br = null;
                
                        try
                        {
                            String sql = "sql of some kind here";
                            pStmt = dbConnection.prepareStatement(sql);
                            pStmt.setTimestamp(1,new Timestamp(System.currentTimeMillis()));
                            res = pStmt.executeQuery();
                            while (res.next())
                            {
                                br = new BufferedReader(res.getClob(1).getCharacterStream());
                                xml = br.readLine(); 
                            }
                        }
                        catch (SQLException e)
                        {
                            e.printStackTrace();
                        }
                        catch (IOException e)
                        {
                            e.printStackTrace();
                        }
                        finally
                        {
                            try { if (br != null) br.close(); } catch (Exception e) {}
                            try { if (res != null) res.close(); } catch (Exception e) {}
                            try { if (pStmt != null) pStmt.close(); } catch (Exception e) {}
                        }
                
                        return xml;
                    }
                }
                %
                • 5. Re: OutOfMemoryError in file handling
                  807607
                  Can you post some more realistic code? You have posted such code:
                  br = null;
                  br.close();
                  This will throw a NullPointerException. Usually there is no need to nullify variables. UlrikaJ (a.k.a. dizzy these days) would have pointed out that it can even be harmful.

                  Can you locate the point in which memory is consumed? Since you are reading from a stream and writing to a file I would not expect big memory allocations.

                  Edit: This was @OP

                  Mike
                  • 6. Re: OutOfMemoryError in file handling
                    807607
                    This is my full code:

                    //Error : java.lang.OutOfMemoryError
                    
                    /*
                     * 
                     *
                     * To change the template for this generated file go to
                     * Window>Preferences>Java>Code Generation>Code and Comments
                     */
                    
                    import java.io.BufferedInputStream;
                    import java.io.BufferedReader;
                    import java.io.ByteArrayInputStream;
                    import java.io.DataOutputStream;
                    import java.io.File;
                    import java.io.FileOutputStream;
                    import java.io.InputStream;
                    import java.io.StringReader;
                    import java.sql.Blob;
                    import java.sql.Clob;
                    import java.sql.Connection;
                    import java.sql.PreparedStatement;
                    import java.sql.ResultSet;
                    import java.sql.SQLException;
                    
                    
                    
                    public class FileServiceDAOImpl extends AbstractDB2DAO implements FileServiceDAO {
                    
                         /**
                          * Source code version Constant
                          */
                         private static final String VERSION = CsdVersion.extract("$ Revision :1.0 $");
                    
                         /**
                          *  Instantiate the Logger
                          */
                         private static final Log LOGGER = LogFactory.getLog(FileServiceDAOImpl.class);
                    
                         private static ConfigurationService technicalConfigService = TechnicalConfigurationServiceFactory.createInstanceForService(FileServiceDAOConstants.APPLICATION_NAME, FileServiceDAOImpl.class.getName());
                    
                         
                         public String getDataSourceName(){
                              //Method signature to use in the log statements
                              final String LOG_METHOD = "getDataSourceName():String";
                    
                              if (LOGGER.isDebugEnabled()) {
                                   LOGGER.debug(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, GenericLogConstants.LOG_METHOD_START, null));
                              }
                    
                              String dataSourceName = "";
                              try {
                                   dataSourceName = technicalConfigService.getString("fileservicedatasourcename","jdbc/ds3");
                                   
                                   //logging DataSourceName
                                   if(LOGGER.isDebugEnabled()){
                                        LOGGER.debug(
                                             ssLogMessage.newMessageInstance(
                                                  this,
                                                  LOG_METHOD,
                                                  VERSION,
                                                  FileServiceDAOConstants.LOG_GETDBSRC_DBSRC_FOUND,
                                                  new Object[]{dataSourceName} 
                                             )
                                        );
                                   }
                    
                              }/*catch (ConfigurationServiceException configurationServiceException) {
                                   configurationServiceException.printStackTrace();
                                   //Log Error
                                   if (LOGGER.isErrorEnabled()) {
                                        LOGGER.error(
                                             ssLogMessage.newMessageInstance(
                                                  this,
                                                  LOG_METHOD,
                                                  VERSION,
                                                  FileServiceDAOConstants
                                                       .LOG_GETDBSRC_CONFIGSERVICEEXCPETION,
                                                  new Object[] {
                                                        configurationServiceException.getMessage()}),
                                             configurationServiceException);
                                   }
                                   Messages Messages = new Messages();
                                   Messages.addMessage(new Message(FileServiceDAOMessageKeys.INSERTFILE_DBSRC_NOTFOUND__EXCEPTION), MessageType.getError());
                                   FileServiceDAOException fileServiceDAOException = new FileServiceDAOException(Messages);
                                   throw fileServiceDAOException;     
                                             
                              }*/finally {
                                   // Log end of method
                                   if (LOGGER.isDebugEnabled()) {
                                        LOGGER.debug(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, GenericLogConstants.LOG_METHOD_END, null));
                                   }
                    
                              }
                              System.out.println("##############" + dataSourceName);
                              return dataSourceName;
                         }
                    
                         /**
                          * This method will split file into 30000 character file and insert the file into database.
                          * @param file which is data file portion of SOAP Message
                          * @param fileHeaderId which is reference of the file to be inserted in database 
                          * @throws FileServiceDAOException
                          */
                    
                         public void insertFile(String file, String fileHeaderId) throws FileServiceDAOException {
                    
                              //Method signature to use in the log statements
                              final String LOG_METHOD = "insertFile(String file , String fileHeaderId):void";
                    
                              // Log start of method
                              if (LOGGER.isDebugEnabled()) {
                                   LOGGER.debug(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, GenericLogConstants.LOG_METHOD_START, null));
                              }
                              Connection dbConnection = null;
                              PreparedStatement pStmt = null;
                              String dataSourceName = null;
                              int sequenceID = 0;
                              int fileStart = 0;
                              int fileEnd = FileServiceDAOConstants.FILEPART_SIZE;
                              int noOfFilePart = 0;
                              int fileSize = file.length();
                              
                              
                              try {
                                   //Get DataBase Connection
                                   dataSourceName = getDataSourceName();
                                   if (dataSourceName == null) {
                                        Messages Messages = new Messages();
                                        Messages.addMessage(new Message(FileServiceDAOMessageKeys.INSERTFILE_DBSRC_NOTFOUND__EXCEPTION), MessageType.getError());
                                        FileServiceDAOException fileServiceDAOException = new FileServiceDAOException(Messages);
                    
                                        //Log Error to be thrown
                                        if (LOGGER.isDebugEnabled()) {
                                             LOGGER.debug(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, FileServiceDAOConstants.LOG_INSERTFIE_DBSRC_NOTFOUND, new Object[] { fileServiceDAOException.getMessage()}), fileServiceDAOException);
                                        }
                    
                                        throw fileServiceDAOException;
                                   }
                                
                                   dbConnection = DAODatabaseUtil.openConnection(dataSourceName);
                    
                                   dbConnection.setAutoCommit(false);
                                   //StringReader reader = new StringReader(file);  //clob
                                   //InputStream is = null;  //blob
                                   byte[] fileBytearr = file.getBytes(); //blob
                                   ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileBytearr); //blob
                                   
                    
                                   pStmt = dbConnection.prepareStatement(SQLReader.getValue(FileServiceDAOConstants.SQL_INSERT_FILE));
                                   //comment by Vishvesh for Clob data type test 07/11/2006               
                                   /*if (fileSize < fileEnd) {
                                        fileEnd = fileSize;
                                   }
                    
                                   String filePart = "";
                    
                                   //number of file parts to be saved
                                   if((fileSize % FileServiceDAOConstants.FILEPART_SIZE )!=0 ){
                                        noOfFilePart = fileSize /FileServiceDAOConstants.FILEPART_SIZE + 1;     
                                   }else{
                                        noOfFilePart = (fileSize /FileServiceDAOConstants.FILEPART_SIZE );
                                   }
                                   
                                   //Logging no of parts to be inserted     
                                   if (LOGGER.isDebugEnabled()) {
                                        LOGGER.debug(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, FileServiceDAOConstants.LOG_INSERTFILE_NOOFPARTS_TOBE_INSERTED, new Object[] { String.valueOf(noOfFilePart)}));
                                   }
                    
                                   StringBuffer fileStringBuffer = new StringBuffer(file);
                    
                                   for (int fileCount = 0; fileCount < noOfFilePart; fileCount++) {
                    
                                        sequenceID = fileCount + 1;
                                        filePart = fileStringBuffer.substring(fileStart, fileEnd).toString();
                                        pStmt.setTimestamp(1, TimeStampConverter.toTimeStamp(fileHeaderId));
                                        //pStmt.setString(1,fileHeaderId);
                                        pStmt.setInt(2, sequenceID);
                                        pStmt.setString(3, filePart);
                                        pStmt.addBatch();
                                        fileStart = fileStart + FileServiceDAOConstants.FILEPART_SIZE;
                                        fileEnd = fileEnd + (((file.length() - fileEnd) < FileServiceDAOConstants.FILEPART_SIZE) ? (file.length() - fileEnd) : FileServiceDAOConstants.FILEPART_SIZE);
                    
                                   }
                                   
                                   int noOfInserts = pStmt.executeBatch().length;
                                   
                                   if (noOfInserts < noOfFilePart) {
                    
                                        //roll back transcation
                                        dbConnection.rollback();
                                        Messages Messages = new Messages();
                                        Messages.addMessage(new Message(FileServiceDAOMessageKeys.INSERTFILE_NOTINSERTED_EXCEPTION), MessageType.getError());
                                        FileServiceDAOException fileServiceDAOException = new FileServiceDAOException(Messages);
                    
                                        //Log Error to be thrown
                                        if (LOGGER.isDebugEnabled()) {
                                             LOGGER.debug(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, FileServiceDAOConstants.LOG_INSERTFILE_BATCHUPLOADED_UNSUCCESSFUL, new Object[] { fileServiceDAOException.getMessage()}), fileServiceDAOException);
                                        }
                    
                                        throw fileServiceDAOException;
                                   }*/
                                   pStmt.setTimestamp(1, TimeStampConverter.toTimeStamp(fileHeaderId));
                                   //pStmt.setCharacterStream(2,reader,(int) file.length()); //clob
                                   pStmt.setBinaryStream(2,byteArrayInputStream,file.length());//blob
                                   
                                   pStmt.executeUpdate();
                                   dbConnection.commit();
                                   
                                   
                                   //Logging batch upload success     
                                   if (LOGGER.isDebugEnabled()) {
                                        LOGGER.debug(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, FileServiceDAOConstants.LOG_INSERTFILE_BATCHUPLOADED_SUCCESS, null), null);
                                   }
                    
                                   DAODatabaseUtil.closeStatement(pStmt);
                                   DAODatabaseUtil.closeConnection(dbConnection);
                    
                              } catch (DAODatabaseException daoDbException) {
                                   //daoDbException.printStackTrace();
                                   //Log Error
                                   if (LOGGER.isErrorEnabled()) {
                                        LOGGER.error(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, FileServiceDAOConstants.LOG_INSERTFILE_DAODATABASEEXCEPTION, new Object[] { daoDbException.getMessage()}), daoDbException);
                                   }
                    
                                   Messages Messages = new Messages();
                                   Messages.addMessage(new Message(FileServiceDAOMessageKeys.INSERTFILE_DAODB_EXCEPTION), MessageType.getError());
                                   throw new FileServiceDAOException(Messages);
                    
                              } catch (SQLException sqlException) {
                                   //sqlException.getNextException();
                                   //sqlException.printStackTrace();
                                   if (LOGGER.isErrorEnabled()) {
                                        LOGGER.error(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, FileServiceDAOConstants.LOG_INSERTFILE_SQLEXCEPTION, new Object[] { sqlException.getMessage()}), sqlException);
                                   }
                                   Messages Messages = new Messages();
                                   Messages.addMessage(new Message(FileServiceDAOMessageKeys.INSERTFILE_SQL_EXCEPTION), MessageType.getError());
                                   throw new FileServiceDAOException(Messages);
                    
                              } catch (Exception exception) {
                                   //exception.printStackTrace();
                                   if (LOGGER.isErrorEnabled()) {
                                        LOGGER.error(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, FileServiceDAOConstants.LOG_INSERTFILE_UNHANDLED_EXCEPTION, new Object[] { exception.getMessage()}), exception);
                                   }
                    
                                   Messages Messages = new Messages();
                                   Messages.addMessage(new Message(FileServiceDAOMessageKeys.INSERTFILE_UNHANDLED_EXCEPTION), MessageType.getError());
                                   throw new FileServiceDAOException(Messages);
                    
                              } finally {
                                   if (pStmt != null)
                                        DAODatabaseUtil.closeStatement(pStmt);
                                   if (dbConnection != null)
                                        DAODatabaseUtil.closeConnection(dbConnection);
                                   // Log end of method
                                   if (LOGGER.isDebugEnabled()) {
                                        LOGGER.debug(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, GenericLogConstants.LOG_METHOD_END, null));
                                   }
                              }
                    
                         }
                    
                         /**
                          * This method will retrieve 30000 character file and join it for sending to BSP.
                          * @param file which is data file portion of SOAP Message 
                          * @throws FileServiceDAOException
                          */
                    
                         public String retrieveFile(String fileHeaderId) throws FileServiceDAOException {
                    
                              String xmlDatafile = "";
                    
                              //Method signature to use in the log statements
                              final String LOG_METHOD = " retrieveFile(String fileHeaderId):String";
                    
                              // Log start of method
                              if (LOGGER.isDebugEnabled()) {
                                   LOGGER.debug(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, GenericLogConstants.LOG_METHOD_START, null));
                              }
                    
                              Connection dbConnection = null;
                              PreparedStatement pStmt = null;
                              ResultSet res = null;
                              String dataSourceName = null;
                              //int count=0;
                              try {
                                   // Get DataBase Connection
                    
                                   StringBuffer fileBuffer = new StringBuffer();
                    
                                   dataSourceName = getDataSourceName();
                         
                                   if (dataSourceName == null) {
                                        Messages Messages = new Messages();
                                        Messages.addMessage(new Message(FileServiceDAOMessageKeys.RETRIEVEFILE_DBSRC_NOTFOUND__EXCEPTION), MessageType.getError());
                                        FileServiceDAOException fileServiceDAOException = new FileServiceDAOException(Messages);
                    
                                        //Log Error to be thrown
                                        if (LOGGER.isDebugEnabled()) {
                                             LOGGER.debug(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, FileServiceDAOConstants.LOG_RETRIEVEFILE_DBSRC_NOTFOUND, new Object[] { fileServiceDAOException.getMessage()}), fileServiceDAOException);
                                        }
                    
                                        throw fileServiceDAOException;
                    
                                   }
                                   
                                   System.out.println(Runtime.getRuntime().freeMemory());
                                   dbConnection = DAODatabaseUtil.openConnection(dataSourceName);
                                   dbConnection.setAutoCommit(false);
                                   
                                   
                                   int noOfFilePart =0;
                                   //comment by vishvesh  date 07/11/2006
                                   /*pStmt =dbConnection.prepareStatement(SQLReader.getValue(FileServiceDAOConstants.SQL_RETRIEVE_COUNT));
                                   pStmt.setTimestamp(1,TimeStampConverter.toTimeStamp(fileHeaderId));
                                   res =pStmt.executeQuery();
                                   while(res.next()){
                                        noOfFilePart =res.getInt(1); //count of file parts
                                   }
                                   DAODatabaseUtil.closeResultSet(res);
                                   DAODatabaseUtil.closeStatement(pStmt);
                                   */
                                   res=null;
                                   pStmt=null;
                                   int startCount =0;
                                   int endCount =0;
                                   int heapCount =noOfFilePart;
                                   //comment by vishvesh date 07/11/2006
                                   /*while(heapCount >0){
                                   
                                        if(noOfFilePart < FileServiceDAOConstants.NOS_FILEPART){
                                             startCount =endCount +1;
                                             endCount = heapCount;
                                        }else{
                                             startCount =endCount+1;
                                             if(heapCount < FileServiceDAOConstants.NOS_FILEPART){
                                                  endCount =endCount + heapCount;
                                                       
                                             }else{
                                                  endCount =endCount + FileServiceDAOConstants.NOS_FILEPART;
                                             }
                                             
                                        }
                                        pStmt = dbConnection.prepareStatement(SQLReader.getValue(FileServiceDAOConstants.SQL_RETRIEVE_FILE));
                                        pStmt.setTimestamp(1, TimeStampConverter.toTimeStamp(fileHeaderId));
                                        pStmt.setInt(2,startCount);
                                        pStmt.setInt(3,endCount);
                                        //pStmt.setString(1,fileHeaderId);
                    
                                        res = pStmt.executeQuery();
                                        //res.setFetchSize(50);
                                        while (res.next()) {
                                             fileBuffer.append(res.getString(FileServiceDAOConstants.COLS_FILE_PART));//EXTF_FILE_PART
                                        }
                                        DAODatabaseUtil.closeResultSet(res);
                                        DAODatabaseUtil.closeStatement(pStmt);
                                        
                                        heapCount =heapCount-FileServiceDAOConstants.NOS_FILEPART;
                                   }
                                   */     
                                   pStmt = dbConnection.prepareStatement(SQLReader.getValue(FileServiceDAOConstants.SQL_RETRIEVE_FILE));
                                   pStmt.setTimestamp(1, TimeStampConverter.toTimeStamp(fileHeaderId));
                                   res = pStmt.executeQuery();
                                   Clob clob = null;
                                   while(res.next()){
                                        clob =res.getClob(1);
                                   }
                                   
                                   BufferedReader br = new BufferedReader(clob.getCharacterStream());
                                   
                                   clob = null;
                                   
                                   /*Blob blob = null;
                                   while(res.next()){
                                        blob = res.getBlob(1);
                                   }
                                   InputStream is = null;
                                   
                                   is = blob.getBinaryStream();*/
                                   
                                   /*BufferedInputStream buf=new BufferedInputStream(is);//for better performance
                                   byte[] buffer=new byte[1024];//byte buffer 
                                   int bytesRead=0;
                                   //String blobString = "";
                                   while (true){
                                   bytesRead=buf.read(buffer,0,1024);
                                   //            bytesRead returns the actual number of bytes read from
                                   //            the stream. returns -1 when end of stream is detected
                                   if (bytesRead == -1) break;
                                   String temp = new String(buffer);
                                   //blobString = blobString + temp;
                                   xmlDatafile = xmlDatafile + temp;
                                   } */
                                   
                                   //byte arr[] = (br.readLine()).getBytes();
                                   //fileBuffer.append(br.readLine());
                                   
                                   DAODatabaseUtil.closeResultSet(res);
                                   DAODatabaseUtil.closeStatement(pStmt);
                                   DAODatabaseUtil.closeConnection(dbConnection);
                                   
                                   //xmlDatafile = blobString 
                                   xmlDatafile = br.readLine();
                                   br = null;
                                   br.close();
                              
                                   //xmlDatafile = arr.toString();
                    
                              } catch (DAODatabaseException daoDbException) {
                                   //Log end of method
                                   //daoDbException.printStackTrace();
                                   if (LOGGER.isErrorEnabled()) {
                                        LOGGER.error(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, GenericLogConstants.LOG_METHOD_END, new Object[] { daoDbException.getMessage()}), daoDbException);
                                   }
                    
                                   Messages Messages = new Messages();
                                   Messages.addMessage(new Message(FileServiceDAOMessageKeys.RETRIEVEFILE_DAODB_EXCEPTION), MessageType.getError());
                                   throw new FileServiceDAOException(Messages);
                    
                              } catch (SQLException sqlException) {
                                   //sqlException.printStackTrace();
                                   if (LOGGER.isErrorEnabled()) {
                                        LOGGER.error(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, GenericLogConstants.LOG_METHOD_END, new Object[] { sqlException.getMessage()}), sqlException);
                                   }
                                   Messages Messages = new Messages();
                                   Messages.addMessage(new Message(FileServiceDAOMessageKeys.RETRIEVEFILE_SQL_EXCEPTION), MessageType.getError());
                                   throw new FileServiceDAOException(Messages);
                    
                              } catch (Exception exception) {
                                   //exception.printStackTrace();
                                   if (LOGGER.isErrorEnabled()) {
                                        LOGGER.error(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, FileServiceDAOConstants.LOG_RETRIEVEFILE_UNHANDLED_EXCEPTION, new Object[] { exception.getMessage()}), exception);
                                   }
                                   Messages Messages = new Messages();
                                   Messages.addMessage(new Message(FileServiceDAOMessageKeys.RETRIEVEFILE_UNHANDLED_EXCEPTION), MessageType.getError());
                                   throw new FileServiceDAOException(Messages);
                    
                              } finally {
                                   if (res != null)
                                        DAODatabaseUtil.closeResultSet(res);
                                   if (pStmt != null)
                                        DAODatabaseUtil.closeStatement(pStmt);
                                   if (dbConnection != null)
                                        DAODatabaseUtil.closeConnection(dbConnection);
                                   // Log end of method
                                   if (LOGGER.isDebugEnabled()) {
                                        LOGGER.debug(ssLogMessage.newMessageInstance(this, LOG_METHOD, VERSION, GenericLogConstants.LOG_METHOD_END, null));
                                   }
                              }
                              return xmlDatafile;
                              
                         }
                    
                    }
                    • 7. Re: OutOfMemoryError in file handling
                      796254
                      nobody's likely to look through all this for you.

                      does this posted stuff compile?

                      %
                      • 8. Re: OutOfMemoryError in file handling
                        796254
                        bellyripper's comment is an excellent one, and still applies:
                        br = null;
                        br.close();
                        This code is guaranteed to get you a NPE if you run it. What should give anybody the feeling that the rest of your stuff is reliable when you write something like this?

                        Looks like a mess to me. Should be simpler.

                        %
                        • 9. Re: OutOfMemoryError in file handling
                          EJP
                          ... and please don't post large blocks of commented-out code. It's a bad habit to even leave them there. If they don't work, delete them. Failed experiments or debugging code are of no interest or relevance to the problem.

                          As several posters have pointed out, this code will certainly get an NPE when you try to close the null pointer. Obviously you've never got that far. In fact you haven't told us how far you do get. Which which line gets the OutOfMemoryError?

                          Can you also explain this:
                          while(res.next()){
                                              blob = res.getBlob(1);
                                         }
                          Surely that should be 'if' not 'while'?
                          • 10. Re: OutOfMemoryError in file handling
                            796254
                            i think "while" is the correct idiom. in this case he assumes that there will only BE one row.

                            doesn't explain the rest of the mess. I totally agree with the suggestion to remove commented code. take it out, it's cruft.

                            %
                            • 11. Re: OutOfMemoryError in file handling
                              807607
                              Thanks for giving ideas, how I can solve it. can you give me some more suggestions for this one.
                              • 12. Re: OutOfMemoryError in file handling
                                EJP
                                Can you answer the question I asked?
                                • 13. Re: OutOfMemoryError in file handling
                                  796254
                                  yes - rewrite them as jstl.

                                  %