3

I created a web service in java to return json data. Ok, no problem for small data but when I work with big data, it returns an out of memory error.

See my code:

public void getImages() throws Exception {
      JSONObject o = new JSONObject();
      JSONArray jsonArray = new JSONArray();

      try {
                Class.forName(DRIVERNAME);
                conn = DriverManager.getConnection(URL, USER, PASS);

                Statement s = connection.createStatement();
                ResultSet r = s.executeQuery("select * from images");

                while (r.next()) {
                          JSONObject obj = new JSONObject();

                          obj.put("id", r.getInt("id"));

                          byte[] iBytes = r.getBytes("image");
                          String iBase64 = DatatypeConverter.printBase64Binary(iBytes);
                          obj.put("image", iBase64);

                          jsonArray.put(obj);
                }

                o.put("images", jsonArray);

      } catch (SQLException e) {

      }
}

Out of memory (java.lang.OutOfMemoryError: Java heap space) happenning the moment the add images in jsonArray:

byte[] iBytes = r.getBytes("image");
String iBase64 = DatatypeConverter.printBase64Binary(iBytes);
obj.put("image", iBase64);

Any idea how to solve the problem?

7
  • Either don't do that or increase the amount of memory available. Commented Feb 25, 2013 at 15:22
  • How many images typically are you retrieving in one call and how big are they? Your current code is pretty memory intensive, if you are running into OOM issues then you will need to increase the amount of memory available to the program, or improve the logic to reduce the amount of data you are attempting to send back to the client. Commented Feb 25, 2013 at 15:22
  • Are you sure you want to transfer image byte data via json ? Maybe you can set a limit on the image size or resize them if you really want to do that. Commented Feb 25, 2013 at 15:25
  • @Perception The call return 2000 images on average. I need to store the images for offline work on application mobile. So, I dont know the paginate the results to avoid out of memory. So, any idea? Commented Feb 25, 2013 at 15:29
  • Two thousand? Thats alot for any kind of application much less a mobile one. You're going to run into memory issues on both sides. Depending on the average image size (which you have not mentioned) you definitely need to paginate the result set. Commented Feb 25, 2013 at 15:31

2 Answers 2

2

Solve your problem with stream json (ObjectMapper, JsonFactory, JsonGenerator) from Jackson Library. See the code below:

public void getImages() throws Exception {

    ObjectMapper objectMapper = new ObjectMapper();
    JsonFactory jf = objectMapper.getFactory();
    JsonGenerator jg = f.createGenerator(new File("c:\\images.json"), JsonEncoding.UTF8);

    try {
        Class.forName(DRIVERNAME);
        conn = DriverManager.getConnection(URL, USER, PASS);

        Statement s = connection.createStatement();
        ResultSet r = s.executeQuery("select * from images");

        jg.writeFieldName("images");
        jg.writeStartArray();

        while (r.next()) {
           jg.writeBinary(r.getBinaryStream("IMAGEM"), -1);
           in.close();
        }

        jg.writeEndArray(); 
        jg.writeEndObject(); 
        jg.close();

    } catch (SQLException e) {
        //use the exceptions
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

What is the "in" variable you have here...Also you forgot to writeStartObject().
1

why don't you simply return the url to the image and let the browser pull that resource? I don't quite see the advantage to passing it via your json response, as you are requiring that java hold the image set in memory before sending it over.

3 Comments

I cant to do because the mobile application will be offline works. So, the user mobile application dont access internet to request url.
in that case, you need to configure your heap size to be bigger
I commented the lines to create image and the out of memory problem dismiss.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.