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?