Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 235 Big Data Appliance
- 1.9K Data Science
- 449.9K Databases
- 221.6K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 533 SQLcl
- 4K SQL Developer Data Modeler
- 186.9K SQL & PL/SQL
- 21.3K SQL Developer
- 295.5K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.2K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 154 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 158 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 402 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 230 Portuguese
Java Card 3.0.2 - Garbage Collection

I am using a NXP J3D081 80K I am compiling against 3.0.2. I am trying to simulate a simple file system. The Write routine is simply:
public class File {
public File Next;
public byte[] Name;
public byte Attributes;
public short Length;
public short Ptr;
public byte[] Data;
public byte Write(byte[] InData,short Offset, short InLength) {
if (Length == 0) {
Data = new byte[InLength];
}
else {
byte[] tmp;
tmp = new byte[(short)(Length+InLength)];
Util.arrayCopyNonAtomic(Data,(short)0, tmp, (short) 0, Length);
Data=null;
Data=tmp;
tmp=null;
}
Util.arrayCopyNonAtomic(InData,Offset,Data,Length,InLength);
Length += InLength;
return(1);
}
....
}
I am trying to append data, so I allocate a new buffer the full size of the file, copy the old data in and then copy in the new data. It was my understanding that this card has garbage collection so the old byte array should be collected at some point, but I am returning JCSystem.getAvailableMemory(JCSystem.MEMORY_TYPE_PERSISTENT); and it shows that the memory available goes down by the size of the existing data array, so it is not garbage collected. Even after a power down/up of the card, the memory is not reclaimed.
What am I missing?????
Answers
-
Since the Java Card heap is in persistent memory, GC takes some time and some platform implementations are using very specific trigger to launch gc (e.g. memory almost full, or operation know to create garbage like application uninstall) instead of regularly trying to launch it.
In an application, you can use JCSystem.requestObjectDeletion() to schedule the gc. For performance reasons, you understand that you should use it only when you know you created garbage. The gc will get launched before the next call to Applet.process().
In addition, in order to reduce pressure on GC (and on persistent memory), I would also recommend to manage independently the total file size (size allocated in persistent memory, which could be the data.length ) from actual data size in the file (length field). You could for example extend your file by bigger chunks (64b or 128b each) and just increase the length by the real number of data written.
Also, don't forget that the heap is persistent, so make sure that any field that do not need to survive across sessions (power-down/power-up) is in volatile memory (RAM). I'm referring here to your 'ptr' which looks like to be the current reading position in the file but does not seem to be something that need to be persistent. If you need to have fields in RAM, consider using a specific context object, internally using one of the JCSystem.makeTransient<...>Array() methods.I hope this will help!
-
If requesting garbage collection doesn't work then completely remove card from field. Some implementations require a full power down and some reader implementations may not always remove the field even on a hard reset.