Feedback/Questions

If you have any topics you would like me to cover or just general feedback on blogs I have written feel free to email me at helloriker@hotmail.com.
Showing posts with label Computer Programing. Show all posts
Showing posts with label Computer Programing. Show all posts

Thursday, September 17, 2009

C#: OutOfMemory and Multithreading

So you just wrote this awesome application and made it multi-threaded for performance reasons. However, now when you run it after a while you get an out of memory exception. You go and check your system ram and the application isn't even using up half of it yet. What gives?

The problem resides with the heap that garbage collection uses and the way that garbage collection works. I won't go into details here about that however I will offer some possible solutions. There are 2 types of garbage collection, workstation and server. Workstation gives you one process and one heap to manage for the entire application regardless of the number of threads. Server will setup a new Garbage Collection thread and heap for each thread launched. Thus this should allow you to avoid that pesky out of memory issue.

Be carefull however, if you are launching hundreds of threads or maybe even more it may be possible for you to run out of ram. To avoid this do a poll of the current state of your ram before launching a new thread as well as try setting a max number of threads you will launch based off of the ammount of ram on the machine.

Please check out more articles at http://techtify.blogspot.com

Saturday, September 12, 2009

Recursion

Ok so we have all seen the shirts. But what do they really mean? What is this concept of recursion and how can I use it?

Recursion is simply any method, function, or subroutine that makes a call back to itself. Basically it would look something like this...

Void Main()
{
Main();

}

As you can see this would end up in an infinite loop which is not something we typically would want to have happen. In order to better grasp recursion let us look at the simple task of copying a directory structure.

In a directory structure there are only 2 things, files and other directories. When we copy a directory we know 2 things the location we intend to copy and the destination we want to copy to. Our header would thus look something like.

Void CopyDirectoryStructure(string RootDir, string TargetDir)

Now if we simply looped through all the directories and files in the RootDir and copied them to the target dir we would not copy any additional files in the subdirectories. Thus this is where recursion comes into play. Instead of writing some complicated decision structure to walk through all the sub dirs and determine what needs to happen we can instead simply call ourselves.

Void CopyDirectoryStructure(string RootDir, string TargetDir)
{

foreach(string file in RootDir.GetFiles())
{

//Copy File From RootDir to TargetDir

}
foreach(string dir in RootDir.GetDirs())
{

//Create new dir in target dir
//CopyDirectoryStructure(RootDir + Dir, TargetDir+Dir)


}

}

That is the basics of a recursive function. Also please note I am using psuedo code there. This will not compile and work! This method will terminate after all directories have been walked through and copied.