大規模なデータセットを扱う場合のメモリ使用量を最適化する

メモリの最適化

大きなExcelファイルの読み取り

以下の例は、最適化モードで大きなMicrosoft Excelファイルを読み取る方法を示しています。

// 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ファイルの書き込み

大きなデータセットを最適モードでワークシートに書き込む方法を示す例が以下に示されています。

// 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();

注意

デフォルトオプションMemorySetting.NORMALはすべてのバージョンで適用されます。大量のセルデータセットを持つワークブックを構築するなどの一部の状況では、メモリ使用を最適化し、アプリケーションのメモリコストを減らすことができるかもしれませんが、このオプションは特定の状況ではパフォーマンスを低下させる可能性があります。

  1. ランダムで繰り返しセルにアクセス: セルのコレクションにアクセスする最も効率的なシーケンスは、行ごとに1つずつセルにアクセスし、その後行ごとにアクセスすることです。特に、CellsRowCollectionおよびRowから取得したEnumeratorで行/セルにアクセスする場合、パフォーマンスはMemorySetting.MEMORY_PREFERENCEで最適化されます。
  2. セルや行の挿入および削除:多くの挿入/削除操作がセル/行にある場合、MemorySetting.MEMORY_PREFERENCEモードでは、MemorySetting.NORMALモードと比較してパフォーマンスの低下が顕著になります。
  3. 異なるセルタイプでの操作:ほとんどのセルが文字列値や数式を含む場合、メモリコストはMemorySetting.NORMALモードと同じになりますが、空のセルが多いか、セルの値が数値、ブール値などの場合、MemorySetting.MEMORY_PREFERENCEオプションはパフォーマンスが向上します。