Friday, November 26, 2010

எப்படியும் ஒருநாள் திரும்பி போறதுதானே

ஆறு பரப்பு காணிவித்து...
ஐந்து பரப்பு காணி வாங்கி...

அப்பப்பா வந்து ...
அடிக்கல் வைத்து...
அத்திவாரம் போட்டு ...

கருத்தகொளும்பான் மரத்திடியில் ...
கலரிந்து, அடுக்கி, தண்ணிவிட்டு ...

சுத்திச் சுவர் எழுப்பி - அதில
கதவு வச்சு ...
யன்னல் வச்சு - அதுக்கு
மழை பட கூடாது என்று - சன்செட்
முற்றத்தில போட்டிக்கோ ...

வெளிச்சுவர் இருக்க ...
உட்சுவர் பூசி...
ஊருக்கெல்லாம் சொல்லி - குடிபூரல் ....

வைரவர் கோயிலிருந்து - படம்
வழி நடையா கொண்டுவந்து ...
பெரிய மாமா தேங்காய் உடைக்க ...
பெரிய அறையில சாமிப்படம்...

தும்பிக்கை சரிபார்த்து - பிள்ளையார் படம் ...
துணைவி வள்ளி, மனைவி தெய்வானை, முருகன் ...
மூன்று பெரும் நிக்கிற ஒரு படம்...
முக்கியமா அவையால் இருக்கவேணும்!

இலச்சுமி படத்துக்கு ...
இலச்ச கணக்குல கண்டிஷன் - வீட்டில
தங்கம் குவியவேண்டும் என்று - காசெல்லாம்
தட்டில விழவேண்டும் - சிதறக்கூடாது ...

அத்திவாரம் போட்டு ...
ஆறு மாதத்தில் அடி வைத்தாலும் ...
ஆடு மாடு கட்ட என்று ...
அருகில ஒரு கொட்டில் ...

எல்லாரும் இப்படித்தான் வீடுகட்டுறது...
எல்லாம் கட்டி என்ன செய்தது - கடைசீல...
எவனோ திறத்த ஓடிவந்ததுதான்...
"எப்படியும் ஒருநாள் திரும்பி போறதுதானே"
எத்தனை காலம் போட்டு இப்படி சொல்லிச் சொல்லி...

இருபது வருஷம் தாண்டி...
இப்ப விடுகினமாம் ...
..................................
இன்னமும் எழுதவேணும் ...

Tuesday, November 23, 2010

Android: When to persist data or release resources if that is expensive?

android: When to release resources?

In android, life-cycle of each application is mostly controled by its Activities. Each Activity has its own left-cycle, you can get detailed information from this link.

Mostly, these Activities interact with user while those are in foreground. Android will put the activity to background in following cases:

1. If the activity starts another activity

2. or in any interrupts, like as, got a incoming call or pressing home button etc.

And if finish() called or back button pressed, it will stop the activity.

Here the problem is with the activities which are brought to background. In one point of time android will stop and remove these activities from the activity stack. There is no guarantee as android will call any call back method before it stops those activities.

Only guaranteed call back is onPause, which will be called while an activity go to background. So, It is recommended to persist state or necessary data and release all allocate resources (Files may have opened in Native or allocated memory etc.)

If you look at the following diagram, you will understand all onPuase calls.





But, in some case it is more expensive to persist the state of application data and releasing resources in every onPause calls. Actually we don’t need to do this if user moving around inside our application. In the above diagram, in each onPause call if it with green pop-up, then we don’t need to persist or release. In other cases we need.

Additionally android provide isFinishing() API to check whether the activity is finishing or not. But that is not clearly provide an idea when to do the persistence and not. We need another flag to indicate while an activity pauses due an new child activity started from that activity.

If i put this into a table:

Main Activity

Child Activity

Start Activity

don’t persist

don’t persist

call finish() or back pressed

persist

don’t persist

Home button pressed, got an incoming call

persist

persist

Let check the following class to track for staring child activity state.

import android.app.Activity;

import android.content.Intent;

public class TscaActicity extends Activity {

private boolean mStartingChildActivity;

public void setStartingChildActivity(boolean startingChildActivity) {

mStartingChildActivity = startingChildActivity;

}

public boolean isStartingChildActivity() {

return mStartingChildActivity;

}

@Override

protected void onResume() {

setStartingChildActivity(false);

super.onResume();

}

@Override

public void startActivityForResult(Intent intent, int requestCode) {

setStartingChildActivity(true);

super.startActivityForResult(intent, requestCode);

}

/... other start * call should flow the same patter ../

}

Let put both isFinishing() and isStartingChildActivity() together in a table:

Main

Main

Child

Child

isFinishing()

isStarting

ChildActivity()

isFinishing()

