在处理具有大型数据集的大文件时优化内存使用
Contents
[
Hide
]
在构建包含大型数据集的工作簿或读取大型 Microsoft Excel 文件时,进程占用的 RAM 总量始终是一个问题。可以采取一些措施来应对挑战。 Aspose.Cells 提供了一些相关选项和 API 调用以降低、减少和优化内存使用。此外,它还可以帮助流程更有效地工作并运行得更快。
采用内存设置.MEMORY_PREFERENCE用于优化用于单元格数据的内存以降低整体内存成本的选项。为单元格构建大数据集时,与使用默认设置相比,可以节省一定的内存内存设置.NORMAL.
优化内存
读取大型 Excel 文件
以下示例显示如何在优化模式下读取大型 Microsoft Excel 文件。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ReadLargeExcelFiles.class); | |
// Specify the LoadOptions | |
LoadOptions opt = new LoadOptions(); | |
// Set the memory preferences | |
opt.setMemorySetting(MemorySetting.MEMORY_PREFERENCE); | |
// Instantiate the Workbook | |
// Load the Big Excel file having large Data set in it | |
Workbook wb = new Workbook(dataDir + "Book1.xlsx", opt); |
编写大型 Excel 文件
以下示例说明如何在优化模式下将大型数据集写入工作表。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(WritingLargeExcelFiles.class) + "articles/"; | |
// Instantiate a new Workbook | |
Workbook wb = new Workbook(); | |
// Set the memory preferences | |
// Note: This setting cannot take effect for the existing worksheets that are created before using the below line of code | |
wb.getSettings().setMemorySetting(MemorySetting.MEMORY_PREFERENCE); | |
/* | |
* Note: The memory settings also would not work for the default sheet i.e., "Sheet1" etc. automatically created by the | |
* Workbook. To change the memory setting of existing sheets, please change memory setting for them manually: | |
*/ | |
Cells cells = wb.getWorksheets().get(0).getCells(); | |
cells.setMemorySetting(MemorySetting.MEMORY_PREFERENCE); | |
// Input large dataset into the cells of the worksheet.Your code goes here. | |
// Get cells of the newly created Worksheet "Sheet2" whose memory setting is same with the one defined in | |
// WorkbookSettings: | |
cells = wb.getWorksheets().add("Sheet2").getCells(); |
警告
默认选项,内存设置.NORMAL适用于所有版本。对于某些情况,例如构建包含大量单元格数据集的工作簿,内存设置.MEMORY_PREFERENCE选项可以优化内存使用并降低应用程序的内存成本。但是,此选项可能会在某些特殊情况下降低性能,例如以下。
- 随机重复访问Cells :访问单元格集合的最有效顺序是在一行中逐个单元格,然后逐行。特别是,如果您通过从中获取的枚举器访问行/单元格Cells, 行集合和排,性能将最大化内存设置.MEMORY_PREFERENCE.
- 插入和删除 Cells & 行 请注意,如果对 Cells/Rows 有大量的插入/删除操作,性能下降将是显着的内存设置.MEMORY_PREFERENCE模式相比内存设置.NORMAL模式。
- 操作不同的 Cell 类型:如果大多数单元格包含字符串值或公式,则内存成本将与内存设置.NORMAL模式,但如果有很多空单元格,或者单元格值为数字、布尔值等,则内存设置.MEMORY_PREFERENCE选项将提供更好的性能。