Sun Microsystems 2 Wireless Office Headset User Manual


 
Chapter 3 Managing Native Resources 23
Many MIDlets are not designed to deal with failure in the midst of an operation. If
such a MIDlet is updating one of its date structures when an allocation failure
occurs, the date structure might end up in an inconsistent or corrupted state.
Providing MIDlets with a resource reservation will prevent failures from occurring
at arbitrary times, thus reducing the possibility of data structure corruption.
Limit
The limit mechanism allows a MIDlet to allocate up to a certain amount of some
resource, but no more. If the MIDlet attempts to allocate more resources than its
limit, the attempt fails, even if resources are actually available on the system.
Conversely, fewer actual resources might be available than a MIDlet’s limit might
indicate. A MIDlet might attempt to allocate resources beneath its limit, but the
attempt still fails if insufficient actual resources are available on the system.
For example, assume that 600 kilobytes total heap is available and that MIDlet A is
using 400 kilobytes. MIDlet B has a limit of 300 kilobytes of heap. It receives an out-
of-memory error after allocating 200 kilobytes, because that’s all the heap available,
even though it is under its limit. Suppose, however, that MIDlet B has a limit of 150
kilobytes. It receives an error after allocating 150 kilobytes, even though 50 kilobytes
of heap is still available.
The limit mechanism is useful to prevent MIDlets from attempting to use all
available resources and thereby disrupting the operation of the system.
Reservations and limits work in concert to control competition among MIDlets that
are using a pool of resources, such as heap memory or network sockets. It is typical
for a running MIDlet to have both a reservation and a limit in effect at the same
time. Note that it only makes sense for the limit to be greater than or equal to the
reservation. A limit differs from a reservation in that a reservation guarantees that
the reserved amount of the resource is always available, but it doesn’t prevent the
MIDlet from attempting to exceed the reservation amount. A limit is quite
complementary: It prevents the MIDlet from using more than a some amount of a
resource, but it doesn’t guarantee that the amount is available.
When resources are explicitly released by a MIDlet, they might or might not become
available to other MIDlets. It depends on the reservation policy for the resources.