memory and memory issues data storage
DESCRIPTION
Memory and Memory Issues Data Storage. Types of BB Memory. Internal Flash Memory aka application storage, onboard memory internal to the device contains the operating system and BB JVM (about 10MB to 15MB) only place where apps can be run available on all bb devices - PowerPoint PPT PresentationTRANSCRIPT
PROG 38448
Mobile Java Application Development
PROG 38448
Mobile Java Application Development
Memory and Memory Issues
Data Storage
04/20/23 Wendi Jollymore, ACES 2
Types of BB MemoryTypes of BB Memory
Internal Flash Memoryaka application storage, onboard memoryinternal to the devicecontains the operating system and BB JVM (about 10MB to 15MB)only place where apps can be runavailable on all bb devicesstores email messages, organizer data, personal information, etcwhere persistent objects/data is storedolder devices around 32MB or morestorm is 128MB, storm 2 is 256MB, torch is 512MB
04/20/23 Wendi Jollymore, ACES 3
Types of BB MemoryTypes of BB Memory
SRAMhttp://www.webopedia.com/TERM/S/SRAM.html Static RAMWorking memory
E.g. when a persistent object is accessed/edited it is copied to SRAM and worked on there
Controls transient data objectsdata objects that have been created during an app session and not saved anywhere
Controls runtime processesBlackBerry stopped posting amount of SRAM on devices :P
04/20/23 Wendi Jollymore, ACES 4
Types of BB MemoryTypes of BB Memory
External SD (Secure Digital) cardsmicroSD card that can be inserted to extend storage on deviceoptional, removable, contains a FAT file systemsupported on o/s 4.2 or later, except 8700 seriesamount of SD card storage varies
the device can support up to a certain amount (see specs for each device)users might only have a certain sized cardE.g. Storm, Storm2, Torch all support up to 32GB card, but some carriers include 8GB card with device
04/20/23 Wendi Jollymore, ACES 5
Types of BB MemoryTypes of BB Memory
Built-in media storage (device memory)embedded multimedia card (eMMC)not removablealso known as internal memory or onboard device memorynot on all devices, and size varies on those that have itE.g. Storm has 1GB, Storm 2 has 2GB, Torch has 4GB
04/20/23 Wendi Jollymore, ACES 6
Persistence & Object HandlesPersistence & Object Handles
Persistent storage – storage that persists when app is not running and device shut off
Data that isn’t deleted when app stops or device resets/shuts down
The opposite is non-persistent storageData that is still there as long as the device is runningBut is lost when the device shuts off/resets
04/20/23 Wendi Jollymore, ACES 7
Persistence & Object HandlesPersistence & Object Handles
Two kinds of object handles:Object Handles (Transitive Object Handles)
Every object you use requires a “handle” in memoryThink of it sort of like a reference to that object
Persistent Object HandlesFor objects that are persisted across device resetsi.e. stored persistently on the device
04/20/23 Wendi Jollymore, ACES 8
Persistence & Object HandlesPersistence & Object Handles
Example: A vector of 10 strings will consume 11 persistent object handles
1 handle for the vector10 handles for each string in the vector
There is a fixed number of persistent object handles available in the system
Determined by the amount of flash memory. A 32MB device is limited to about 65,000 persistent object handles and 132,000 object handles.
You could run out of persistent object handles well before you run out of memory
04/20/23 Wendi Jollymore, ACES 9
Low Memory ManagerLow Memory Manager
If SRAM runs low, JVM swaps with flash memoryIf flash memory is full, problems!
Garbage collector tries to free up memory no longer in use
When SRAM and flash memory get fullGarbage Collector runs more often and it will take longer
04/20/23 Wendi Jollymore, ACES 10
Low Memory ManagerLow Memory Manager
Handles memory resources on the BB when available
When resources fall below a certain thresh-holdFrees used memory to provide more available memory
All apps work with the low memory manager to free as much memory as possible when device is low on memoryWhen low memory manager will work:
Amount of flash memory falls below a certain threshhold (usually 400KB to 800KB)# of persistent object handles available falls below 1000# of object handles available falls below 1000
04/20/23 Wendi Jollymore, ACES 11
Low Memory ManagerLow Memory Manager
Will remove low priority stuff like cached dataThen will go to medium priority stuff
old emails and out of date calendar entries
If critical, will remove more recent emails, phone logs, etc
04/20/23 Wendi Jollymore, ACES 12
Conserve Memory ResourcesConserve Memory Resources
Flash memoryRefers to the raw persistent storage space that is available on each handheld. Remember that this is fixed
Amount depends on deviceThis is where persistent data and object handles are stored
04/20/23 Wendi Jollymore, ACES 13
Conserve Memory ResourcesConserve Memory Resources
Reduce the number of objects!Fewer objects means fewer persistent object handles and object handlesRecall Vector example with 10 String objects
11 handlesIf you had 3000 records, that’s 33,000 handlesOn some older devices with only 16MB memory, this is too much
A device with 16MB flash memory can only hold 27,000 persistent object handles
04/20/23 Wendi Jollymore, ACES 14
Conserve Memory ResourcesConserve Memory Resources
Data Structure SelectionMake sure data structure contains minimum possible number of objects
especially with vector or hashtable - great objects but resource heavy - avoid using if possible
Use primitive types instead of objects - primitives require no object handle
Note that an array is an object so an array of primitives requires one object handle
Strings are equivalent to byte arrays so a string will take one object handle
04/20/23 Wendi Jollymore, ACES 15
Conserve Memory ResourcesConserve Memory Resources
Object consolidationRecall Vector example with 10 String objects and 3000 records
net.rim.device.api.system.ObjectGroup allows you to group objects so that they take only one handle
string example that takes 11 - can group this into 1)
Problem: grouped objects are read only so if you edit, you must ungroup, change, re-group again – can affect performancealso ungrouping it obviously requires all the object handles again
04/20/23 Wendi Jollymore, ACES 16
Minimize Memory UsageMinimize Memory Usage
Use primitive data types instead of objectsE.g. use int instead of Integer
Do not depend entirely on the garbage collector.Avoid creating many objects quickly.Set object references to null when you are finished using them.Reuse objects as much as possible.Move heavy processing to the server.
Example: filter or sort data before sending it to the BlackBerry smartphone.
04/20/23 Wendi Jollymore, ACES 17
Storage ChoicesStorage Choices
MIDP RMS (record mgmt system)simple non-relational database formatstores data in arrays of bytesnot much support for sharing data between appsapp data is attached to app, so when app is removed from device, so is datause this if you're developing for older devices or developing a MIDletjavax.microedition.rms package
04/20/23 Wendi Jollymore, ACES 18
Storage ChoicesStorage Choices
BlackBerry Persistent Storesimilar to RMSeasier to store wider range of objectscan store instances of custom classesoffers optional compression and securitymost popular
can be written only to the devices internal flash memorypersists across device resets
04/20/23 Wendi Jollymore, ACES 19
Storage ChoicesStorage Choices
runtime store similar to persistent storedoesn't persist across device resetsused for apps to share info
04/20/23 Wendi Jollymore, ACES 20
Storage ChoicesStorage Choices
FileConnection APIAllows access to BlackBerry file system
files and folders
Can access application storage, SD card, device memoryFile system is where media, pics, downloaded files, attachments from emails, etc are storedAccessible by all apps on the deviceGood for large files, pics, documentsGood for files that need to be accessible (e.g. thru desktop manager)
04/20/23 Wendi Jollymore, ACES 21
Storage ChoicesStorage Choices
SQLite (5.0 and later)Full database for structured dataCan store and access data on device's internal memory and external sd cards
04/20/23 Wendi Jollymore, ACES 22
BB Persistent StoreBB Persistent Store
Stores data to flash memoryCan't access SD card
not good for documents or anything large
Flash memory contains the O/S, email, other data
still should be a lot free – check device specs
04/20/23 Wendi Jollymore, ACES 23
BB Persistent StoreBB Persistent Store
net.rim.device.api.system.PersistentStoreManages a list of keys (long) and objects (PersistentObjects)List is global across all apps
You don't know which keys are in use by other appsHowever, key space is large enough that conflicts never occur
Think of key like the ID or file name
04/20/23 Wendi Jollymore, ACES 24
BB Persistent StoreBB Persistent Store
net.rim.device.aip.system.PersistentObjectPersistentStore.getPersistentObject()
returns an instance of PersistentObjecteven if key hasn't been used beforetherefore, you will always get back a PO, but it might have a null valueso either nothing has been saved with that key or it was deleted
04/20/23 Wendi Jollymore, ACES 25
BB Persistent StoreBB Persistent Store
PersistentObject persistentObject = PersistentStore.getPersistentObject(0x2a5c4229e4666089L);Contents of the Persistent Object are accessed via the getContents() method, which might return null:
Object contents = persistentObject.getContents();To set or replace the contents of a PO, use setContents() method:
Hashtable hashtable = new Hashtable();persistentObject.setContents(hashtable);
04/20/23 Wendi Jollymore, ACES 26
BB Persistent StoreBB Persistent Store
Setting contents doesn't necessarily mean object has persisted
must call commit() method: persistObj.commit();
Persistent Object maintains a reference to its contents
to change the data in the persistent object, you must modify the instance and call commit()you don't need to call setContents() again unless you want entirely different object to be associated with that key
04/20/23 Wendi Jollymore, ACES 27
BB Persistent StoreBB Persistent Store
// This will persist MyKey and New Value// no need to call // persistentObject.setContents() againhashtable.put("MyKey", "New Value");persistentObject.commit();
04/20/23 Wendi Jollymore, ACES 28
What Can You Persist?What Can You Persist?
Only objects, not primitives (use wrapper classes)Any object you persist must implement net.rim.device.api.util.Persistable interfaceSome other classes are allowed even though they don't implement Persistable:
wrapper classses, Object, String, Vector, Hashtable
Arrays of primitives and other persistable types
04/20/23 Wendi Jollymore, ACES 29
What Can You Persist?What Can You Persist?
Persistence saves entire object including all objects it referencesObjects your object references must also be persistable
e.g You want to persist a Hashtable of Employee objectsEmployee class must be persistable (implement Persistable interface)
04/20/23 Wendi Jollymore, ACES 30
ExercisesExercises
Do the example in Beginning Blackberry (Persistent Store section)Do the tutorials and lab from the RIM materials
There is also a lab using Runtime store from the RIM materals
All of this is in the notes