Added a memprofile option.

Also, wrap the h.Close() in a closure, otherwise it closes the first
file descriptors instead of the last, causing a crash.