تحسين استخدام الذاكرة أثناء العمل مع ملفات كبيرة تحتوي على مجموعات بيانات كبيرة

تحسين الذاكرة

قراءة ملفات 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);

كتابة ملفات إكسيل الكبيرة

توضح المثال التالي كيفية كتابة مجموعة بيانات كبيرة إلى ورقة عمل بوضع محسن.

// 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، RowCollection و Row، سيتم تحقيق أقصى أداء مع MemorySetting.MEMORY_PREFERENCE.
  2. إدراج وحذف الخلايا والصفوف: يرجى ملاحظة أنه إذا كانت هناك الكثير من عمليات الإدراج/الحذف للخلايا/الصفوف، سيكون هناك تدهور أداء ملحوظ لوضع MemorySetting.MEMORY_PREFERENCE مقارنة بوضع MemorySetting.NORMAL.
  3. العمل على أنواع مختلفة من الخلايا: إذا كان معظم الخلايا تحتوي على قيم سلسلة أو صيغ، فإن تكلفة الذاكرة ستكون نفسها كوضع MemorySetting.NORMAL ولكن إذا كانت هناك الكثير من الخلايا الفارغة، أو قيم الخلايا هي رقمية، بوليانية وما إلى ذلك، فإن الخيار MemorySetting.MEMORY_PREFERENCE سيعطي أداءً أفضل.