Tuesday, November 25, 2008

Oceans Ten Times More Acidic Than Thought

National Geographic has a curious article entitled, Oceans Ten Times More Acidic Than Thought" that contains so many errors it's difficult to know where to begin. For starters, the title implies that the ocean is ten times more acidic that previously thought, like it slipped from 6.5 to 5.5pH, catching researchers off guard. In reality, it's the increase in acidity that is ten times higher than some climate model predicted. Perhaps a better title would have been, "Global Warming Model Incorrect By an Order of Magnitude".

The word "Oceans" in the title implies many of our oceans have increased acidity, but the article cleanly states the research applies to the ocean around one island (or perhaps "Oceans" in the title needs a possessive apostrophe).

A whale of a contradiction in the article is "...carbon dioxide dissolves in the oceans it forms carbonic acid, which in turn has a negative impact on marine life.", but later the article claims, "Larger mussels and barnacles suffered, leaving smaller barnacles and some calcium-based seaweeds better off."
Increased acidity doesn't have a negative impact on all marine life. Under almost any change in environment, some species will do better and other will flounder.

The article ends with a stinging quote: "This is typical of so many climate studies—almost without exception things are turning out to be worse than we originally thought." This clearly is a call to the readers to do something about the tsunami of impending climate change. Although it could be argued from the article that there's something fishy with these climate models which can't make accurate predictions.

The whole article should be taken with a grain of salt.

Friday, November 21, 2008

C# OutOfMemoryException and StringBuilder

We had an interesting OutOfMemoryException this week. When creating really long strings and adding them to a StringBuilder, we get the OutOfMemoryException way before we really run out of memory. Come to find out, StringBuilder is searching for contiguous memory and may not find that long before you run out of real memory.

To help find your upper limit of memory use, .Net provides the friendly MemoryFailPoint as shown below:

using System;
using System.Runtime;
using System.Text;
namespace PlayingAround {
class Memory {
private static void Main() {
MemoryFailPoint memFailPoint;
StringBuilder sb = new StringBuilder("1234567890");

for (int i = 0; i < 100; i++) {
int aboutToGetMemoryInBytes = sb.Length*5;
Console.Out.WriteLine("aboutToGetMemoryInBytes = " + aboutToGetMemoryInBytes.ToString("#,##0"));
int aboutToGetMemoryInMegaBytes = (int)(1 + (aboutToGetMemoryInBytes >> 20)); //round up to the next megabyte
Console.Out.WriteLine("aboutToGetMemoryInMegaBytes = " + aboutToGetMemoryInMegaBytes.ToString("#,##0"));
using (memFailPoint = new MemoryFailPoint(aboutToGetMemoryInMegaBytes)) {
Console.Out.WriteLine("GC.GetTotalMemory(true) = " + GC.GetTotalMemory(true).ToString("#,##0"));
Console.Out.WriteLine("sb.Length = " + sb.Length.ToString("#,##0"));
catch (InsufficientMemoryException) {
var maxMem = GC.GetTotalMemory(true);
Console.Out.WriteLine("maxMem = " + maxMem);
} catch (OutOfMemoryException) {
Console.Out.Write("press return to exit.");

Wednesday, November 12, 2008

DataTable.Compute considered harmful

Our team has been investigating a particularly obscure bug in our .Net 3.5 application. During load and performance testing we crashed the application on our Windows Server 2003 test farm. IE7 would not render the page, Firefox crashed the application pool. We got three basic errors:
1. application pool had been abandoned.
2. "aspnet_wp.exe (PID: 2536) stopped unexpectedly."
3. a stack overflow error.
Oddly the application ran fine on our Windows XP boxes used for development. After much searching and testing the problem was with
DataTable.Compute(string expression, string filter) 

We had a large filter string which exhausted the memory on the machine since the filter was used in a recursive manner. It did not help that the stack size in IIS for Server 2003 was reduced to 256K from 1MB in 2000. (In retrospect this was a good thing since it exposed an error earlier in testing and not later in production).
We will rewrite the code calling Compute() and do it iteratively in our C# code. Not all uses of Compute() are bad, but be careful about using large filters.