Adjustment to memories allocation for directories.

Iteratively allocate and de-allocate the memories for directories.