15.01.2013 Views

Foundations of Programming - Karl Seguin

Foundations of Programming - Karl Seguin

Foundations of Programming - Karl Seguin

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7 - Back to Basics: Memory<br />

Until we look at the interaction between the heap and the stack, the only real way to get in trouble with<br />

the stack is with the StackOverflowException. This means that you've used up all the space<br />

available on the stack. 99.9% <strong>of</strong> the time, this indicates an endless recursive call (a function which calls<br />

itself ad infinitum). In theory it could be caused by a very, very poorly designed system, though I've<br />

never seen a non-recursive call use up all the space on the stack.<br />

The Heap<br />

Memory allocation on the heap isn't as straightforward as on the stack. Most heap-based memory<br />

allocation occurs whenever we create a new object. The compiler figures out how much memory we'll<br />

need (which isn't that difficult, even for objects with nested references), carves up an appropriate chunk<br />

<strong>of</strong> memory and returns a pointer to the allocated memory (more on this in moments). The simplest<br />

example is a string, if each character in a string takes up 2 bytes, and we create a new string with the<br />

value <strong>of</strong> "Hello World", then the CLR will need to allocate 22 bytes (11x2) plus whatever overhead is<br />

needed.<br />

Speaking <strong>of</strong> strings, you've no doubt heard that string are immutable - that is, once you've declared a<br />

string and assigned it a value, if you modify that string (by changing its value, or concatenating another<br />

string onto it), then a new string is created. This can actually have negative performance implications,<br />

and so the general recommendation is to use a StringBuilder for any significant string manipulation.<br />

The truth though is that any object stored on the heap is immutable with respect to size allocation, and<br />

any changes to the underlying size will require new allocation. The StringBuilder, along with some<br />

collections, partially get around this by using internal buffers. Once the buffer fills up though, the same<br />

reallocation occurs and some type <strong>of</strong> growth algorithm is used to determined the new size (the simplest<br />

being oldSize * 2). Whenever possible it's a good idea to specify the initial capacity <strong>of</strong> such objects in<br />

order to avoid this type <strong>of</strong> reallocation (the constructor for both the StringBuilder and the<br />

ArrayList (amongst many other collections) allow you to specify an initial capacity).<br />

Garbage collecting the heap is a non-trivial task. Unlike the stack where the last scope can simply be<br />

popped <strong>of</strong>f, objects in the heap aren't local to a given scope. Instead, most are deeply nested references<br />

<strong>of</strong> other referenced objects. In languages such as C, whenever a programmer causes memory to be<br />

allocated on the heap, he or she must also make sure to remove it from the heap when he's finished<br />

with it. In managed languages, the runtime takes care <strong>of</strong> cleaning up resources (.NET uses a<br />

Generational Garbage Collector which is briefly described on Wikipedia).<br />

There are a lot <strong>of</strong> nasty issues that can sting developers while working with the heap. Memory leaks<br />

aren't only possible but very common, memory fragmentation can cause all types <strong>of</strong> havoc, and various<br />

performance issues can arise due to strange allocation behavior or interaction with unmanaged code<br />

(which.NET does a lot under the covers).<br />

<strong>Foundations</strong> <strong>of</strong> <strong>Programming</strong> Copyright © <strong>Karl</strong> <strong>Seguin</strong> www.codebetter.com<br />

54

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!