Experion Presentation at JavaOne 2011: Memory optimizing Java ME applications

18
Memory Optimizing Java ME applications Sreekumar Pillai , CTO, Experion

description

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.

Transcript of Experion Presentation at JavaOne 2011: Memory optimizing Java ME applications

Page 1: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

Memory Optimizing Java ME applications Sreekumar Pillai , CTO, Experion

Page 2: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

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 !

Page 3: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

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

Page 4: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

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

Page 5: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

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

Page 6: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

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

Page 7: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

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

Page 8: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

Memory profiler

Page 9: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

Memory profiler

Page 10: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

How do we optimize ?

Page 11: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

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

Page 12: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

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 )

Page 13: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

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

Page 14: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

XML Vs JSON <widget> <debug>on</debug> <window title="Sample Konfabulator Widget"> <name>main_window</name> <width>500</width> <height>500</height> </window> <image src="Images/Sun.png" name="sun1"> <hOffset>250</hOffset> <vOffset>250</vOffset> <alignment>center</alignment> </image> <text data="Click Here" size="36" style="bold"> <name>text1</name> <hOffset>250</hOffset> <vOffset>100</vOffset> <alignment>center</alignment> <onMouseUp> sun1.opacity = (sun1.opacity / 100) * 90; </onMouseUp> </text> </widget>

{"widget": { "debug": "on", "window": { "title": "Sample Konfabulator Widget", "name": "main_window", "width": 500, "height": 500 }, "image": { "src": "Images/Sun.png", "name": "sun1", "hOffset": 250, "vOffset": 250, "alignment": "center" }, "text": { "data": "Click Here", "size": 36, "style": "bold", "name": "text1", "hOffset": 250, "vOffset": 100, "alignment": "center", "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;" } }}

Page 15: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

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

Page 16: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

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

Page 17: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

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

Page 18: Experion Presentation at JavaOne 2011:  Memory optimizing Java ME applications

Thank you ...