This blog is meant to provide information, thoughts and links that may be found useful to a computer programmer. There is no set strategy or limits of what topics will be discussed.

April 10, 2007

A brief look at Garbage Collection

Garbage Collection

What is it?

Garbage Collection (GC) is a built-in feature of the .NET Common Language Runtime (CLR). The purpose of Garbage Collection is to free managed memory resources. The reason this has come into existence is that in prior windows versions the programmer was responsible for releasing allocated memory resources when appropriate. Resources are most typically used from the creation of objects ( i.e. using the keyword "new" operator). The problem with this was that most developers (especially the hobbyist) would not properly clean applications (and their objects) from the resources; another issue had been that if an application had failed and pre-maturely terminated then the resources would remain in use until the memory was specifically told to clear the resources. The developer of complicated applications that would use a vast amount of resources had to create methods to determine if a failure had occurred and have the memory cleared prior to restarting the memory intensive application.

 

Microsoft's answer was to create the Garbage Collection class. This class would periodically scan the current allocated memory resources and determine if any resources were in use by programs that have already been terminated (rather it be intentional or accidental). Should the Garbage Collection find a used resource with an invalid application reference it would then clear the resource to free up what could be a valuable resource.

How do you use it?

Microsoft recommends that you do not use the Garbage Collector class directly; however, they have left the class open to be used in the few and rare instances it may need to be called. The reason Microsoft recommends against its use is that the Garbage Collector is an automated task performed by Microsoft Windows through the .NET Framework ( 1.1 and higher). The primary reason not to use the Garbage Collector class is that each time the GC is called it will suspend all active threads. This can cause serious degradation in performance should the GC class be called too often.

 

Since this is an automated feature you may ask why you would even bother to learn about this feature. The primary reason is that since you won't control when this feature is initiated you could inadvertently lock your application (or absorb large performance degradation). A prime example would be an application that supports multiple data connections through an SQL adapter. The default SQL adapter uses a pool of 10 connections. It is conceivable if you were to use all 10 connections in your application and not properly release the connections from memory, and then call another connection into memory that you would have to wait until the Garbage Collection had occurred to clear the memory of these connections, even though you have already closed the prior 10 connections before calling the additional connections!

 

So, when is an instance you would want to call the GC class, and how would you call it? One instance would be the above example of utilizing a large pool of SQL connections and releasing them all at one moment. Another time you would want to call it is if you have an application that uses a document that contains a large number of references using unmanaged memory resources and your application closes this document; you then would know that all of the resources should be released since the document no longer exists (suspension of threads referencing the document would not effect your application; however, still use caution in case of other threads are still in use in your application). You would then want to call the GC.Collect method. Again, you will want to ensure this is placed in a block of coding that is limited in calls, preferably called only once. A great example would be to place any method calls to GC.Collect in the form's Finalize class-method; you would "override" the finalize method to include the GC.Collect class and proper calls.

 

What can I do to release memory resources, without calling GC.Collect?

It is best described in the MSDN webpage titled "Garbage Collection". I will use a single method to describe how to release a memory resource; however, keep in mind that there are a few different ways and many of them shall depend on the chosen programming language. Such as C# uses additional features for releasing memory that cannot be used in Visual Basic. You can read more detail at: http://msdn2.microsoft.com/en-us/library/fs2xkftw(VS.80).aspx .

 

You can simply call the Dispose() method directly from your application. An example in Visual Basic would be:

Public Sub Close()

' Calls the Dispose method without parameters.

Dispose()

End Sub

 

An example in C# would be:

public void Close()

{

// Calls the Dispose method without parameters.

Dispose();

}

 

These two methods show a very simplistic method of disposing of your object; however, you will want to review the above mentioned website for the preferred patterns and practices that will ensure proper disposing of the object by implementing the IDisposable class.

 

This should get you started and should help you to understand how to make your programs less susceptible to degradation from memory resources not being properly released.

April 4, 2007

What is .NET?

