大規模なデータセットを含む大きなファイルを操作する際のメモリ使用量の最適化
Contents
[
Hide
]
大規模なデータ セットを含むワークブックを作成する場合、または大きな Microsoft Excel ファイルを読み取る場合、プロセスが使用する RAM の総量が常に懸念されます。課題に対処するために適応できる対策があります。 Aspose.Cells は、いくつかの関連するオプションと API 呼び出しを提供して、メモリ使用量を削減、削減、および最適化します。また、プロセスがより効率的に機能し、より速く実行するのに役立ちます.
使用MemorySetting.MEMORY_PREFERENCEセル データに使用されるメモリを最適化して、全体的なメモリ コストを削減するオプション。セルの大きなデータ セットを構築する場合、既定の設定を使用する場合と比較して、一定量のメモリを節約できます。MemorySetting.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(); |
注意
デフォルトのオプション、MemorySetting.NORMALすべてのバージョンに適用されます。セルの大きなデータ セットを含むワークブックを作成するなど、状況によっては、MemorySetting.MEMORY_PREFERENCEオプションにより、メモリの使用が最適化され、アプリケーションのメモリ コストが削減される場合があります。ただし、このオプションは、次のような特殊なケースではパフォーマンスを低下させる可能性があります。
- Cells にランダムに繰り返しアクセスする注: セル コレクションにアクセスするための最も効率的な順序は、1 つの行でセルごとにアクセスし、次に行ごとに実行することです。特に、Enumerator から取得した行/セルにアクセスする場合Cells, 行コレクションと行、パフォーマンスは最大化されますMemorySetting.MEMORY_PREFERENCE.
- Cells & 行の挿入と削除 Cells/Rows の挿入/削除操作が多い場合、パフォーマンスの低下が顕著になることに注意してください。MemorySetting.MEMORY_PREFERENCEモードと比較してMemorySetting.NORMALモード。
- 異なる Cell タイプでの操作 ほとんどのセルに文字列値または数式が含まれている場合、メモリ コストはMemorySetting.NORMALモードですが、空のセルがたくさんある場合、またはセルの値が数値、bool などの場合、MemorySetting.MEMORY_PREFERENCEオプションを使用すると、パフォーマンスが向上します。