Back to Devexpress

How to: Use Pageable Collections

xpo-2075-examples-how-to-use-pageable-collections.md

latest4.4 KB
Original Source

How to: Use Pageable Collections

  • Aug 24, 2020
  • 3 minutes to read

This example provides a memory usage comparison between the XPCollection and XPCursor instances.

Creating and persisting the objects in the database.

csharp
int i;
string dataString = new String('?', 1024);
Session.DefaultSession.ClearDatabase();
Console.WriteLine("Creating objects");
// The loop here creates 1000 objects.
for(i = 0; i<1000; ++i) {
    SampleObject theObject = new SampleObject();
    theObject.Data = dataString;
    theObject.Save();
}
vb
Dim i As Integer
Dim dataString As New String("?", 1024)
Session.DefaultSession.ClearDatabase()
Console.WriteLine("Creating objects")
Dim theObject As SampleObject
' The loop here creates 1000 objects.
For i = 1 To 1000
    theObject = New SampleObject()
    theObject.Data = dataString
    theObject.Save()
Next

Then we iterate through the contents of the XPCollection and XPCursor and modify the values of the retrieved objects. We assess the amount of memory allocated every time one hundred objects have been modified.

csharp
GC.Collect();
i = 0;
Console.WriteLine("Processing objects with XPCursor");
XPCursor cursor = new XPCursor(Session.DefaultSession, typeof(SampleObject));
cursor.PageSize = 100;
// Loop through all the objects.
foreach(SampleObject theObject in cursor) {
    theObject.Data = new String('!', 1024);
    theObject.Save();
    ++i;
     // Every 100 objects write out memory used.
    if(i % 100 == 0)
Console.WriteLine(String.Format("Object {0}, memory allocated {1}", i, GC.GetTotalMemory(true)));
}

GC.Collect();
i = 0;
Console.WriteLine("Processing objects with XPCollection");
using(XPCollection collection = new XPCollection(Session.DefaultSession, typeof(SampleObject))) {
// Loop through all the objects.
    foreach(SampleObject theObject in collection) {
        theObject.Data = new String('@', 1024);
        theObject.Save();
        ++i;
        if(i % 100 == 0)
            // Every 100 objects write out memory used.
            Console.WriteLine(String.Format(" Object {0}, memory allocated {1}", i, 
              GC.GetTotalMemory(true)));
    }
}
vb
GC.Collect()
 i = 0
 Console.WriteLine("Processing objects with XPCursor")
 Dim cursor As New XPCursor(Session.DefaultSession, GetType(SampleObject))
 cursor.PageSize = 100
' Loop through all the objects.
 For Each theObject In cursor
      theObject.Data = New String("!", 1024)
      theObject.Save()
      i = i + 1
      ' Every 100 objects write out memory used.
      If i Mod 100 = 0 Then
          Console.WriteLine(String.Format(" Object {0}, memory allocated {1}", i, _
            GC.GetTotalMemory(True)))
      End If
Next theObject

GC.Collect()
i = 0
Console.WriteLine("Processing objects with XPCollection")
' Loop through all the objects.
For Each theObject In New XPCollection(Session.DefaultSession, GetType(SampleObject))
    theObject.Data = New String("@", 1024)
    theObject.Save()
    i = i + 1
    ' Every 100 objects write out memory used.
    If i Mod 100 = 0 Then
  Console.WriteLine(String.Format("Object {0}, memory allocated {1}", i, GC.GetTotalMemory(True)))
    End If
Next theObject

The table below shows the result:

|

Processing objects with XPCursor

|

Processing objects with XPCollection

| | --- | --- | |

Object 100, memory allocated 382752

Object 200, memory allocated 389152

Object 300, memory allocated 395552

Object 400, memory allocated 386336

Object 500, memory allocated 392736

Object 600, memory allocated 386336

Object 700, memory allocated 392736

Object 800, memory allocated 386336

Object 900, memory allocated 392736

Object 1000, memory allocated 386036

|

Object 100, memory allocated 2568928

Object 200, memory allocated 2568504

Object 300, memory allocated 2568504

Object 400, memory allocated 2568504

Object 500, memory allocated 2568504

Object 600, memory allocated 2568492

Object 700, memory allocated 2568492

Object 800, memory allocated 2568492

Object 900, memory allocated 2568492

Object 1000, memory allocated 2568492

|

From the output you can see that the XPCursor has significantly improved the way in which memory is allocated for the collection’s contents.