Patent ID: 7519639

Claim:
A method of defragmenting a memory heap arranged into a series of pages, the method comprising: dividing the pages of the memory heap into a series of blocks of different size classes; selecting a size of the blocks to limit an amount of internal fragmentation; determining that a number of empty pages of memory has fallen below a threshold number of empty pages of memory; marking objects in the memory heap that were not used during a previous garbage collection cycle as dead objects; marking objects in the memory heap that were used during the previous garbage collection cycle as live objects; sorting pages of the memory heap according to a number of dead objects per page, from a fullest page to an emptiest page; wherein the emptiest page is the page with a highest number of dead objects; and the fullest page is the page with a least number of dead objects; setting an evacuation pointer to the emptiest page; setting an allocation pointer to the fullest page; starting with a largest block size, defragmenting pages of the memory heap containing the dead objects by iteratively moving the live objects from the emptiest pages to the fullest pages and evacuating the dead objects evacuating the dead objects when the emptiest pages become filled with only the dead objects; wherein the defragmenting is performed without stopping execution of an application calling the live and dead objects; wherein moving the live objects comprises: using a read barrier with a forwarding pointer that originally points to the object itself; allocating a new block of memory in a page for a copy of the object; and once the new block is allocated, redirecting the forwarding pointer so that it points to the new block; progressing the allocation pointer to a subsequent page when all blocks on a previous page have been allocated; progressing the evacuation pointer to a previous page when all the live objects have been moved from a current page; after the largest block size objects have been moved, repeating the sorting, setting, defragmenting, and progressing steps for each lower block size until the allocation and evacuation pointers point to the same page, such that there is at most one page having both live and dead objects.