当保存一个包含1500行或更多行的文件时,会出现这个错误“outofmemoryerror:java heap space”。
我试着解决这个问题拆分列表数据
我试图将这个数据列表拆分成更小的部分,并保存和加载ods文件。我使用de函数insertrowsbefore来追加新行。但这对我没用。当save加载表、追加新行列表并保存时,save函数再次保存一个完整的表,我得到了错误。我的行有三十五列
我尝试使用以下版本:
<dependency>
<groupId>org.odftoolkit</groupId>
<artifactId>simple-odf</artifactId>
<version>0.6.6</version>
</dependency>
和
<dependency>
<groupId>org.apache.odftoolkit</groupId>
<artifactId>simple-odf</artifactId>
<version>0.7-incubating</version>
</dependency>
错误详细信息:
13:45:12,821 INFO [stdout] Excepción: java.lang.OutOfMemoryError: Java heap space
13:45:12,822 INFO [stdout] (asyncExecutor-1) at java.util.Arrays.copyOf(Arrays.java:2271)
13:45:12,822 INFO [stdout] (asyncExecutor-1) at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:113)
13:45:12,822 INFO [stdout] (asyncExecutor-1) at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
13:45:12,822 INFO [stdout] (asyncExecutor-1) at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:140)
13:45:12,823 INFO [stdout] (asyncExecutor-1) at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
13:45:12,823 INFO [stdout] (asyncExecutor-1) at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:282)
13:45:12,823 INFO [stdout] (asyncExecutor-1) at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
13:45:12,823 INFO [stdout] (asyncExecutor-1) at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
13:45:12,823 INFO [stdout] (asyncExecutor-1) at java.io.Writer.write(Writer.java:127)
13:45:12,824 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.Printer.printText(Unknown Source)
13:45:12,824 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.XMLSerializer.printAttribute(Unknown Source)
13:45:12,824 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.XMLSerializer.serializeElement(Unknown Source)
13:45:12,824 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(Unknown Source)
13:45:12,824 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.XMLSerializer.serializeElement(Unknown Source)
13:45:12,824 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(Unknown Source)
13:45:12,825 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.XMLSerializer.serializeElement(Unknown Source)
13:45:12,825 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(Unknown Source)
13:45:12,825 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(Unknown Source)
13:45:12,825 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.BaseMarkupSerializer.serialize(Unknown Source)
13:45:12,826 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.DOMSerializerImpl.write(Unknown Source)
13:45:12,826 INFO [stdout] (asyncExecutor-1) at org.odftoolkit.odfdom.pkg.OdfPackage.flushDom(OdfPackage.java:1626)
13:45:12,826 INFO [stdout] (asyncExecutor-1) at org.odftoolkit.odfdom.pkg.OdfPackage.getBytes(OdfPackage.java:1566)
13:45:12,826 INFO [stdout] (asyncExecutor-1) at org.odftoolkit.odfdom.pkg.OdfPackage.save(OdfPackage.java:877)
13:45:12,827 INFO [stdout] (asyncExecutor-1) at org.odftoolkit.odfdom.pkg.OdfPackage.save(OdfPackage.java:823)
13:45:12,827 INFO [stdout] (asyncExecutor-1) at org.odftoolkit.simple.Document.save(Document.java:728)
13:45:12,827 INFO [stdout] (asyncExecutor-1) at org.odftoolkit.odfdom.pkg.OdfPackageDocument.save(OdfPackageDocument.java:228)
自定义函数和服务的日志
13:45:12,828 INFO [stdout] (asyncExecutor-1) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
13:45:12,828 INFO [stdout] (asyncExecutor-1) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
13:45:12,828 INFO [stdout] (asyncExecutor-1) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
13:45:12,828 INFO [stdout] (asyncExecutor-1) | java.lang.OutOfMemoryError: Java heap space
13:45:12,828 INFO [stdout] (asyncExecutor-1) at java.util.Arrays.copyOf(Arrays.java:2271)
13:45:12,828 INFO [stdout] (asyncExecutor-1) at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:113)
13:45:12,828 INFO [stdout] (asyncExecutor-1) at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
13:45:12,828 INFO [stdout] (asyncExecutor-1) at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:140)
13:45:12,828 INFO [stdout] (asyncExecutor-1) at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
13:45:12,829 INFO [stdout] (asyncExecutor-1) at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:282)
13:45:12,829 INFO [stdout] (asyncExecutor-1) at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
13:45:12,829 INFO [stdout] (asyncExecutor-1) at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
13:45:12,829 INFO [stdout] (asyncExecutor-1) at java.io.Writer.write(Writer.java:127)
我的出口代码:
public static <T> void export(String[] columnTitle, List<T> data, String[] fields, String path, String filename) throws Exception {
try {
SpreadsheetDocument ods = SpreadsheetDocument.newSpreadsheetDocument();
ods.removeSheet(0);
Table table = Table.newTable(ods, data.size() + MagicNumber.TWO, columnTitle.length, 0, 0);
table.setTableName(filename);
String[] values = new String[fields.length];
int line = 0;
Border border = new Border(Color.BLACK, BORDER_WIDTH, StyleTypeDefinitions.SupportedLinearMeasure.PT);
List<Row> rows = table.getRowList();
Row r = rows.get(line);
Cell cell = r.getCellByIndex(0);
//Style for column title
Font font = new Font(ARIAL_FONT, FontStyle.BOLD, MagicNumber.SEVEN, Color.BLACK);
r = rows.get(line);
for (int a = 0; a < fields.length; a++) {
cell = r.getCellByIndex(a);
cell.setStringValue(columnTitle[a]);
cell.setBorders(CellBordersType.ALL_FOUR, border);
cell.setCellBackgroundColor(Color.valueOf("#A5A5A5"));
cell.setFont(font);
cell.setHorizontalAlignment(HorizontalAlignmentType.CENTER);
}
//write data
font = new Font(ARIAL_FONT, FontStyle.REGULAR, MagicNumber.SEVEN);
for (T obj : data) {
line++;
r = rows.get(line);
for (int i = 0; i < fields.length; i++) {
cell = r.getCellByIndex(i);
values[i] = getProperty(obj, fields[i]);
cell.setStringValue(String.valueOf(values[i]));
cell.setBorders(CellBordersType.ALL_FOUR, border);
cell.setFont(font);
cell.setHorizontalAlignment(HorizontalAlignmentType.RIGHT);
}
}
//Error with 2000 results
ods.save(path);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
LOG.error(e);
} catch (Exception e) {
LOG.error(e);
}
}
1条答案
按热度按时间wsewodh21#
在得到gcoutofmemory错误之前,我不得不写200000行。
为了解决这个问题,我在for循环中添加了一个save every 1000 rows(save\ point below)
我在一些论坛上读到,简单的odf对于大型电子表格不是很好,所以如果你有任何进一步的问题,可以尝试另一个库,比如poi。