简体   繁体   English

Jasper 报告导出到文本导致 java.lang.OutOfMemoryError: Java heap space

[英]Jasper report exporting to text results in java.lang.OutOfMemoryError: Java heap space

Currently I am using jasper v 3.7.4目前我正在使用 jasper v 3.7.4

While exporting to any other format ( csv , xls , pdf ) from the same dataset - I have has no issues.从同一dataset导出到任何其他格式( csvxlspdf )时 - 我没有问题。

Exporting to text throws:导出到text抛出:

java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:3236) at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118) java.lang.OutOfMemoryError: java.util.Arrays.copyOf(Arrays.java:3236) 处的 Java 堆空间 java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)

Here is my code:这是我的代码:

    byte[] bytes = null;
    JRTextExporter  exporter = new JRTextExporter();
    ByteArrayOutputStream txtReport = new ByteArrayOutputStream();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, txtReport);
    exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "mytxt.txt");
    exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
    exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, 2.0F);
    exporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, 100.0F);
    exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, 4.0F);
    exporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, 50.0F);
    
    exporter.exportReport();
    bytes = txtReport.toByteArray();

    FileOutputStream fos = new FileOutputStream("c:\\myfile.txt")
    System.out.println(bytes.length/1024+" Kbytes");
    fos.write(bytes, 0, bytes.length);
    fos.flush();
    fos.close();

I am also using a virtualizer as jasperPrint parameter我也使用virtualizer作为jasperPrint参数

 JRFileVirtualizer virtualizer = new JRFileVirtualizer(150);
 virtualizer.setReadOnly(false);
 params.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);

But it does not help, and also the following warning is shown:但它没有帮助,并且还显示以下警告:

WARNING: Parameter "REPORT_VIRTUALIZER" already registered, skipping this one.警告:参数“REPORT_VIRTUALIZER”已经注册,跳过这个。

Given that csv is also a " text " file and is generated without any problem, it is kind of strange that exporting to text fails.鉴于csv也是一个“ text ”文件并且生成没有任何问题,导出到text失败有点奇怪。

May be something is wrong with the parameters I provide for the exporter?我为出口商提供的参数可能有问题?

Thanks.谢谢。

While text and csv may both be text based formats, they're not the same format and therefore do not take the same amount of space.虽然textcsv可能都是基于文本的格式,但它们不是相同的格式,因此不会占用相同的空间。

Generating anything (usually reports) to memory is hazardous since while it may work in testing, a large report in production causing an OOME will wreak havoc.任何内容(通常是报告)生成到内存中是危险的,因为虽然它可能在测试中起作用,但在生产中导致 OOME 的大型报告将造成严重破坏。

Use a real stream when generating things.生成事物时使用真实的流。 A FileOutputStream , network stream, any proper stream that doesn't store things in memory.一个FileOutputStream ,网络流,任何不在内存中存储东西的适当流。 If you're using a ByteArrayOutputStream for "real work", you're most likely doing something wrong.如果您将ByteArrayOutputStream用于“实际工作”,那么您很可能做错了什么。

Immediately output the report to the file, instead of first collecting it.立即将报告输出到文件中,而不是先收集它。

try (FileOutputStream fos = new FileOutputStream("c:\\myfile.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos)) {
    JRTextExporter  exporter = new JRTextExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, bos);
    exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "mytxt.txt");
    exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
    exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, 2.0F);
    exporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, 100.0F);
    exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, 4.0F);
    exporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, 50.0F);

    exporter.exportReport();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM