Although any applet on the card can request it, only the Java Card Runtime Environment (Java Card RE) can start the object deletion mechanism. The applet requests the object deletion mechanism with a call to the JCSystem.requestObjectDeletion()
method.
In the following code example, the method updates the buffer capacity to the given value. If it is not empty, the method creates a new buffer and removes the old one by requesting the object deletion mechanism.
/** * The following method updates the buffer size by removing * the old buffer object from the memory by requesting * object deletion and creates a new one with the * required size. */ void updateBuffer(byte requiredSize){ try{ if(buffer != null && buffer.length == requiredSize){ //we already have a buffer of required size return; } JCSystem.beginTransaction(); byte[] oldBuffer = buffer; buffer = new byte[requiredSize]; if (oldBuffer != null) JCSystem.requestObjectDeletion(); JCSystem.commitTransaction(); }catch(Exception e){ JCSystem.abortTransaction(); } }