大規模なデータセットを扱う場合のメモリ使用量を最適化する
大規模なデータセットを含むワークブックを構築したり、大きなMicrosoft Excelファイルを読み込んだりする際に、プロセスが使用する合計RAM量は常に懸念事項です。Aspose.Cellsは、メモリ使用量を低減、削減、最適化するための関連するオプションやAPI呼び出しを提供します。これにより、プロセスが効率的に動作し、高速に動作するようにすることができます。
セルのデータのメモリ使用を最適化し、総合的なメモリコストを減らすために MemorySetting.MemoryPreference オプションを使用してください。大規模なセルデータセットを構築する際、デフォルトの設定(MemorySetting.Normal)に比べて一定量のメモリを節約することができます。
メモリの最適化
大きなExcelファイルの読み取り
以下の例は、最適化モードで大きなMicrosoft Excelファイルを読み取る方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Specify the LoadOptions | |
LoadOptions opt = new LoadOptions(); | |
// Set the memory preferences | |
opt.MemorySetting = MemorySetting.MemoryPreference; | |
// 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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// 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.Settings.MemorySetting = MemorySetting.MemoryPreference; | |
// 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.Worksheets[0].Cells; | |
cells.MemorySetting = MemorySetting.MemoryPreference; | |
// 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.Worksheets.Add("Sheet2").Cells; | |
// ......... | |
// Input large dataset into the cells of the worksheet. | |
// Your code goes here. | |
// ......... |
注意
デフォルトオプションである MemorySetting.Normal はすべてのバージョンで適用されます。セルの大規模なデータセットを使用するなど、アプリケーションのメモリ使用を最適化し、メモリコストを減らすためには MemorySetting.MemoryPreference オプションを使用することができます。ただし、このオプションは次のような特殊なケースにおいてパフォーマンスが低下する可能性があります。
- ランダムで繰り返しセルにアクセス: セルのコレクションにアクセスする最も効率的なシーケンスは、行ごとに1つずつセルにアクセスし、その後行ごとにアクセスすることです。特に、Cells、RowCollectionおよびRowから取得したEnumeratorで行/セルにアクセスする場合、パフォーマンスはMemorySetting.MemoryPreferenceで最適化されます。
- セルや行の挿入・削除: セル/行の挿入/削除が多い場合、MemoryPreferenceモードのパフォーマンス劣化がNormalモードと比較して顕著になります。
- 異なるセルタイプ間での操作: ほとんどのセルが文字列値や数式を含む場合、メモリコストはNormalモードと同じになりますが、空のセルが多い場合やセルの値が数値、ブール値などの場合、MemorySetting.MemoryPreference オプションの方がパフォーマンスが向上します。