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.

Friday, October 22, 2010

Looping in different Threads?

My last post ends with looping in different threads. Yes, that is not clear enough to understand what it is. Let check some situation where this may applicable.
In google app engine, It is a request based engine, where http request will be placed and our application should process and provide result back. But we have a limitation; each request should return within 30 seconds. But google app engine provides a task queue implementation where we can post request to that queue and which will be executed later. So, say if we want to process a request which last more than 30 seconds we should split that into small parts and post to task queue.
Mostly, if we want to process a collection, then we will go for looping. But in the case of google app engine, for every item of that collection, we should post request one after other by holding a counter in bigTable (google app engine datastore) .
Here if you see this we are looping a collection but each item will be processed in different threads.
Ok, Now think a senario where you want to loop from middle to end and start of a collection then we need to find some better way.

Thursday, October 14, 2010

Looping (count/2*(count%2==2?1:-1)) from middle to start and end.

Have you ever loop from middle to end and start of any list. i.e the index should move something like 0,1,-1,2,-2,3,-3 ...
One of the following would do:
1.
for(int i = 0 ; i < 5;i++){
System.out.println(i);
System.out.println(i*-1);
}

2.
for(int i =1 ; i <10;i++){
System.out.println(i/2*(i%2==0?1:-1));
}

of course, first one will print 0 two times. So,

1.1.
System.out.println(0);
for(int i = 1 ; i < 5;i++){
System.out.println(i);
System.out.println(i*-1);
}

Let me change these suitable for real situation:

1.1.
doSomethingForIndex(0)
for(int i = 1 ; i < 5;i++){
doSomethingForIndex(i);
doSomethingForIndex(0);
}

2.
for(int i =1 ; i <10;i++){
doSomethingForIndex(i/2*(i%2==0?1:-1));
}

If you look at these you will see no different in execution, because all statements run one after other.
But, say if we want to run doSomethingForIndex(i); in different threads one after other, then sample 1.1 will run two statements in one thread. So in that case it is better to go for option 2. (count/2*(count%2==2?1:-1))

Sunday, April 11, 2010

Counting boxes


I used to count boxes from any surface wherever I saw. Say, If we have a rectangle surface with boxes as shown here.

1. We can count all boxes one by one like this.

2. But, Yes, simply we can count rows and columns and multiply to get the total.


Yes that is 4x6=24, In this case we have to count two times(1-4,1-6)

3. What if we count like one of the following way.


We have to count only one time (1-6) but we have to remember the number once we reach edge.

I see counting diagonally means, counting both rows and columns at once. This different type of counting is possible(or we may use) in different situations in our life. May be sizing a problem etc. I am looking forward to see any real world examples/applications to match this. I just thought to write so you also can think.