views:

50

answers:

1

I just read a pretty interesting article on how android (and i assume other OSs) work when low on memory. How is this done theoretically? Is it similar to Java's object serialization?

+1  A: 

In a word: yes.

In a few more words, sort of. You have to handle more of it manually than personally I'd like. Essentially, all Android provides for you is a hash to shove a few serializable objects, referenced by strings, that is guaranteed to be safe across application shutdowns. So, whenever something happens that you'd like to preserve across a shutdown of your application, you are responsible for updating this saved state hash (and letting Android know that you've done so). This includes things like half-finished text entry in form fields. That means you have a lot to listen to.

Android will then call a particular hook in your Activity that handles restoring state to the Activity when it recycles your application and you need to do so. This doesn't happen for all recycles — there are various states of being/existence for your application.

The screwy part is that because you're expected to do this sort of tedious work anyway, Android gets lazy and implements things like screen rotation as a full recycle of your application.

I'm making it sound worse than it really is once you get used to it; it's really not a bad way of solving the problem in the confines of Java and mobile computing.

Of course, this is a response regarding Android. Other (desktop) OS's rely on Virtual Memory and Paging to deal with memory constraints.

Clint Tseng
Screen rotation is implemented as a full recycle for a very good reason: this lets the app easily reload all resources for a different configuration (strings, layouts, images, etc.) without having to manually replace tons of variables in memory.Also all default Android widgets have built-in support for persistence. Any widget with an id will save its state automatically (this includes text entry fields btw...).
Romain Guy
The text entry fields saving their state is new.As I say, it's not a poor solution to the problem. It just seems remarkably inefficient. Is their layout engine really not sophisticated enough to figure out which of the two sets of dimensions and resources to use? (the answer is yes, but it didn't have to be that way.)
Clint Tseng