Monitoring Database Storage in C#

C# applications can obtain database page statistics directly from the Connection class properties FreePages, TotalPages and DbPageSize. Also, the Connection methods GetClassStats() and GetIndexStat() return the class and index statistics, and property IndexStatCount gives the number of indexes in the database.

The following code snippet from sample "samples/csharp/Statistics" demonstrates how these statistics can be retrieved:

 
    Console.WriteLine("Total pages: {0}", con.TotalPages);
    Console.WriteLine("Free pages:  {0}", con.FreePages);
    Console.WriteLine("Page size:   {0}", con.DbPageSize);
 
    con.StartTransaction(Database.TransactionType.ReadOnly);
    for (int cls = 1; cls <= parameters.Classes.Length; cls++)
    {
        ClassStat stat = con.GetClassStat(cls);
        Console.WriteLine("Class {0}: {1} objects, {2} pages, {3} blob pages",
            cls, stat.ObjectsCount, stat.CorePageCount, stat.BlobPages);
    }
 
 
    con.RollbackTransaction();
    Console.WriteLine();
    con.StartTransaction(Database.TransactionType.ReadOnly);
    short ind_num = con.IndexStatCount;
    for (short idx = 0; idx < ind_num; idx++)
    {
        IndexStat stat = con.GetIndexStat(idx);
        Console.WriteLine("{0}: {1} key(s) {2} page(s) ", stat.Label, stat.KeysCount, stat.PagesCount);
    }
    con.RollbackTransaction();
     

Persistent Databases

C# applications can obtain information about database storage on persistent media directly from the Connection class method GetDiskInfo() which returns properties DataFileSize, UsedDatabaseSize and LogFileSize.

The following code snippet from sample "samples/csharp/Statistics" demonstrates how these sizes can be retrieved:

 
    DiskInfo di = con.GetDiskInfo();
    Console.WriteLine("Disk file size: {0}, Used: {1}, Transaction log file size: {2}",
        di.DataFileSize, di.UsedDatabaseSize, di.LogFileSize);