使用cad-java 23.7 版本,把一个250兆的.stl文件转成pdf ,内存飙到了11G,这个吃内存太厉害了,很容易导致内存溢出

下面是我的代码, 有什么办法减少内存使用,或者可以设置超过多少内存,就到磁盘处理,不然这个根本没法使用,一点就内存溢出了

    public synchronized Path doCadToPdf(InputStream inputStream, String outputFilePath)  throws IOException {

        Path outputPath = Paths.get(outputFilePath);
        LoadOptions opts = new LoadOptions();
        opts.setSpecifiedEncoding(CodePages.SimpChinese);
        opts.setCustomFontFolders(new String[]{fileProperties.getCustomFontFolder()});
        CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
        cadRasterizationOptions.setBackgroundColor(Color.getWhite());
        cadRasterizationOptions.setPageWidth(fileProperties.getNewImageHeight());
        cadRasterizationOptions.setPageHeight(fileProperties.getNewImageWeight() + 105F);
        cadRasterizationOptions.setAutomaticLayoutsScaling(true);
        cadRasterizationOptions.setNoScaling(false);
        cadRasterizationOptions.setDrawType(1);
        PdfOptions pdfOptions = new PdfOptions();
        pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
        try (Image cadImage = Image.load(inputStream, opts);
             OutputStream stream = Files.newOutputStream(outputPath)) {
            cadImage.save(stream, pdfOptions);
            return outputPath;
        }
    }

@huangzx

Cause

The high memory usage when converting a large .stl file to PDF is likely due to the size and complexity of the file. The Aspose.CAD library processes the entire file in memory, which can lead to excessive memory consumption, especially with large files.

Solution

To reduce memory usage during the conversion, you can try the following strategies:

  1. Use Streamed Processing: Instead of loading the entire file into memory, consider processing it in chunks or using a streamed approach if supported by the library.

  2. Adjust Rasterization Options: Modify the CadRasterizationOptions to lower the resolution or quality of the output PDF, which can significantly reduce memory consumption.

  3. Increase Virtual Memory: If your application allows, you can increase the virtual memory settings of your Java environment to handle larger files more gracefully.

  4. Use Temporary Files: If possible, configure the library to use temporary files on disk instead of keeping everything in memory. This can help manage large data sizes.

Code

Here’s a modified version of your code with some adjustments to the rasterization options:

public synchronized Path doCadToPdf(InputStream inputStream, String outputFilePath) throws IOException {
    Path outputPath = Paths.get(outputFilePath);
    LoadOptions opts = new LoadOptions();
    opts.setSpecifiedEncoding(CodePages.SimpChinese);
    opts.setCustomFontFolders(new String[]{fileProperties.getCustomFontFolder()});
    
    CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
    cadRasterizationOptions.setBackgroundColor(Color.getWhite());
    cadRasterizationOptions.setPageWidth(800); // Adjusted width
    cadRasterizationOptions.setPageHeight(600); // Adjusted height
    cadRasterizationOptions.setAutomaticLayoutsScaling(true);
    cadRasterizationOptions.setNoScaling(false);
    cadRasterizationOptions.setDrawType(1);
    
    PdfOptions pdfOptions = new PdfOptions();
    pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
    
    try (Image cadImage = Image.load(inputStream, opts);
         OutputStream stream = Files.newOutputStream(outputPath)) {
        cadImage.save(stream, pdfOptions);
        return outputPath;
    }
}

Next Steps

  1. Test the modified code with smaller dimensions for the PDF output.
  2. Monitor memory usage during the conversion process.
  3. If memory issues persist, consider breaking the .stl file into smaller parts if feasible.

Please implement these suggestions and let me know if you encounter any further issues!

@huangzx,
请提供初始测试文件,以便我们测试并寻找优化内存使用的方法。

这个就是是当时处理的文件 [https://file.yj2025.com/p3/GP_011_P011-LHD_20250620.zip](https://CAD文件)

@huangzx,
我们创建了 CADJAVA-12004 来调查该问题并寻找优化方法。

好了吗,在哪里看 CADJAVA-12004 这个

@huangzx,
任务的状态可在本页底部查看,目前正在等待调查。

@huangzx,
您好。
我们已经调查了内存占用过高的问题。我们会尝试在未来的版本中改进,但遗憾的是,由于文件包含超过 500 万张人脸,对其进行光栅化无论如何都会耗费大量资源,因此无法保证显著的改进。

从您的示例中不清楚画布的宽度和高度的值是什么:

cadRasterizationOptions.setPageWidth(fileProperties.getNewImageHeight());
cadRasterizationOptions.setPageHeight(fileProperties.getNewImageWeight() + 105F);

我们建议考虑以更灵活的方式设置这些尺寸。例如,1000-4000 范围内的尺寸大约需要 7 GB(使用最新的 Aspose.CAD for Java 25.7 测试),并且结果质量相当。

/**
 * 最小宽度(A4纸:1240像素)
 */
private int newImageWeight = 1241;

/**
 * 最小高度度(A4纸:1754像素)
 */
private int newImageHeight = 1754;

@huangzx,
这些值很好,所以最新版本 25.7 比 23.7 更有效,我们会尝试进一步改进它。