This blog is merely an attempt to help you understand the .NET Framework. It will not cover in detail how to use the framework or what is contained within the framework. It will provide links to locations that will provide this needed detailed information.

 

To understand the history of .Net we need to understand how Windows operates. To utilize the classes within windows (pre-.Net) the programmer would need to access Application Programming Interfaces (APIs). These would allow the programmer to access specific classes that were designed to interact with the Microsoft Windows Operating System.

 

An example would be that the developer would need to access a CD-ROM from the CD-ROM Drive. The developer would first reference an API wrapper in the application and then provide coding that would access the drive. The beauty of this method, back then, was the developer didn't have to know how to create code that would directly interact with the mechanical and electronics of the drive. All the developer needed to know was how to initiate the API for accessing the drive, what the API would return as a result (success/fail of accessing, information about the drive, information contained on the CD, etc), and then the developer needed to know how to handle these results.

 

Now all of this sounds simple when you look at the overview of what needs to happen; unfortunately, in practice it was not as simple as it is today. The largest problem the developer would face was learning all the arguments needed; the exact location of the desired classes to instantiate, and many other problems. Microsoft had attempted to increase the ease of the development methods by introducing COM, GDI, and many other things. This was Microsoft's beginning to implement OOP concepts (For more information on OOP, see our earlier blog, "What is Object-Oriented Programming (OOP) and why do you need it?").

 

To lessen the burden on the developer, increase the ease of software developing, and to increase the power of the applications developed, .NET Framework was created. .NET Framework had initially started as 1.0.

 

.NET Framework is essentially a large collection of classes that are, or can be used, in conjunction with the Microsoft Windows Operating System. Some of the classes contained in the framework are: Microsoft.VisualBasic.FileIO, Microsoft.WindowsMobile.DirectX.Direct3D, System.Collections, System.Collections.Generics, System.Data, System.Data.SQL, System.Data.Design, System.Security. The complete list is exhaustive; and the previously mentioned framework classes are a small sample of what you could find. You will want to visit the .NET Framework Class Library Reference website to see a complete listing.

 

.NET Framework is a collection of components. It models the method(s) in which an application can operate with the Operating System. The core of the framework is the Common Language Runtime (CLR) model. This model's principal concept is that it uses 'managed' methods to interact with code and data. Managed Coded and Data provide a safer method of application programming. Some other components within the .NET Framework are: Base Class Library (BCL), Metadata/Intermediate Language (MIL), Common Type System. These are just some of the models and is merely an introduction to the .NET Framework. You can visit the .NET Framework Developer Center to find further information on how the framework is placed together, why it is there and especially how to leverage its current technologies.

 

By visiting the .NET Framework Developer Center you will be able to access a huge amount of information; in particular whenever you run across a section that shall cover the "best practices" for a subject, you should immediately bookmark it. These "best practices" sections will help to eliminate bugs, increase productivity, increase reliability, and most importantly increase the security of your application!

 

A thought that should constantly be kept in the back of your mind is that the .NET Framework is very robust; because of the robustness it is always being improved upon and new versions are being developed. .NET 2.0 is currently the framework in active use (at the time of this writing); however, .NET 3.0 has been officially released and will shortly become the main stream framework with the production of applications that will utilize the power and security of the newer technologies found in Windows Vista Operating System.

 

