Android background image high memory usage issue

Hi everyone,

In my previous article, I have mentioned that I am doing memory analysis for one of our apps. While investigating memory usage on Motorola Nexus 6 phone, I realized something unusual.

Android Studio's Memory Monitor - 68 MB! Wooww

Android Studio’s Memory Monitor – 68 MB

Android Studio’s Memory Monitor was telling me that our app was using 68 MB memory. The app doesn’t have any large images except the one in the background. I decided to investigate the reason behind it and I finally fixed the issue. In this article, will explain how I tracked down and fixed the issue.

To start with, I used Allocation Tracker of Android Studio’s Memory Monitor. I expanded Thread 1 saw that onCreate() was making an allocation of 49,035,328 bytes (~47 MB). I expanded the call until I find the cause. Looking at the calls, it is obvious that an image is causing this issue.

Result of Allocation Tracking

Result of Allocation Tracking


I put break points to onCreate() and stepped into the calls until I reach class. In this class, background image is assigned in this block of code:

if(mBackgroundResource !=0){
    background = getContext().getDrawable(mBackgroundResource);}
    background = mBackgroundDrawable;}

The background Bitmap had a resolution of 2626×4669. However, the background image I used has a resolution of 750×1334

I had two concerns about the size of Bitmap and scaling and posted on Stackoverflow. Android master CommonsWare responded with a clean answer, I will just summarize his answer here.

1) The reason Bitmap is scaled to 2625×4669, even if the phone had a resolution of 1440×2560, is that somehow I put my background image to drawable folder. The drawable folder is same as drawable-mdpi and mdpi stands for 160 DP according to Android.

Motorola Nexus 6 has a density of 560 DP. To match that, Android resized the image multiplying by 560/160 = 3.5

750 x 3.5 = 2625 and
1334 X 3.5 = 4669

2) The reason it was 47 MB is that, even if the image is 160 KB on the disk, Bitmaps are decompressed version of this files and every pixel in ARGB_888 takes 4 Bytes of memory.

2625x4669x4 = 49,024,500 bytes ~ 47 MB

The Solution

Before explaining the solution always remember to test your application on different devices. That was the main reason I missed that point. The device I tested my app during development cycle had 245 DP and it was hard to notice memory leak (It was using only 15 MB memory). Android documentation has a section for Supporting Different Screen Sizes, but it doesn’t mention that upper-scaling of images can cause high memory usage. It would be nice to see that in the documentation.

The solution to that is putting a single image to drawable-nodpi folder or creating images at different resolutions for different densities. To understand the difference between these options you can check CommonsWare‘s article.

To create images for different densities remember to scale your images according to 3:4:6:8:12:16 ratio.

For reference,Supporting Different Screen Sizes

After fixing this issue, the memory usage on Motorola Nexus 6 dropped to 29 MB.

Memory Usage After

Memory Usage After

Android Memory Leak in createFromAsset() Method

Hi guys,

We are about to ship an Android application at my company, and I decided to do some memory analysis before we ship it, for a better performance. I was trying to investigate memory leaks, and one detail grabbed my attention.

You can print your app’s memory allocation typing this command in Terminal:

adb shell dumpsys meminfo <package_name|pid> [-d]

So I put this command and it printed that:

Output of memory analysis

Output of memory analysis 

Nothing seems extraordinary here. When I scroll down I saw that:

Output of memory analysis - Asset Allocations

Output of memory analysis – Asset Allocations


Investigating Your RAM Usage page of android gives an example output from Maps application, but they don’t have Asset Allocations section in their example.

In the first output, I counted number of asset allocations and it showed 30 allocations. I continued to use application by closing/opening new activities/fragments and the number was growing. In the example it is 93, but after the screenshot I reached 165 allocations.

The Problem

The problem is that every time I call createFromAsset() it was creating a new asset and keeping a reference to it. I searched the problem online and I found the following Stack Overflow post:  Investigating Android Memory Info.

The answer had a link to the issue reported in Android Open Source Project.

I read the issue and all comments. They claimed that it was fixed in 3.2.4. However, I am using 6.0.0 and seeing the same issue.

The Solution

One user on this page found a work around caching assets and it works fine for now. Until they actually fix it, this is the best we can do I believe:

public class Typefaces {
private static final String TAG = "Typefaces";
private static final Hashtable<String, Typeface> cache = new 
Hashtable<String, Typeface>(); 

