Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6574125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:13:16+00:00 2026-05-25T15:13:16+00:00

I am currently writing a Java application to retrieve BLOB type data from the

  • 0

I am currently writing a Java application to retrieve BLOB type data from the database and I use a query to get all the data and put them in a List of Map<String, Object> where the columns are stored. When I need to use the data I iterate the list to get the information.

However I got an OutOfMemoryError when I tried to get the list of rows more than a couple times. Do I need to release the memory in the codes? My code is as follows:

ByteArrayInputStream binaryStream = null;
OutputStream out = null;
try {   
   List<Map<String, Object>> result =
    jdbcOperations.query(
        sql,
        new Object[] {id},
        new RowMapper(){
          public Object mapRow(ResultSet rs, int i) throws SQLException {
            DefaultLobHandler lobHandler = new DefaultLobHandler();
            Map<String, Object> results = new HashMap<String, Object>();
            String fileName = rs.getString(ORIGINAL_FILE_NAME);
            if (!StringUtils.isBlank(fileName)) {
              results.put(ORIGINAL_FILE_NAME, fileName);
            }
            byte[] blobBytes = lobHandler.getBlobAsBytes(rs, "AttachedFile"); 
            results.put(BLOB, blobBytes);

            int entityID = rs.getInt(ENTITY_ID); 
            results.put(ENTITY_ID, entityID);
            return results;
          }
        }
    );

  int count = 0;
  for (Iterator<Map<String, Object>> iterator = result.iterator(); 
      iterator.hasNext();) 
  {
    count++;
    Map<String, Object> row = iterator.next();
    byte[] attachment = (byte[])row.get(BLOB);
    final int entityID = (Integer)row.get(ENTITY_ID);
    if( attachment != null) {
      final String originalFilename = (String)row.get(ORIGINAL_FILE_NAME);
      String stripFilename;
      if (originalFilename.contains(":\\")) {
        stripFilename = StringUtils.substringAfter(originalFilename, ":\\");
      }
      else {
        stripFilename = originalFilename;
      }
      String filename = pathName + entityID + "\\"+ stripFilename;

      boolean exist = (new File(filename)).exists();

      iterator.remove(); // release the resource

      if (!exist) {
        binaryStream = new ByteArrayInputStream(attachment);
        InputStream extractedStream = null;
        try {
          extractedStream = decompress(binaryStream);
          final byte[] buf = IOUtils.toByteArray(extractedStream);
          out = FileUtils.openOutputStream(new File(filename));
          IOUtils.write(buf, out);
        }
        finally {
          IOUtils.closeQuietly(extractedStream);
        }
      }
      else {
        continue;
      }
    }
  }
}
catch (FileNotFoundException e) {
  e.printStackTrace();
}
catch (IOException e) {
  e.printStackTrace();
}
finally {
  IOUtils.closeQuietly(out);
  IOUtils.closeQuietly(binaryStream);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:13:17+00:00Added an answer on May 25, 2026 at 3:13 pm

    Consider reorganizing your code so that you don’t keep all the blobs in memory at once. Instead of putting them all in a results map, output each one as you retrieve it.

    The advice about expanding your memory settings is good also.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing an iPhone application which gets some data from the user and
I am writing an application in java (1.6) using swing. I currently have a
I'm currently writing a Java app that interfaces with a MySQL database. My problem
I'm currently writing a Java application that needs to look at how heavily loaded
I'm currently transforming my Web Application tn Java with Spring MVC framework from ASP.NET
I'm writing a java application. Currently this produces a file called trace.txt in the
I'm currently writing a Java application to be used with a Windows-Machine authed with
I'm currently writing a web application in java using GWT 2.0 in eclipse. I
I am writing a distributed Java application that will make heavy use of networking,
I'm writing a little Java application that I want to use to search RSS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.