In order to thank you for all members' support of our website, we are holding new events - Getting Free Samples by Paying Freight only.
Want to get free samples? Please check this post for more details.

Doubt about JavaCard Memory

JavaCard Applet Development Related Questions and Answers.
afrascoi
Posts: 18
Joined: Mon Jan 25, 2016 1:20 am
Points :265
Contact:

Doubt about JavaCard Memory

Post by afrascoi » Thu Oct 12, 2017 10:27 pm

I made a simple applet to test the speed of adding arrays, depending whether they are saved in EEPROM or RAM. Since working with variables stored in RAM should be faster, I was really surprised when I noticed no difference between them. I would really like to know where I went wrong, but simply cannot figure it out. Could anyone give me some clues on this question?

Code: Select all

package test;

import javacard.framework.APDU;
import javacard.framework.ISO7816;
import javacard.framework.Applet;
import javacard.framework.ISOException;
import javacard.framework.JCSystem;

public class SimpleMemoryTest extends Applet {
     private static final short ARRAY_SIZE = 32;

     byte[] eeprom_a,  eeprom_b, eeprom_c;
     byte[] ram_a, ram_b, ram_c;
     short[] mem;

     private SimpleMemoryTest() {
          this.mem = new short[2];
     }
     
     public static void install(byte[] bArray, short bOffset, byte bLength) {
          new SimpleMemoryTest().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
     }

     public void process(APDU apdu) {
          if (selectingApplet()) return;

          byte[] buf = apdu.getBuffer();
          switch (buf[ISO7816.OFFSET_INS]) {
          case (byte) 0x00: { // Print memory info to verify where arrays are stored
               mem[0] = JCSystem.getAvailableMemory(JCSystem.MEMORY_TYPE_PERSISTENT);
               mem[1] = JCSystem.getAvailableMemory(JCSystem.MEMORY_TYPE_TRANSIENT_DESELECT);

               buf[0] = (byte)(mem[0] / 256);
               buf[1] = (byte)(mem[0] % 256);
               buf[2] = (byte)(mem[1] / 256);
               buf[3] = (byte)(mem[1] % 256);
     
               apdu.setOutgoingAndSend((short)0, (short)4);
               break;
          }
          case (byte) 0x01: { // Put arrays in EEPROM.
               this.eeprom_a = new byte[ARRAY_SIZE];
               this.eeprom_b = new byte[ARRAY_SIZE];
               this.eeprom_c = new byte[ARRAY_SIZE];
               break;
          }
          case (byte) 0x02: { // Put arrays in RAM.
               this.ram_a = JCSystem.makeTransientByteArray(ARRAY_SIZE, JCSystem.CLEAR_ON_DESELECT);
               this.ram_b = JCSystem.makeTransientByteArray(ARRAY_SIZE, JCSystem.CLEAR_ON_DESELECT);
               this.ram_c = JCSystem.makeTransientByteArray(ARRAY_SIZE, JCSystem.CLEAR_ON_DESELECT);
               break;
          }
          case (byte) 0x03: { // Add arrays in EEPROM 100 times
               short i, j;

               for (i = (short) 0; i < (short) 100; i++) {
                    for (j = (short) 0; j < (short) ARRAY_SIZE; j++) {
                         eeprom_c[j] = (byte)(eeprom_a[j] + eeprom_b[j]);
                    }
               }
               break;
          }
          case (byte) 0x04: { // Add arrays in RAM 100 times
               short i, j;

               for (i = (short) 0; i < (short) 100; i++) {
                    for (j = (short) 0; j < (short) ARRAY_SIZE; j++) {
                         ram_c[j] = (byte)(ram_a[j] + ram_b[j]);
                    }
               }
               break;
          }
          default:
               ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
          }
     }
}


choimillen
Posts: 29
Joined: Wed Aug 19, 2015 2:55 am
Points :185
Contact:

Re: Doubt about JavaCard Memory

Post by choimillen » Fri Oct 13, 2017 3:53 am

When you create a byte array, all values are initialized to 0. Looking at your loops, it seems you're adding 0's only. Smart card operating systems normally do a read before write, to make sure not the same value is written. It saves time, and in case of EEPROM also to prolong the lifetime. I think this is the reason why you see no difference .. because there is no writing. Do something like

Code: Select all

eeprom_c[j] = (byte)(i + j);


Code: Select all

case (byte) 0x03: { // Add arrays in EEPROM 100 times
               short i, j;

               for (i = (short) 0; i < (short) 100; i++) {
                    for (j = (short) 0; j < (short) ARRAY_SIZE; j++) {
                         eeprom_c[j] = (byte)(eeprom_a[j] + eeprom_b[j]);
                    }
               }
               break;
          }
          case (byte) 0x04: { // Add arrays in RAM 100 times
               short i, j;

               for (i = (short) 0; i < (short) 100; i++) {
                    for (j = (short) 0; j < (short) ARRAY_SIZE; j++) {
                         ram_c[j] = (byte)(ram_a[j] + ram_b[j]);
                    }
               }
               break;
}

tomwu2016
Posts: 1
Joined: Fri Jun 03, 2016 1:49 am
Points :16
Contact:

Re: Doubt about JavaCard Memory

Post by tomwu2016 » Fri Oct 13, 2017 4:09 am

Are you execute soft simulation in IDE? if so ,there is no diffence.

User avatar
mabel
Posts: 228
Joined: Mon May 18, 2015 3:09 am
Points :1623
Contact:

Re: Doubt about JavaCard Memory

Post by mabel » Fri Oct 13, 2017 5:49 am

The size of your test array is rather small, so considering all the overhead of executing other bytecodes, the difference is probably going to be difficult to notice.

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 3 guests

JavaCard OS : Disclaimer