    public static Typeface get(Context c, String assetPath) {
    synchronized (cache) {
            if (!cache.containsKey(assetPath)) {
                try {
                    Typeface t = Typeface.createFromAsset(c.getAssets(),assetPath);
                    cache.put(assetPath, t);
                } catch (Exception e) {
                Log.e(TAG, “Could not get typeface ‘” + assetPath + "' because " + e.getMessage());

                return null;

            return cache.get(assetPath);

In the past, I have seen this problem being reported on Stack Overflow, and I thought it was fixed. Apparently, the bug is still around. There are several blog posts/SO answers about this issue, this post is intended to show how we can catch it without doing deep investigation in Eclipse MAT.

Finally Graduated from College: #TheGradAwakens

After changing 3 colleges, 2 countries, and 2 majors, I finally ended 8.5 years of college life with a BS degree in Computer Science at the University of Nebraska at Omaha on December 18th.

It is a nice coincidence that my graduation day was also the release day of the new Star Wars movie The Force Awakens. Take a look at my cap and give a big shout out to Elisa for this awesome design.

Gokhan Arik Graduation Cap - TheGradAwakens

Gokhan Arik Graduation Cap – TheGradAwakens

Kantin Textbook Exchange Project Announcement

is now available at for University of Nebraska at Omaha students.

Today I am going to announce a project, Kantin, that I’ve been working on past two weeks. It is a textbook exchange website for students. For now, it will be available only for University of Nebraska at Omaha students, however I might add some other universities in the future.

What is Kantin?

Kantin (canteen) is Turkish word that stands for a cafeteria provided by an organization

such as a college or company for its students, staff, etc. In Turkey and some other European countries, Kantin is more than a cafeteria.

It is a place where students meet before and after classes, play games, talk about classes while having a cup of tea/coffee, and get together –usually 10-15 people– around a small table one hour before midterm starts and try to understand 1-2 months of course materials in that short period of time.

Kantin Project

The goal of this project is to help students to find textbooks from the students who studies in the same university and college. You can only register using your college email address. Once you register, you can either Sell Book on the website or you can Find Books.

To sell a book, you have to specify the college and university where you used the book. This will help other students to find books by university and college. For example, as a Computer Science major, I can visit the website and search books for University of Nebraska at Omaha -> College of Information Science & Technology. This will show me a list of books posted by students from the same college.

When you find the book you need, you can send email to seller clicking Contact Seller button. Kantin will send an email to user stating that you are interested to buy his/her book. After that all you need to do is to wait to hear from the seller student. Once you hear from the seller, all you need to do is to schedule a meeting on campus complete the transaction.


Kantin is completely free. Registration, contacting seller, completing transaction, and other everything is free. It also will be mobile-friendly website when it is completed.

Time Schedule

I was planning to release the website today, but it took longer than I thought to finish and I had to deal with some unexpected problems. I will hopefully publish the project by this Friday, January 9th. I included some sample screenshots from the application below.

Gokhan Arik


Kantin Desktop - Home Page

Kantin Desktop – Home Page


Kantin Desktop - Find Book

Kantin Desktop – Find Book


Kantin - Desktop - Sell Book

Kantin – Desktop – Sell Book


Kantin - Desktop - User Profile

Kantin – Desktop – User Profile

Kantin Mobile - Find Books

Kantin Mobile – Find Bo




















Kantin Mobile - Sell Book

Kantin Mobile – Sell Book

Kantin Mobile - Contact Seller

Kantin Mobile – Contact Seller










Closest Pair of Points Algorithm Python Implementation – Animated

This is Python implementation of the Closest Pair of Points algorithm. It uses pyplot from matplotlib Python library to show points on a graph and animate them.

This algorithm runs in O(nlogn) time.

The algorithm divides the array of points in the middle and recursively solves left and right side. If number of points is less than 3, it uses brute-force technique.

You can find the full source code at : Closest Pair of Points

Closest Pair of Points Python Implementation by Gokhan Arik

Closest Pair of Points Python Implementation by Gokhan Arik

Closest Pair of Points Python Implementation by Gokhan Arik

Closest Pair of Points Python Implementation by Gokhan Arik

Closest Pair of Points Python Implementation by Gokhan Arik

Closest Pair of Points Python Implementation by Gokhan Arik

Python Implementation of Depth First Search – Animated Traversal

Depth First Search (DFS) is one of the most important algorithms in computer science. This is Pyhton implementation of the algorithm with step by step animated traversal using networkx Pyhton library. 

You can find the full source code at : Depth First Search

P.S. I will post detailed explanation of code later.

Depth First Search by Gokhan Arik

Depth First Search by Gokhan Arik

Depth First Search by Gokhan Arik

Depth First Search by Gokhan Arik

Beatify – Spotify Application to Get Beats Per Minuet (BPM) for Playlist

UPDATE: Spotify has stopped supporting third party applications and they won’t be available in future versions of Spotify. They started to focus on their Web API.

This is one of the applications they developed using Echonest + Spotify , I would recommend using it instead of mine, since mine will not work anymore : SortYourMusic

Beatify is a Spotify application that shows beat per minuet (bpm) information for songs in a playlist. I’ve developed this application last year for personal use, but it wasn’t complete and I never had chance to publish.

It shows BPM for songs and allows you to Sort by BPM.

Spotify stopped app submissions for their market in March, 2013, but you can still use this application on your computer following instructions on Readme file.

Beatify on GitHub

Beatify - Spotify App to Get BPM for Songs by Gokhan Arik

Beatify – Spotify App to Get BPM for Songs by Gokhan Arik


Git Hook to Abort Commit If Any Error/Warning Exists in IntelliJ Project


Intellij Code Inspector git pre-commit script

Intellij Code Inspector git pre-commit script

Hello everyone,

I finally finished the code I have been working on for a few days.

This is a pre-commit hook for git and it will run Code Inspector of IntelliJ IDEA before every commit. If any error/warning exists, it will abort the commit.

More detailed information is in README file at GitHub :

GitHub – git-hooks by Gokhan Arik


Heartbleed Bug in OpenSSL

Hello everybody,

This is an important message for everybody about a bug which might have affected your passwords.

Probably you all heard about Heartbleed Bug in Open SSL. I will not dive into technical terms but many popular websites including Google, Yahoo, Instagram, Wells Fargo and Facebook was affected by this bug.

Which means someone else might have stolen your password or credit card information in last 2 years. You can check on this website if the bank, website you use was affected or not :

Many of major websites have fixed the bug and there is nothing to worry. Considering the fact that bug was around here for 2 years, someone else might have stolen your information in the past.

Here is what I would recommend:

1. Check the website you use if it is still vulnerable or not.
2. If it is vulnerable, it will not help to change password, just make sure you don’t have any credit card information on that website wait until they fix it.
2. If it is not vulnerable(as I said many banks and major companies fixed it) you should change your password.

Updates and more detailed info about the bug:!Dwijh