3

I know there all lot of questions on java.lang.OutOfMemoryError: Java heap space.

like question 1

But none of the links are not proper answers to my question.

We are generating reports in spreadsheet format, where huge data is coming from database side. We increased the heap memory size from 2 GB to 4 GB, no use.

May be because of some extra space in database column, so I trimmed all the getters and setters using trim() method, this is also no use.

For Example :

String s = "Hai       ";
s.trim();

If any one having how to resolve this issue from Java coding side, without increasing the size of the heap space. Because client told, they will not increase the heap space any more.

When calling this method getting exception

private CrossTabResult mergeAllListsSameNdc(CrossTabResult crt, CrossTabResult res) {

        crt.setFormularyTier(crt.getFormularyTier()==null ? "":((crt.getFormularyTier().contains(crt.getListId())? crt.getFormularyTier(): crt.getListId()+crt.getFormularyTier()) +"~"+res.getListId()+res.getFormularyTier()));
        crt.setFormularyTierDesc(crt.getFormularyTierDesc()==null ? "":((crt.getFormularyTierDesc().contains(crt.getListId())? crt.getFormularyTierDesc(): crt.getListId()+crt.getFormularyTierDesc()) +"~"+res.getListId()+res.getFormularyTierDesc()));}

Can't share more code, due to confidential. By looking above code, if you people have any alternative solution means inform me. We are merging two String based on the same id.

Arvind Katte
  • 995
  • 2
  • 10
  • 20

1 Answers1

2

We are generating reports in spreadsheet format, where huge data is coming from database side.

In this kind of use cases, you have at least two things to study that may improve the consumed memory but first you have to identify the culprits.

Mainly causes identified by monitoring tools in this use case are generally :

1) If data loaded from the DB is identified as a big consumer of memory, you should probably not load all data in one shot.
Keeping all theses objects in memory and in the same time creating the spreadsheet from these data may consume a lot of memory.
Overall if the application is parallely used for other use cases. You should rather divide the retrieval in several retrievals.
Then invoke one to retrieve some objects, populate the spreadsheat and free these objects as not required any longer. And so on...

2) During the spreadsheet creation, if the object spreadsheet created with the library was identified as a big consumer of memory, you should favor streaming API or event API to write the spreadsheet over API that loads the whole spreadsheet in memory.
For example POI provides a DOM-like API : XSSF and a streaming API : SXSSF.
You don't specify the library use to create the spreadsheet but it doesn't matter as the logic to use should be the same for anyone.

davidxxx
  • 125,838
  • 23
  • 214
  • 215