Get Total Memory

image_pdfimage_print
   
  
using System;


public class MyClass {

    public void Dispose() {
        Console.WriteLine("Dispose()");
        GC.SuppressFinalize(this);
    }
}

public class ReRegFinalApp {

    public static void DoSomething() {
        MyClass t = new MyClass();
        Console.WriteLine(t);
        t.Dispose();
        GC.ReRegisterForFinalize(t);

    }

    public static void Main(string[] args) {
        long n = GC.GetTotalMemory(true);
        Console.WriteLine("start of Main: {0} bytes allocated", n);
        DoSomething();
        n = GC.GetTotalMemory(true);
        Console.WriteLine("end of Main: {0} bytes allocated", n);
    }
}

   
     


MaxGeneration is zero based.

image_pdfimage_print
   
  

using System;
using System.Collections.Generic;
using System.Text;

public class Car {
    private int currSp;
    private string petName;

    public Car() { }
    public Car(string name, int speed) {
        petName = name;
        currSp = speed;
    }

    public override string ToString() {
        return string.Format("{0} is going {1} MPH",
            petName, currSp);
    }
}
class Program {
    static void Main(string[] args) {
        Console.WriteLine("Estimated bytes on heap: {0}", GC.GetTotalMemory(false));
        Console.WriteLine("This OS has {0} object generations.
", (GC.MaxGeneration + 1));

        Car refToMyCar = new Car("Zippy", 100);
        Console.WriteLine(refToMyCar.ToString());

        Console.WriteLine("
Generation of refToMyCar is: {0}", GC.GetGeneration(refToMyCar));
    }

    public static void MakeACar() {
        Car myCar = new Car();
    }
}

   
     


Estimated bytes on heap

image_pdfimage_print
   
  

using System;
using System.Collections.Generic;
using System.Text;

class Program {
    static void Main(string[] args) {
        Console.WriteLine("Estimated bytes on heap: {0}", GC.GetTotalMemory(false));
        Console.WriteLine("This OS has {0} object generations.
", (GC.MaxGeneration + 1));
    }
}

   
     


Check the Generation for an object array

image_pdfimage_print

using System;
using System.Collections.Generic;
using System.Text;

public class Car {
private int currSp;
private string petName;

public Car() { }
public Car(string name, int speed) {
petName = name;
currSp = speed;
}

public override string ToString() {
return string.Format(“{0} is going {1} MPH”,
petName, currSp);
}
}
class Program {
static void Main(string[] args) {
Car refToMyCar = new Car(“Zippy”, 100);
Console.WriteLine(refToMyCar.ToString());
Console.WriteLine(GC.GetGeneration(refToMyCar));

object[] tonsOfObjects = new object[50000];
for (int i = 0; i < 50000; i++) tonsOfObjects[i] = new object(); GC.Collect(0); GC.WaitForPendingFinalizers(); Console.WriteLine(GC.GetGeneration(refToMyCar)); } public static void MakeACar() { Car myCar = new Car(); } } [/csharp]

If object array is still alive

image_pdfimage_print

using System;
using System.Collections.Generic;
using System.Text;

public class Car {
private int currSp;
private string petName;

public Car() { }
public Car(string name, int speed) {
petName = name;
currSp = speed;
}

public override string ToString() {
return string.Format(“{0} is going {1} MPH”,
petName, currSp);
}
}
class Program {
static void Main(string[] args) {
Car refToMyCar = new Car(“Zippy”, 100);
Console.WriteLine(refToMyCar.ToString());

Console.WriteLine(”
Generation of refToMyCar is: {0}”, GC.GetGeneration(refToMyCar));

object[] tonsOfObjects = new object[50000];
for (int i = 0; i < 50000; i++) tonsOfObjects[i] = new object(); GC.Collect(0); GC.WaitForPendingFinalizers(); Console.WriteLine("Generation of refToMyCar is: {0}",GC.GetGeneration(refToMyCar)); if (tonsOfObjects[9000] != null) { Console.WriteLine("Generation of tonsOfObjects[9000] is: {0}",GC.GetGeneration(tonsOfObjects[9000])); } else Console.WriteLine("tonsOfObjects[9000] is no longer alive."); } public static void MakeACar() { Car myCar = new Car(); } } [/csharp]

Use FormsAuthentication

image_pdfimage_print
   

using System;
using System.Web.Security;

//MD5Test

class Class1 {

    static void Main(string[] args) {
        string Brian = FormsAuthentication.HashPasswordForStoringInConfigFile("MyPassword", "MD5");
        Console.WriteLine(Brian);
        Brian = FormsAuthentication.HashPasswordForStoringInConfigFile("MyPassword", "sha1");
        Console.WriteLine(Brian);

    }
}

           
          


Demonstrate compound modifiers and calls to external API

image_pdfimage_print


   

/*
C# Programming Tips &amp; Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

//
//  GetUser.cs -- demonstratec compound modifiers and calls to external API
//
//                Compile this program with the following command line:
//                    C:>csc /unsafe GetUser.cs
//
namespace nsUser
{
    using System;
    using System.Runtime.InteropServices;
    
    public class GetUser
    {
        [DllImport ("user32.dll")]
        static extern public int MessageBox(int hWnd, string msg,
                                            string title, int type);
        [DllImport ("advapi32.dll")]
        static unsafe extern public bool GetUserName(byte [] User,
                                                     long *size);
        [DllImport ("advapi32.dll")]
        static unsafe extern public bool GetUserNameW(char [] User,
                                                     long *size);
        static public void Main ()
        {
            byte [] user = new byte[256];
            long size = (long) user.Length;
            unsafe
            {
                if (GetUserName (user, &amp;size) == false)
                    Console.WriteLine ("Error getting user name");
            }
            string strUser = "";
            foreach (byte ch in user)
            {
                if (ch == 0)
                    break;
                strUser += (char) ch;
            }
            MessageBox (0, "The current user is " + strUser, "Howdy", 0);
            /*
            //********************************************
            // The following code uses the wide-character version GetUserNameW()
            //
            strUser = "";
            char [] cUser = new char[256];
            size = cUser.Length;
            unsafe
            {
                if (GetUserNameW (cUser, &amp;size) == false)
                    Console.WriteLine ("Error getting user name");
            }
            foreach (char ch in user)
            {
                if (ch == 0)
                    break;
                strUser += ch;
            }
            MessageBox (0, "The current user is " + strUser, "Howdy", 0);
            */
        }
    }
}