Android app: out of memory exception

An android app has some limitations when it comes to memory availability. Unlike a normal Java app, you do not have luxury of expanding memory your application uses, as it will be completely dependent on the device on which the app is running, memory available in the phone, number of applications running simultaneously. We will not have control over these external factors so we will need to optimize memory usage on our end.

Some of the techniques I used in one of my last projects which used a lot of images/ bitmaps and hence ran into many memory issues.

  • Use of flags: Set android:hardwareAccelerated=”true” and android:largeHeap=”true” flags in manifest file. Downside is that these flags are only available after android 3.0. More than 50% of android devices still run on gingerbread (2.3+), so this is not a very reliable solution if you are targeting a larger audience. Though worth a try for high end (3.0+) devices.
  • Memory leakage: DVM grabage collection works n principle of JVM garbage collection, that is it will reclaim the memory for an object if there is no reference to it. So make sure when you are done with an object and do not need it anymore, mark the object reference as null.
  • Use minimum space for an object: Design your classes intelligently, for example if you are handling student data and you address details will not be used frequently, create a separate class and initialize the object only when it is required. Remember, if a char can work, don;t use int or long.
  • Objects are always placed on heap: One of my colleagues was wondering that how can an object occupy space, after the method exits, if the object is declared inside a method. Remember, only references are stored in the stack for method, the actual object is always stored in the heap. So the memory will be freed only when next cycle of garbage collection occurs
  • Use Garbage collection: You can call system.gc(); to initiate garbage collection, but this only suggests JVM/DVM to run a garbage collection, you cannot force it.
  • Playing with Bitmaps/ Images: Bitmaps and images take highest amount of memory, so make sure you recycle() bitmaps as soon as you are done using it. Make sure you have removed all references (imageviews/ objects) to bitmap before calling recycle().
  • Handle Activity Stack: If the app can be used back and forth multiple times, it is better to handle the back-front flow in the code, as default back button handling in android needs to keep a tack of activity stack and uses memory for that. Simplest will be to call previous activity on back button through code and always finish current activity before moving to next.

Test your SQL

SQL Fiddle is the place to test your queries and database design. You can simply share the links with other team members to let them know what you are thinking. It provided a number of database platforms like Oracle, Mysql, MS Sql, Postgres etc.

What is Type Erasure?

Since Java 5, Java has come up with the concept of Generics. Idea of generics came from the fact that developers at times faced issue with assignment of data types to Lists and other structure. In case a wrong type was assigned or was tried to fetched, error could be caught at run time only

Example

ArrayList arr=new ArrayList();
arr.add("kamal");
Integer i=arr.getIndex(0); //run time error

So Java gave us an option

ArrayList arr=new ArrayList();
arr.add("1");
Integer i=arr.getIndex(0); //compile time error

But we should note that Generics is a compile time concept. When Java convert the code to bytecode for JVM, it removes all generics related info. So finally

ArrayList arr=new ArrayList();

will be converted to

ArrayList arr=new ArrayList();

This concept is known as Type Erasure. This was necessary for Java in order to give backward compatibility, otherwise the code written earlier would not have been supported by newer JVMs.

Accessing localhost from virtual machine or Android Emulator

Two problems- one solution i.e. 10.0.2.2

Sometime back, I faced an issue that I had installed a virtual machine on mu linux machine. I had a server/ website running which I wanted to access from virtual machine. http://localhost/site simply refused to work. A little googling helped with an answer to use http://10.0.2.2/site

Again faced similar issue. Was trying to access local website from within android emulator. Again localhost refused to work. And guess who was the rescuer? yep 10.0.2.2 again 🙂

How to check logs in Android app

Where does all the statements from Log.v go when we run an Android app on emulator

Go to platform-tools under android sdk and type ./adb logcat

for example
kamal@ubuntu:~/softwares/android-sdk-linux_x86/platform-tools$./adb logcat

Adding/ Removing Browser Authentication for tomcat app

There can be many cases in your projects where you would like to add a browser authentication for your project. But there will be a few where you will actually need to remove that. That’s what happen to me today. Checked out code for a project and tried to run on local tomcat instance, everything worked fine. When tried to open up the app like localhost:8080/myapp, browser asked for username and password. I had no idea about username and password.

So started by googling about removing browser authentication in tomcat, that was a mistake. Could not find a single site talking about removal of authentication. After spending 20 minutes, I got the idea to actually look for- how to set the browser authentication. And bam- that worked.

In your apps web.xml, there will be setting for <security-constraint> where you can set up the <login-config> as basic (or actually remove in my case).

<!–
Define the Members-only area, by defining
a “Security Constraint” on this Application, and
mapping it to the subdirectory (URL) that we want
to restrict.
–>
<security-constraint>
<web-resource-collection>
<web-resource-name>
Entire Application
</web-resource-name>
<url-pattern>/members/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>member</role-name>
</auth-constraint>
</security-constraint>
<!– Define the Login Configuration for this Application –>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>My Club Members-only Area</realm-name>
</login-config>

Source point 5 of  http://oreilly.com/java/archive/tomcat-tips.html

100 Door puzzle

Puzzle: There are various versions of the puzzle, there are 100 doors (or some other object), and 100 cops (prisoners). All the doors are closed initially. Cop 1 goes and opens up all the the doors, Cop 2 goes and closes door at even number 2,4,6 etc. Cop 3 goes and alters the state of doors divisible by 3 (3,6,9), so if the door was closed it is opened and if it was open, it is closed by cop 3. Cop 4 will alter state of all the doors divisible by 4 and so on. At the end we have to find out how many doors are closed now.

Solution: Before getting to a generic solution, lets take case of few doors 1 by one. We can easily see that a door’s state is same if it was visited even number of time (initially closed than (open + close) (open + close).. ), and it is changed if the door is visited odd number of times.
Case of Door 1: Only visited once by Cop1, so the state changes to open
Case of Door 2: Visited twice by cop1 and cop2, so finally it is closed, no change in state (see it is visited even number of times)
Case of Door 3: Visited by 1 and 3- Closed
Case of Door 4: Visited by 1, 2 and 4- Open
Case of Door 5: Visited by 1 and 5- Closed
Case of Door 6: by 1, 2, 3 and 6- Closed
Case 9: 1, 3 and 9- Open
Case 10: 1,2, 5 and 10- Open

So on careful examination, figure out doors which have even number of divisors are not changing state. When will a number have odd number of divisors? A divisor pair of a number z is x*y (always 2, and hence even)

for 50: 1*50, 2*25, 5*10- Even
for 100: 1 * 100, 2*50, 4*25, 5*20, 10*10- Odd (As 10 appears twice)

So for all the perfect squares, we can see we have odd number of divisors and hence the state of the door will be changed.

Installing postgres 9

I was trying to install postgres 9 on my Ubunto 11.04 machine, but the GUI installer simply hanged. A little bit of googling provided the workaround.

Follow the steps

1. sudo apt-get install python-software-properties
2. sudo add-apt-repository ppa:pitti/postgresql
3. sudo apt-get update
4. sudo apt-get install postgresql-9.0 libpq-dev

Source
http://www.dctrwatson.com/2010/09/installing-postgresql-9-0-on-ubuntu-10-04/