4 Replies Latest reply: Apr 4, 2013 12:10 PM by Sebastien_Lorquet RSS

    Is possible to free transient arrays in RAM?

    PavolAlcohol
      Hi all,

      In my applet's methods I've allocate several transient arrays by following routine:
                
      inBuffer = JCSystem.makeTransientByteArray(size,JCSystem.CLEAR_ON_DESELECT);

      where inBuffer is global applet's variable of byte array type.

      The result of my work is that i haven't any next memory(Transient Heap) and cannot continue with applet execution.

      In the end of my process method I allways call following:
      inBuffer = null;
      if (JCSystem.isObjectDeletionSupported()) {
           JCSystem.requestObjectDeletion();
      }

      When I read java docu it should ensure deletetion of transient objects before the next card reset session. When I reset card via command */atr* nothing is freed.

      I use JCOP 2.4.1r3, JC2.2.2, GP2.1.1 and test it on simulation.

      Can somebody help me?
        • 1. Re: Is possible to free transient arrays in RAM?
          925183
          you have to keep the reference to the transient byte array and just reuse it in later parts of your code
          every function has offset and length parameters so you can easily use the same array for multiple purposes lateron
          • 2. Re: Is possible to free transient arrays in RAM?
            PavolAlcohol
            Thanks, you're right, it's a standard way how to use transient arrays initialized during applet installation.

            but i found reason in my case: one my object kept still a reference on my array. Now, it works but I have to remake it for array reusing.

            thanks
            • 3. Re: Is possible to free transient arrays in RAM?
              925183
              using JCSystem.requestObjectDeletion() is not the right way to work with java card.
              as for as i know it is really slow
              you should create all your objects used during lifetime in the installtion process and reuse transient arrays by using Util.arrayFill / arrayCopyNonAtomic / functions with offset+length
              • 4. Re: Is possible to free transient arrays in RAM?
                Sebastien_Lorquet
                Yes, there is no guarantee that object deletion is supported. And even if the platform says it is, requestObjectDeletion() can be a nop that just marks an object for deletion at instance removal, or worse, at package removal for static allocations.

                And even if objects were really deleted, deleting too much objects will wear the eeprom/flash memory. Transient buffers references are indeed held in non volatile memory, only the target buffer contents is volatile.

                As said before, common JC coding practices forbid code that may result in an object being allocated more than once. Either allocate memory in the constructor, or make sure that objects will never be allocated twice by wrapping these allocation in a test for nullity:
                class thing {
                  private byte[] array;
                
                  private method() { //may be called more than once
                    if(array==null) {
                      //okay, allocate
                      array = JCSystem.makeTransientByteArray(size,type);
                    } else {
                      //this may be a severe error
                    }
                  }
                
                }