Posts

Showing posts from November, 2014

Reducing memory consumption (Part 2)

As we have seen in earlier post first method to reduce memory was reducing font loading. This is described here . Next method is to use weak references. 2. Making use of weak references Our application uses observer model to notify data availability. Here a EventNotifier class is used to register listeners for the event. As soon as the event occurs, it fires the event with EventId and EventObject. Some activities use data that is fetched from server. In order to get this event activity registers itself with the EventNotifier. There is a communicator thread, which downloads data, and calls method public int eventNotify(int eventType, Object eventObject) of EventNotifier. Now this method finds out registered listeners and informs them about update. Problem comes when Android decides to kill activity for reusing resources. In this case OnDestroy() method may not get called. OnDestroy() handles unregistration activity from EventNotifier. Now as the EventNotifier still holds re...

Reducing memory consumption (Part 1)

Recently our app faced lot of issues because of huge memory consumption. We resolved these issues using following techniques: 1. Reducing font instances 2. Making use of weak references 3. Making handlers static and final In this post we will see first approach. Second approach will be covered in next post. 1. Reducing font instances We used to have a custom text view. This text view uses a font provided by application. Code below shows how it used to load. public boolean setCustomFont(Context ctx, String asset) { Typeface tf = null; try { tf = Typeface.createFromAsset(ctx.getAssets(), "fonts/" + asset.trim()); } catch (Exception e) { Log.e(TAG, "Could not get typeface: " + e.getMessage() + "\n value of asset string " + asset); return false; } setTypeface(tf); return true; } After using memory profiling tool of android, we came to know that each time this activity is opened memory usage increases...