Do periodic garbage collection to avoid running out of memory