isStarting

ChildActivity()

Start Activity

false

true

false

true

call finish() or back pressed

true

false

true

false

Home button pressed, got an incoming call

false

false

false

false

So finally, we need to write a Main Activity and Child Activity as follows:

import com.muvee.studio.activity.TscaActicity;

public class MainActivity extends TscaActicity {

@Override

protected void onPause() {

if(!isStartingChildActivity()){

//persist

//release

}

super.onPause();

}

}

import com.muvee.studio.activity.TscaActicity;

public class ChildActivity extends TscaActicity {

@Override

protected void onPause() {

if(!isFinishing() && !isStartingChildActivity()){

//persist

//release

}

super.onPause();

}

}

Friday, November 5, 2010

Java References

For a long years, I have been working with Java, but now only working with mobile application. It is interesting, but sometime it is within a triangle sided by Performance, Quality and Resource. If we try to improve one then application may hit by other. Not like servers or desktop we can't increase resources as we need in mobile devices.

Lets look at Java References which provides support to reclaim objects when memory demand.

You all know that,
1. Java has garbage collector (GC) which will take care of removing unused objects, so no need handle object removable inside the program.
2. when java trying to create a object, if it can not allocate memory, then it will throw OutofMemoryError.
3. Even if you run a java application just with only one thread. i.e the main thread, there at least two thread will be running, one is the main thread and other one is the GC thread.

To check this out, I wrote a class:

class MyObject{
private int index;
private byte [] bs = new byte[16*1024*1024];
public MyObject(int index) {
super();
System.out.println("creating index="+index);
this.index = index;
}

@Override
protected void finalize() throws Throwable {
System.out.println("finalizing index="+index);
super.finalize();
}
}


When create a MyObject this will allocate 16*1024*1024 (=16Mb) memory. And will print "creating index=*" while creating and "finalizing index=*" while removing from heap.

1. First, lets create a unreachable objects.
private static void unreachable() {
for(int i = 0;i<1000;i++){
new MyObject(i);
}
}


If you run this you will get:
creating index=0
finalizing index=0
creating index=1
finalizing index=1
creating index=2
creating index=3
finalizing index=2
finalizing index=3
...
This means these objects will be removed each next CG thread.

2. Strong References:
private static void stongReference() {
List references = new ArrayList();
for(int i=0;i<1000;i++){
references.add(new MyObject(i));
}
}


creating index=0
creating index=1
creating index=2
creating index=3
creating index=4
creating index=5
creating index=6
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at DemoReferences$MyObject.(DemoReferences.java:12)
at DemoReferences.stongReference(DemoReferences.java:50)
at DemoReferences.main(DemoReferences.java:35)

Yes you will get OutOfMemoryError, because non of the objects can't be collected since all are strong reference.

3. Weak Reference
private static void weakReference() {
List> references = new ArrayList>();
for(int i = 0; i < 1000;i++){
references.add(new WeakReference(new MyObject(i),referenceQueue));
}
}
creating index=0
finalizing index=0
creating index=1
creating index=2
finalizing index=1
creating index=3
finalizing index=2
finalizing index=3
...
This looks same as unreachable objects. But as long as it available by calling WeakReference.get() can get the object back.

4. Soft Reference
private static void softReference() {
List> references = new ArrayList>();
for(int i = 0 ; i < 10000;i++){
references.add(new SoftReference(new MyObject(i),referenceQueue));
}
}


creating index=0
creating index=1
creating index=2
creating index=3
creating index=4
creating index=5
creating index=6
finalizing index=6
finalizing index=5
finalizing index=4
finalizing index=3
finalizing index=2
finalizing index=1
finalizing index=0
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at DemoReferences$MyObject.(DemoReferences.java:12)
at DemoReferences.softReference(DemoReferences.java:64)
at DemoReferences.main(DemoReferences.java:36)

This may looks like Strong Reference, but VM make sure to remove all object before throw OutOfMemoryError. So, the error can be caught and try again.

private static void softReference() {
List> references = new ArrayList>();
for(int i = 0 ; i < 10000;i++){
try{
references.add(new SoftReference(new MyObject(i),referenceQueue));
}catch(OutOfMemoryError error){
System.out.println(error.getMessage());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
references.add(new SoftReference(new MyObject(i),referenceQueue));

}
}
}


creating index=0
creating index=1
creating index=2
creating index=3
creating index=4
creating index=5
creating index=6
finalizing index=6
finalizing index=5
finalizing index=4
finalizing index=3
finalizing index=2
finalizing index=1
finalizing index=0
finalizing index=0
creating index=7
...

Here also SoftReference.get() will give you the object if not removed. In this way we can keep the objects for long time as long as there is a memory demand.
This will be a good approach to create a memory sensitive cache.