The one thing this blog is attempting to do is to help you understand that the .NET Framework is integral to programming regardless of using any of the .NET programming languages ( i.e. Visual Basic.NET, Visual C#.NET, etc). You don't need to have a thorough knowledge of the subject, but you will eventually need some basic understanding of the concepts and how to use it.

March 11, 2007

Can we get a book that covers something that can't be found in 100 other similar books?

I just got done reading a great book! Believe it or not, this book was called, “An Introduction to Object-Oriented Programming with Visual Basic .Net” by Dan Clark (APress, 2002). What would make this such a great book? You may even be more curious because it’s an introductory topic book.

I think it is great because there is an impressive section in regards to the pre-designing of software. In particular this book covers the proper methods in determining what a client’s needs are and how to go about gathering all the information and putting it in an organized fashion so that a developer (or a team of developers) can follow a blue print of the desired application for the client. I have read dozens upon dozens of books in regards to programming; and I can tell you that more than 75% of the books aimed at beginner and intermediate programmers do not cover the topic of how to plan a software development project; and just about the remaining ones have a very slim section that introduces vague ideas and methods. This is the first book I have ever read that actually went into detail and provided case studies on a method to collect and analyze data to determine what the proper course of actions are to develop the software. This book introduces the concept of the Unified Modeling Language (UML) with decent detail; the subject is a very large subject and Mr. Clark had done a wonderful job with introducing the reader to this subject.

A triple kudos to you Mr. Dan Clark! Please continue producing publications that cover topics you can’t find in other competing books!

Another thing that has really bugged me for the past few years is partly due to Microsoft and partly due to the authors of books. I dislike that Microsoft had changed the methods to programmatically print; I felt they should have left a backwards compatible printing method. Don’t get me wrong here; I understand, and am pleased, that Microsoft had provided a much more powerful printing method. However, trying to convert old applications and even just adjusting and relearning how to print was a whole headache of a task. Which brings me to the authors of these helpful publications; how come no one, and I mean absolutely no one, has produced a publication that goes into details on how to print programmatically and cover the ins and outs of printing? I have managed to find a few books that almost mention printing has even changed, and of these few books none have provided more than a few pages of discussing there was a change and absolutely none (0) have a chapter on how to print! How is this possible?!?!?! If you read the commonly used message boards (such as MSDN, DevX, etc) you will find hundreds of questions on printing and most people are frustrated with the inability to easily print a multiple line text document. In the old print methods there were simple ways to determine the edge of a print line and if the word currently printing was going to get cut off and if so to have the word print on the next print line. Now you have to calculate so many factors, such as the page length, the text size, the font, if any of the text is bold, what the margins are and yet a few additional other things! This all comes into account just to decide if the current word being printed will get cut of in the middle of it or not.

My point on the lack (and I think "lack" is a nice way to put the issue) of books covering printing is that if an author actually takes the time to create a book on this topic they could make a huge profit! There is no competition and programmers need this type of a book. You don’t need to take my word on the need for this publication, just take a quick glimpse on MSDN’s community forums and do a search on the keywords “VB” and “Printing” and view only 25 posts. You will quickly come to a conclusion that there is a void in the printing subject and filling it would be beneficial to the programming community and, especially, to the author and publishing company!

March 9, 2007

VB Beginner Blog at another location

If you are looking for a blog that focuses on the Visual Basic (VB) language and is intended to help the beginning and novice programmers then please visit my 'Visual Basic Helper' blog (http://visualbasichelper.blogspot.com)

Thank you,
James

Introduction to this blog

This blog will focus on discussing all different topics of the programming world.

I primarily program in the VB.Net language; however, I also dabble in a few other languages as well.

My primary focus will to enlighten the beginning programmers with concepts that are broken down to help in learning the vast world of programming. I shall also provide some other points of interest; such as, coding samples, discussions on opinion topics and whatever else I feel about rambling on for that particular blog.

I hope you will find this information useful, and always remember that these are my views and I am only human. I never expect anyone to agree with, or understand, my views; I only ask that the reader accept this as my view on the topic. I will understand, and expect, some readers to disagree with my views.

I welcome any comments on my blogs. However, I do reserve the right to edit, delete and/or use any postings on my blog in any way I choose. In other words, if you leave a comment that I feel is inappropriate or needs to be toned down, or just simply shortened, I will do so!

If you are looking to read a blog that will help you to learn how to program in the Visual Basic language, please read my Visual Basic Helper blog (http://visualbasichelper.blogspot.com).

If you are looking to read a blog that will help you to learn how to program in the C# (pronounced C-Sharp) language, please read my C# Helper blog (http://csharphelper.blogspot.com).

Enjoy my future ramblings!

Thank you,
James