Jump to content
Changes to the Jaspersoft community edition download ×

Possible solution to OUT OF MEMORY ERROR

Recommended Posts

By: anavarro - anavarrog

Possible solution to OUT OF MEMORY ERROR

2005-07-11 05:51

When report contains multiple pages with images that are repeated in each page, these are loaded every time having occupied memory in the JVM. When exceeding maximo of memory assigned to the JVM takes place error OUT OF MEMORY, and in the following pages this image will not be.The solution is that a single image is loaded once and not like before, that is loaded whenever it must show. In order to solve to this error I have made some changes in the JRImageLoader class:

- We added the Imagenes and ImagenesBytes variable that will be a Map with all the different images and already loaded.


private static HashMap ImagenesBytes = new HashMap();

private static HashMap Imagenes = new HashMap();

- we modified the method loadImageDataFromFilebeing of the following way:

public static byte[] loadImageDataFromFile(File file) throws JRException {

try {

/* Modificado por ANG */

Object o = ImagenesBytes.get(file.getName());


return (byte []) o;



byte[] bytes = JRLoader.loadBytes(file);


return bytes;



// return JRLoader.loadBytes(file);

} catch (JRException e) {

throw new JRException("Error loading image data : " + file, e);




- And we modified the method loadImage being of the following way:


public static Image loadImage(byte[] bytes)


/**Modificado por ANG */


return (Image) Imagenes.get(bytes);



Image image = Toolkit.getDefaultToolkit().createImage( bytes );

MediaTracker traker = new MediaTracker(new Panel());

traker.addImage(image, 0);






catch (Exception e)



image = null;


/** Modificado por ANG****/



return image;



With this solution I have obtained that, for example, to print 100 invoices with logotipo of my company, this logotipo single it loading for the first invoice, occupying approximately 1 megabyte in the JVM. Before 100 megabytes, 1 MB by invoice would have occupied me. I think that this code would have to be taken into account by the developers of the Jasperreport to be including in later versions.

A greeting, Antonio Navarro. anavarrog@gtssl.com




By: Daren O - rckrll106

RE: Possible solution to OUT OF MEMORY ERROR

2005-07-11 07:00

I think this is a great idea. I'm currently hashing images before report fill and passing in an image map to the report. I'm doing this for barcode images where I could have 1000's of images.





By: Teodor Danciu - teodord

RE: Possible solution to OUT OF MEMORY ERROR

2005-07-19 10:13




I don't see why you needed to that.

The image elements already have an "isUsingCache"

attribute that enables image caching when the image

expression is of type String.

So, you should put the image file name in your

image expression and set the flag to true.

The logo image will be loaded once and reused

throughout the report.


I hope this helps.






By: evgenykravchenko - ekravche

RE: Possible solution to OUT OF MEMORY ERROR

2005-07-21 15:07

A good solution to memory issues would be to have a page processing scheme where the client would request a certain page and the page broker would render that page and return the result to the client. So instead of rending the entire report, only render pages which are request by the client. So if I had a 1000 page report and I only needed to view the 2nd page, then send a request to the page broker to retrieve page 2. You can add all kinds of caching mechanisms to this. This solution also eliminates dependency on the client side to have the entire serialized version of the report on hand before the report can be displayed.



So I suppose one solution is to render pages on a per page request basis, and later maybe add a caching mechanism.


I'm not sure how it would change the architecture and the design of the current solution, and how difficult it would be to implement this solution.

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...