I am working on a Java Card applet (for Java Card 2.2.1) which requires some temporary objects for processing APDU commands. I have questions about correct memory management. I have spent plenty of time on researching on the issues, but no where I have found any clarification or good sample codes on some java card APIs. I have 2 major questions:
How can I create an array of transient objects (I mean custom objects in RAM not in EEPROM). I have read about
makeTransientObjectArray
but it returns only array of Object type. I know how to create for example, transient byte array usingmakeTransientByteArray
, but my problem is about transient array of object instances. Or may be any way in java language to cast byte array to instances without serialization?I need this transient array of objects only during process of incoming APDU command and not need to keep the memory allocated for me. Where is the best place to allocate this transient memory (inside
install
,select
,process
, ... functions)?
Edited for more explanations:
As I have already read from documentations, any object instance is stored in EEPROM. Assume that I know max number of objects I will need in my process algorithm (say 100). I generate 100 instances of MyClass inside install method. Each instance of MyClass contains 3 fields: field1 a short, field2 a byte, and field3 a short type. All these 100 instances will be filled by input of APDU command. If for each command, I fill objects on EEPROM, its not a good practice, because they are temporary data. Also EEPROM has a max wiriting cycles. An approach may be that for each instance, I allocate 5 bytes for each object using
makeTransientByteArray
andmakeTransientShortArray
. But as I have read from documentations, It allocates memory by clusters (of 32 bytes - not sure for size) which is not effective. So what I have to do in this scenario?I mean what to do for transient memory. If allocate transient memory inside install function, it will not be available for other applets. If the applet is the only one applet on the Card, it is a good practice to allocate all transient memory in
install
function. I want to know a general effective method for all conditions (single applet device or multi-applet device). Also I am not sure, if transient memory allocated insideinstall
, will be available inside process function whenever the card is inserted in the card reader.