Lab 1: Classes and Garbage Collection

Lab 1: Classes and Garbage Collection

In this lab, you will practice creating classes and members, and you will create a demonstration of how garbage collection automatically manages memory. You will create a class that interacts with a pre-made user interface. This class will have a shared variable that keeps track of the number of instances that currently exist in memory. Additionally, you will add code to the constructor and destructor of this class to increment and decrement this variable. You will then create multiple instances of this class and watch as their memory is reclaimed by garbage collection. The solution to this lab is available in the \Labs\Ch01\Solution folder on the Supplemental Course Materials CD-ROM.

NOTE

For this and all of the labs in this book, you will find Visual Basic .NET and Visual C# solutions in their respective folders in \Labs\Chxx\Solutions, where xx stands for the appropriate chapter number.

Before You Begin

There are no prerequisites to complete this lab.

Estimated lesson time: 20 minutes

Exercise 1.1: Making the Demo Class

In this exercise, you will create the Demo class that interacts with the DemoTest project. The DemoTest project is available in the \Labs\Ch01\Partial folder on the Supplemental Course Materials CD-ROM.

To make the Demo class

  1. Open the DemoTest.sln solution in \Labs\Ch01\Partial. This solution contains all of the front-end code you will need for your project.

  2. From the Project menu, choose Add Class.

  3. In the Add New Item dialog box, name your class Demo.

  4. Add a public, shared field named Instances to Demo. This field will track the number of instances of Demo that are currently in memory. The following line shows an example:

    Visual Basic .NET

    Public Shared Instances As Long

    Visual C#

    public static long Instances;

  5. Create a constructor for this class (Visual Basic .NET), or add to the default constructor created by Visual Studio (Visual C#). In the constructor, you will add code to increment the Instances variable. The following code shows an example:

    Visual Basic .NET

    Public Sub New() Instances += 1 End Sub

    Visual C#

    public Demo() { Instances++; }

  6. Create a destructor (finalizer) for this class. In the destructor, add code to decrement the Instances variable. For example:

    Visual Basic .NET

    Protected Overrides Sub Finalize() Instances -= 1 End Sub

    Visual C#

    ~Demo() { Instances--; }

    NOTE
    In Visual Basic .NET, you must use the Overrides keyword in the finalizer. The meaning and use of the Overrides keyword is discussed in Chapter 4.

  7. From the File menu, choose Save All to save your work.

Exercise 1.2: Demonstrating Garbage Collection

The front end provided in the DemoTest project contains a form that displays two controls: a button and a label. Additionally, there is an invisible timer component that updates the label control every second. You will run the application and observe how instances of your class are created and garbage collected.

To create the garbage collection demo

  1. In the Designer, examine Form1. You can open the designer by double-clicking Form1 in Solution Explorer. Note that it has a Button control, a Label control, and a Timer component in the component tray.

    NOTE
    Controls are examined further in Chapter 2.

  2. Double-click the Button to open the code window to the click event handler.

  3. Find Private Sub Button1_Click (Visual Basic .NET) or private void button1_Click (Visual C#). Add the following code:

    Visual Basic .NET

    Dim Counter As Integer Dim aDemo For Counter = 1 to 1000 aDemo = New Demo() Next

    Visual C#

    int Counter; Demo aDemo; for (Counter = 0; Counter < 1000; Counter++) { aDemo = new Demo(); }

    This code declares two variables, a Counter and a variable of the Demo class. It then enters an iteration loop. One thousand loops are iterated, and in each loop, the aDemo variable is assigned to a new instance of the Demo class. Recall that creating a new instance of Demo will cause the class s constructor to execute, incrementing the shared variable Instances. As the loop ends and restarts, the aDemo variable is assigned to another new instance of Demo, and all the references to the previous instance of the Demo class are released, thus marking the class for garbage collection. This loop will execute 1000 times for every click of the button.

  4. Press F5 to build and run your application. You should see a button and a label indicating how many instances of Demo exist in memory. Click this button once.

    The label now reads There are 1000 instances of Demo in memory . Wait for a while. After a measurable interval, perhaps even as long as a couple minutes, the label will indicate zero instances again, indicating that the 1000 instances of Demo have been garbage collected and their destructors executed, decrementing the Instances variable.

    The label did not revert instantly because garbage collection is a relatively low-priority thread under normal circumstances. However, when memory gets scarce, the priority of the thread is increased.

  5. Click the button several times in succession. See how many instances you can put into memory. If your machine has a large amount of memory, you might be able to create tens of thousands of instances before garbage collection is performed. Once memory gets scarce, though, garbage collection rapidly and efficiently reclaims the memory used by these unreferenced objects.



MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[.  .. ]0-316
MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[. .. ]0-316
ISBN: 735619263
EAN: N/A
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net