Posts

Showing posts from May, 2010

Singleton vs. Static Class

I'm often asked “Why create a Singleton when a Static Class does the same thing?”.  Well, while using a Singleton vs a Static Class may seem to be the “same thing”, they are quite different.  There are pros and cons to both, and each has it's place in an application.  To understand the difference, let's first take a look at memory allocation.  Then we’ll look at memory implications, state and synchronization, and some caveats as to the use and behavior of the two. Stack and Heap There are two major types of memory allocation within the .Net CLR and Java VM--the stack and the heap.  The stack is just that—an organized stack of frames.  Think of it as a stack of blocks and you can only access the block at the top of the stack—the active frame .  Each thread gets it's own stack.  The heap is a disorganized “pile” of “things” that can be quickly accessed from anywhere in the pile.  Under the hood, .Net and Java handle these memory areas differently, but they are similar e