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.

5
  • 1
    Could you provide some more code? Commented Aug 18, 2017 at 10:18
  • 2
    Try to bring the data in batches. I guess to experience that issue you must get all the data in one shot from the db to your application server. Commented Aug 18, 2017 at 10:18
  • @notanormie I've edited question with sample code. Commented Aug 18, 2017 at 11:13
  • @alexd yes, we are getting all the data in a single shot, by calling store_proc Commented Aug 18, 2017 at 11:14
  • @ArvindKatte , what is your system configuration? and how mush RAM are you using java heap ? Commented Oct 25, 2017 at 3:52

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I agree for the first point. But now code moved to production level, now we can't change the data structure right.
@ArvindKatte, well, if you won't change anything, nothing'll get fixed.
Today we have call with clients, let's see what approach they will fallow.
your first approach really helps me, I'm creating one temporary variable then writing value to the sheet, then clearing the temporary variable memory. this approach client agreed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.