We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Higih memory consumption in Cell Styles

Hi,

Cell cell = ...;

Style style = cell.getStyle();

//modify the style.

cell.setStyle(style);

The above code fragment is executed for a large number of cells(>100,000) in the worksheet. When i profiled the code I saw that a large number of Color Objects(>100,000) are being created even if I do not change the color value of the cell's style.

To fix this memory issue I created a cache of reusable style objects - I need only 5 to 10 distinc style objects for any worksheet. Now the code is as follows

Style style = ExcelCellStyleFactory.getCellStyle(cell, stylePamaters);

cell.setStyle(style);

ExcelCellStyleFactory{

public static Style getCellStyle(Cell cell, StylePamaters stylePamaters) throws ReporterRunTimeException{

private static Map cellStyleMap = new HashMap(); // The cache of reusable style objects.

String styleKey = getCellStyleKey(element);// Generates a unique key for the stylePamaters object.

Style style = (Style)cellStyleMap.get(styleKey);

if(style == null){

style = constructNewCelStyle(cell,element);

cellStyleMap.put(styleKey, style);

}return style;

}

private static Style constructNewCelStyle(Cell cell,ElementDTO element)

Style style = cloneStyle(cell.getStyle());

style.setBorderColor.. // Applying the various formating style to style object

.....

}

private static Style cloneStyle(Style style){

style = (Style)style.clone();

return style;

}

}

This fixes the memory issue. But i have another problem. All the cells have borders by default even if i do not set any borders!!

To confirm that there is no problem in the code i have written I made the following changes to fic the borders problem.

Style style = ExcelCellStyleFactory.getCellStyle(cell,element);

Style s2 = cell.getStyle();

s2.copy(style);

cell.setStyle(style);

The above code fixes the borders problem but brings back the Memory issue as it is copying the style.

Now the question I have is, can i reuse Style objects for multipe cells ?

If, yes, How do i fix the borders problem.

If No, How do i fix the memory problem.

Please advice.

Hi,

Thanks for sharing some details.

For memory problem, you have to make sure that you given enough memory to JVM. I think you may try some command lines, e.g..,

java -Xms512m -Xmx512m TestExample ......... you allot more memory appropriately.

And, we will check the border issue soon.

Thank you.

Hi,

For the issue regarding border when reusing Style object for multiple cells, I think maybe it is due to the fact that you have modified one Style object twice or improperly. It is sure that Aspose.Cells for Java supports reusing one Style object for multiple cells, and for performance consideration, we recommend user to reuse Style object to save memory.

Following is my sample code for reusing Style object, and it works fine:

Workbook workbook = new Workbook();
Worksheets worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.getSheet(0);
Cells cells = sheet.getCells();
Cell cell1 = cells.getCell(0, 1);
Style st1 = cell1.getStyle();
st1.setBorderLine(BorderType.LEFT, BorderLineType.DOUBLE);
st1.setBorderColor(BorderType.LEFT, Color.RED);
st1.setBorderLine(BorderType.RIGHT, BorderLineType.DOUBLE);
st1.setBorderColor(BorderType.RIGHT, Color.BLUE);

Cell cell2 = cells.getCell(0, 7);
Style st2 = cell2.getStyle();
st2.setColor(Color.GREEN);

for(int i=0; i<20; i++)
{
cells.getCell(i, 1).setStyle(st1);
cells.getCell(i, 4).setStyle(st1);
cells.getCell(i, 7).setStyle(st2);
cells.getCell(i, 9).setStyle(st2);
}

workbook.save("ttt.xls");

But one thing needs to be noted here, if you apply one Style object to multiple cells, when you modify the Style object that is obtained from any one of those cells by the method named getStyle() later, all the style of those cells will be changed too, because all those cells' style is referring to the same one object. So, please check your code and make sure you have not fetched or modified any existing Style object applied for some cells and have not taken it as new Style to apply for other cells. From your code snippet, we are not sure where the border issue lies. We appreciate if you can create a sample project to reproduce the issue, so, we can trace it.

Thank you.

Thank you.

Hi Amjad,

Thanks for the quick reply.

The Border problem is with my code and not Aspose.Cells. We have abstacted aspose behind our own classes. But then some of the Aspose.Cells code had leaked to another class accidentally and hence the border problem - I did not check that other class. I will be refactoring that code and moving it to its correct place.

One other problem I faced was that I could not extend the Style class (It has a private/protected constructor). If I could i would have made the Factory return immutable Style objects. This would ensure that the Style objects cannot be modified anywhere else.

It would have been even better if Style was an interface and not a class.

Hi,

Thanks for updating us (as you have corrected your codes regarding Border issue) and sharing your concerns,

We will get back to you soon.

Thanks for being patient!

Hi,

For high memory consumption issue in Cell Styles, we will look into it to provide a more convenient way for end users, that is, when a user call Cell.setStyle()/Row.setStyle()/Column.setStyle(), Aspose.Cells will gather Style objects automatically to decrease the counting of Style objects, hence it would decrease the memory consumption to certain extent. In this way, end users do not need to gather Style objects by themselves, such as, what your ExcelCellStyleFactory does. We will support this feature in about two months after we finish some important tasks on hand.
Hope, you understand it,
Thank you.