out-of-memorysmartcardapdugemalto

outofmemoryexception when reading from smart card


I'm using .Net framework to develop an application that interact with Gemalto smart card (adding and retrieving), I've successively done with the addition part, however when I try to read the data that I stored in the card I got an outOfMemoryException in the host application, can anyone figure out why does this happen?

this is the code in the host application that read from the card:

for (int i = 0; i < 5; i++)
{
     try
     {
          string bookString = service.getBook(i);
     }catch (Exception x) { 
          MessageBox.Show("an error occur");
     }
}

and in app that is loaded on the card, I have this method:

public string getBook(int index) 
{
     return BookList[index].getBookID() + " , " + BookList[index].getBookDesc();
}

Solution

  • The Gemalto .NET Card contains both persistent memory and volatile memory that are used for data storage. The persistent memory acts as persistent storage for the card - data persists in it even after the card is removed from a smart card reader. Volatile memory is reset when the card loses power and cannot be used for persistent storage.

    how you store your data, and how you fill the BookList with data ? please clarify more.

    you have memory limitation of course, so you cannot store up to certain size, in this .net card you have 16KB of volatile memory (RAM) and 70KB of persistent memory (that contain assemblies, storage memory).

    I tested in some Gemalto .net card and able to store 20KB of data in persistent storage memory, after that limit i get the same exception OutOfMemoryException (because the other 50KB is filled with files, assemblies).

    This card is not designed to store database, records and so on, its used to store critical information like keys and passwords. So don't save more than this size and your code will be fine, or use any text compression algorithm (in the client application) to reduce the size before storage in card, but in the end don't try to store more than this ~XX KB.

    update: Because of this limitation you cannot store more than 70K in persistent storage, also you cannot retrieve more than 16KB from the card to client (because this data will be stored in local variable i.e volatile memory and then retrieved back to your client, and you have constrains also here).

    So this is the source of your problem, you retrieve more than volatile memory can hold:

    public string getBook(int index)
    {
      return bookList[index].getId() + " , " + bookList[index].getName();
    }
    

    before return value, this data will be in temporarily variable, and because you can't store more than 16KB you get the exception OutOfMemoryException.

    the solution is to use this storage directly from the client (you have the reference so just use it):

    public Book getTheBook(int index)
    {
       return bookList[index];
    }
    

    and from the client you can access Book functionality(make sure your Book is struct because marshalling is supported only for struct and primitive types in Gemalto .net card):

    Console.WriteLine(service.getTheBook(0).getName());