大規模なデータセットを含む大きなファイルを操作する際のメモリ使用量の最適化

メモリの最適化

大きな 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すべてのバージョンに適用されます。セルの大きなデータ セットを含むワークブックを作成するなど、状況によっては、MemorySetting.MEMORY_PREFERENCEオプションにより、メモリの使用が最適化され、アプリケーションのメモリ コストが削減される場合があります。ただし、このオプションは、次のような特殊なケースではパフォーマンスを低下させる可能性があります。

  1. Cells にランダムに繰り返しアクセスする注: セル コレクションにアクセスするための最も効率的な順序は、1 つの行でセルごとにアクセスし、次に行ごとに実行することです。特に、Enumerator から取得した行/セルにアクセスする場合Cells, 行コレクション、パフォーマンスは最大化されますMemorySetting.MEMORY_PREFERENCE.
  2. Cells & 行の挿入と削除 Cells/Rows の挿入/削除操作が多い場合、パフォーマンスの低下が顕著になることに注意してください。MemorySetting.MEMORY_PREFERENCEモードと比較してMemorySetting.NORMALモード。
  3. 異なる Cell タイプでの操作 ほとんどのセルに文字列値または数式が含まれている場合、メモリ コストはMemorySetting.NORMALモードですが、空のセルがたくさんある場合、またはセルの値が数値、bool などの場合、MemorySetting.MEMORY_PREFERENCEオプションを使用すると、パフォーマンスが向上します。