in reply to memory usage woes

The program should reuse memory allocated for lexical variables, but it will grow to the maximum size needed at any one time. That means that if one of the times you call this sub it uses 300MB, that 300MB will stay allocated to this perl process, but can be reused by the same lexicals if you execute the sub again.

If it seems to be adding each time, rather than reusing previously allocated memory, you may have a scoping bug in your code. It's possible that you missed a place where you are keeping a reference to some large data structure and thus preventing it from being garbage collected.