javasmartcardjavacard

Which objects are persistent in Java Card, and when?


This question is twofold, but hopefully shouldn't be unreasonably long to answer, despite its own length. Feel free to answer only part of it, though, and sorry for making it so exhaustive!

For the first part, the question applies to:

For the second part, it applies depending on the answer of the first question. For instance:

Thank you in advance!


I have looked into the JCRE specification v2.2.2 (the version I'm using), but there are points that are unclear in it (hence the exhaustiveness of this question, as I feel this is more appropriate for a Q/A-style forum):

These three questions are respectively equivalent to:


Solution

  • Q: Which objects are persisted? Which objects are transient?

    All object instance fields are persistent when they are created using new. Objects created by the JRE may not be: you'd have to check the Java Card or OS documentation to find out. Note that fields to other objects are references. Those references themselves may point to arrays in transient memory or to other objects that contain volatile state.

    Arrays are transient if they have been created or defined as being transient (usually by calling makeTransientXxxArray()). They are persistent when they are created using new.

    Q: When is an object persisted?

    Upon creation or during any update. Generally the persistent values are simply put in persistent memory: flash or EEPROM. Any write to flash or EEPROM is persisted directly. Note that the Java Card transaction mechanism can still roll back changes to persistent memory.

    This is very different from Java SE where objects instances are stored in RAM, and therefore need to be persisted after creation and each update (if they are persisted at all).

    Q: For the first part, the question applies to: ...

    No, now you're making it unnecessarily complex. The fields of an object are always persisted. How they are referred to doesn't matter.

    The reference itself may not be persistent, even if the object is. It may be in a transient object array or local variable for instance. If all references are lost then the object cannot be accessed anymore and it becomes eligible for garbage collection. But before it is collected it will still remain in persistent memory.

    I assume objects that are directly stored in a transient object array are not persisted. In this case and assuming objects on the stack are persisted, when does the object stored there become not-persisted? Is it persisted at the new Object and un-persisted when being put into the transient object array?

    Consider the previous answer. There is really no such thing as a "transient object array". What exists is an array that consists of references to other objects. These references may be transient or persistent. The objects referenced may be transient (arrays) or contain references to transient values.

    Objects themselves generally do not convert from persistent to transient or vice versa. They don't care how they are referenced. They only care if their reference count doesn't become zero, because then they are lost and eligible for garbage collection. This is of course more likely if they are just referenced by a volatile reference in a transient object array or in a local variable.

    The stack itself is always in transient memory. It includes all local variables (including parameters and return values). But again: it doesn't contain any objects: it may just contain transient references to objects on the heap. If those references are lost then the object may be garbage collected.


    Notes: