The present invention is directed at a virtual machine environment
operating on portable devices with limited resources. The virtual machine
environment includes a method for managing dynamically generated code and
data together in order to shift memory usage to/from generated code or
data as needed. Each application domain manages several code pools and
several garbage collection pools that are allocated from a system memory
heap. When additional memory is not available for allocation from the
system memory heap, garbage collection is performed until sufficient
memory becomes available. During garbage collection, unreachable data in
each garbage collection pool is cleared. The garbage collection process
may further compact the garbage collection pools in order to empty one or
more of the garbage collection pools. Still further, code pools may be
released back to the system memory heap if the generated code is not
associated with a current application domain, is executed infrequently,
or is not associated with an instruction on a stack. Thus, memory may
flow from data to code or vice versa as needed and among application
domains.