The session "Memory-Optimizing Java ME Applications" was taken by Sreekumar Pillai, CTO, Experion Technologies at the 2011 JavaOne conference held in San Francisco, California.
Enterprise mobile applications often include more data-entry-intensive screens and more screen transitions than regular consumer applications. Also the data in a mobile phone is required to be in sync with the data in a back-end server. Due to the limitation in memory capacity for low-end devices, the application design must be highly optimized to avoid any out-of-memory errors during application usage.
This session closely examines the challenges and offers solutions for memory management issues, based on the experience of implementing a field force automation solution for emerging markets.
2. Java ME Platform
§ Java ME devices often are consumer devices which
has resource constraints
§ Mobile devices are not small PCs
§ Applications are to make best use of the available
resources
§ Development requires a different mindset which
expects unexpected things !
3. Why memory optimization?
§ Functionality is often given priority in initial phases
of development
§ Performance tuning takes a backseat and generally
developers ignore efficient usage of memory
§ Memory issues are hard to fix
§ In Java ME this can be fatal and midlet may not run
at all
4. Java ME Enterprise applications
§ Enterprise applications are mostly used by staff to
perform operations while they are on Field
§ Typically such applications require to connect to a
backend server for data access
§ Volume of data can be a bottleneck and requires
careful design and optimization
5. Java ME Memory architecture
§ Automatic memory management is part of JVM
§ Heap
- Heap is created by JVM at startup
- Class instances are allocated in Heap
- Size of Heap is typically fixed to 2Mb (low end devices)
§ Garbage Collection
- JVM Specification mandates a garbage collection mechanism
- Programmer is not allowed to explicitly deallocate the memory of
an object
6. Garbage collection
§ There is no concept of a pointer in Java
§ Memory management is left to JVM
§ GC is provided by JVM to reclaim the unreferenced
memory
§ An object is eligible for garbage collection if it can
not be accessed by any live thread
7. Detecting memory issues
§ Exceptions
- JVM Throws OutOfMemoryError
- When trying to allocate more memory than available as a
single chunk
§ Memory Profiler
- See the objects being allocated and the memory they
occupy
- Set the available amount of memory and stress the
application
11. Object allocations
§ Use as few as possible
§ Dereference objects(set to NULL) to enable GC
§ Reuse objects
§ Heap fragmentation
- See the possibility of doing large allocations first
§ Catch OutOfMemoryError for large allocations
12. Reorganize UI Navigation
§ Having more UI elements in a single screen
increases the memory requirment
§ Good to reduce the data presented in a single
screen
§ Long lists & scrolling does not result in good user
experience
§ Split the data into multiple screens ( e.g. a product
catalogue can be splitted into category, sub
category, and item screens )
13. Data representations
§ XML
- XML is suitable for hierarchical data like a product
catalogue
- XML is lengthy and occupies more bandwidth
§ JSON
- Simple than XML
- More popular among developers
§ CSV / Custom Fixed length
- Full control over data format and parsing
15. Optimizing XML / JSON
§ Reduce size of tags / keys
§ <Product code>PT1432</Product code>
§ Reduce the number of fields required in client side
- Very generalized web services is an overhead for client
applciations
§ Parsers are memory intensive - For large data
strings alternate options to be considered
16. RMS - Alternate storage
§ RMS RecordStore can be used for storing data
when size is high
§ RecordStore can be addressed using a name tag
§ Organize data – Read required information into
memory
17. JAR Size
§ Reduce size of classes
- Use an obfuscator to minimize the size of classes
§ Themes
- Do you require multiple themes ?
§ Images
- Minimize resource sizes
- Use fewer colors as possible