Maxmem

implement max_memory handling as proposed in #14