![]() |
Start of Tutorial > Start of Trail |
Other than on the PC with its constantly enlarging sizes of RAM, Memory is precious on small devices
like the RCX.
Since there is no mechanism on Java to free memory at a determined point of time, and particularly
with the lack of a garbage collector with leJOS, allocating objects on the heap is liable to set forth
some kind of "out of memory" behavior of your robot. In peculiar, memory consuming collections like arrays
are prone to make itself a nuisance.
To help this, some new josx.util package located classes have been introduced in
leJOS' version 2.1.0 with the task of providing a recycling mechanism:
Recyclable an interface for an recyclable objectAbstractRecycable an abstract base class for Recyclables which implements
some of Recyclable methodsRecyclableArray a recyclable array implementing the Recyclable interfaceRecycler an abstract base class which is able to recycle RecyclablesArrayRecycler an array recyclernew(), you order such a new instance by a Recycler, which may be thought as a
factory that stores currently unused objects and delivers them on order.
Recyclable interface for the classes he wants to be recycled that way,Recycler.allocate(),
Recycler.recycle([recyclable object])) andRecycler class in overwriting its createInstance()
method (don't worry: as a rule that requires no more than creating a new instance of your recyclable class using
new).josx.util.AbstractRecyclable class; this means, it has to implement the following two
methods:
public void init() called, when an instance is allocated by the recyclerpublic void release() called by the user when the instance is no longer needed; you should
release any resources here, including nested Recyclables.Recyclable directly; in this case you
have to implement also the getNextRecyclable() and setNextRecyclable() by
yourself.
josx.util.Recycler class, which means that your recycler has to to implement the
public Recyclable createInstance()
method and adapt it to create an instance of your particular recyclable class.
Of course you can build your own recycler, which means that you have to take care of the complete recycling process by yourself.
josx.util.RecyclableArray class already,
which delivers all the basic functionalities you would expect from an array like get(), put() and
getLength().
josx.util.ArrayRecycler which
extends josx.util.Recycler and implements its abstract methods
- ready to be used.
examples/test/recycling
section of the leJOS tree.
![]() |
Start of Tutorial > Start of Trail |