What are Out Of Memory Errors and how to handle them?

Out Of Memory Error

The java.lang.OutOfMemoryError exception occurs if the Java VM has run out of memory. A Java VM has a specified amount of available memory. The default value depends on the architecture (32 bit/64bit), the Java version and the available RAM of the system. This means that even if you have 100 GB of memory, the Java VM can use memory up to this limit, only. To find the current limit you can use the follow command line:

java -XX:+PrintFlagsFinal -version | grep MaxHeapSize 

To solve this problem you can increase the memory heap of the JVM or modify the reports so that fewer records are fetched per report or the report contains fewer summary fields etc.

Increase the maximum memory of the Java VM

With the command line parameter ‘’"-Xmx"’’ you can specify the maximum memory for the JVM, e.g. for 1024 MB:

java -Xmx1024m …

You should never set a value that is larger than your physical memory, as this would greatly reduce the performance. Also is the maximum value for a 32 bit Java VM aproximate 1400 MB.

Memory usage

If increasing the Java VM memory does not solve the error or you can’t raise it, then you should check the reason why i-net Clear Reports needs so much memory. For the following things the Java VM needs memory:

  • Other Components: If you use i-net Clear Reports in a complex environment together with other Java components then it can be that other components need the most memory but the error occurs in i-net Clear Reports for the first time. If you use other Java components, then check how much memory is used by these components when i-net Clear Reports is deactivated.
  • Data Fetching: By default i-net Clear Reports reads all data into the memory before rendering the report.
  • Rendering: While rendering, i-net Clear Reports temporarily stores some data in the memory, however this is usually very minimal - approximately the memory for 3-4 pages of the document.
  • Rendering Results: Depending on the cache type, the resulting document might be cached in the memory (MemoryCache).

Reducing memory on data fetching

First you should calculate the needed memory. The memory that is needed for the pure data is approximately:

[field count] * 16 bytes * rows
Fields are: database fields , SQL expression fields, summary fields, and formula fields with evaluation time WhileReadingRecords.

For String fields you’ll need to calculate additional 2 bytes for every character.

Additionally, some JDBC drivers cache types and also stores the complete results in the memory. This can be a large problem if your “Record Selection Formula” is not executable directly on the database or you fetch a lot of records.

If you calculate that the memory used for the data is larger than 10% of the memory that is available for the Java VM, then it could be the problem. You can reduce the amount of memory needed with the following actions:

  • Make sure the Record Selection Formula is executable on the database.
  • You can set a Row Buffer limit if it is possible for your report. You can set it in the i-net Designer dialog “Report | Document Properties | Row Buffer]]”.
  • If your report includes one or more groups then you can split the report into a main and a sub report (see below).

Splitting large reports into main and sub reports

If your report has many rows (for example a hundred thousand or more) and you have one or more groups in the report then you can split it into a main and sub report. The idea is that the main report has only one row per group. And an instance of the sub report has only the rows for one group. This causes the count of rows and columns to be reduced.

Steps to do this:

  • Create a backup of your report. You will need it if you make a mistake or you do not receive the result that you want.
  • Create 2 files of the report. We will call them main.rpt and sub.rpt in the next steps.
  • Open sub.rpt in the Designer.
  • Delete all content from the page header, page footer, report header and report footer.
  • Delete the first group.
  • Remove unused Summary Fields.
  • Remove unused Formula Fields.
  • Remove unused tables.
  • Save sub.rpt.
  • Open main.rpt.
  • Delete the content of the detail area.
  • Hide the Detail Area.
  • Delete all groups except the first group.
  • Insert a sub report into the group footer. Select sub.rpt as your sub report.
  • Add a sub report link to the sub report with the Group Field as the link.
  • Remove all Sort Fields except the first group.
  • Remove unused Summary Fields.
  • Remove unused Formula Fields.
  • Remove unused tables.
  • Save main.rpt.

Now we want to reduce the count of rows for every group in the main report to 1. If your group header or footer has no summary fields that are based on the group then you can enable “Select distinct rows” in the “Summary Info” dialog. If you use summary fields based on the group then it is more difficult.

  • Note all the summary field columns and it’s summary operation.
  • Open the “Visual Database Wizard”.
  • Click on the button “To SQL”
  • Now, you’ll need to add an aggregate function around every column that you use in a summary. All the other columns need to add to the new GROUP BY clause. For example you might see a SQL statement like “SELECT table.col1, table.col2, table.col3, table.col4 From table table ORDER BY table.col1”. In this case, your new SQL should look like: SELECT table.col1, sum(table.col2) as sum_col2, count(table.col3) as count_col3, table.col4 From
    table table GROUP BY table.col1, table.col4 ORDER BY table.col1
  • Remove the Summary Fields and replace it with the new fields.

Reducing memory on saving results

How large are the rendering results? Save the result as a file. Is it many mega bytes large? If you use large images or many small images, then this is a major factor on large rendering results. That applies to your JavaBeans using image mode, as well. With file formats that do not support drawing (for example), charts can also be a problem. Remove these image objects or reduce their pixel size.

If you can’t change the L&F then you’ll need to use a hard disk or database cache. This works with the i-net Clear Reports Plus edition, only. If you want to test whether it solves your problem, then you can request a trial license key for the i-net Clear Reports PLUS edition from our support team.

Other solutions

  • Reduce the count of parallel rendering reports with the general option “Max Active Requests”. With a value of 1, only one report will be rendering at the same time.
  • Last but not least, it could be a Out Of Memory Error Bug like an infinite loop.

See Also

© Copyright 1996 - 2018, i-net software; All Rights Reserved.