Memory Management in Titanium Application

A virtual place which is totally responsible to store and hold our data in this virtual world of mobile or internet is Memory. As this is limited in mobile devices so while developing any application used by them, we should keep this Memory in our mind. And due to this step of Memory Management comes to play.

Memory and storage are looks similar but if we look closely, can find that Memory are volatile location where application and its related data are stored temporarily when application get processed. And the storage had the places where we store our data as long as we need. In mobile devices both storage and memory related things are handled by ship and also by SD Card. Memory spaces varying device to device. In any device there is a limitation for memory consumed by different application and it also varied from device to device. So memory consumed by our application should not exceed the memory limitation provided on device. Violation of this will result crashing of that application unexpectedly. That’s why memory management is the crucial thing that we need to be handled carefully while developing any application.

In Titanium we are actually write our code in JavaScript. Each and every variable, objects made in our code occupy some space in memory. As the number of variables and objects increased the space in memory gets reduced.

In JavaScript, there is a mechanism called JavaScript Garbage Collection, where the interpreter determines which variable is no longer needed by the process and then they it releases the memory occupied by them. Here we can assign null to the variables and objects those are no longer needed. Actually Titanium is third person in between JavaScript and native OS. In titanium suppose we have created a view object, it automatically creates proxy in native OS. So if we destroy any objects in JS automatically its corresponding proxy get destroyed. Let’s have a look on the code snippet given below.

//Define the current window
Var currentWin = Ti.UI.currentWindow;

//Define a view
var mainView = Ti.UI.createView({
width : 100,
height: 100,
backgroundColor : ‘#FFFF’

//View added to the window

In above code block there is a view that we have added to a window. Whenever we have created the view it took few spaces on memory. After some time if we removed the view from the window, it does not mean that the memory used by the view got released. We need to explicitly assign null to the view object.

//removes the view from window

//assign nuul to view removes the space occupied by it
mainView = null;

There are various places where memory leakage occurs. Like in some cases we are hiding the controls whenever it was not required, this also does not means that memory get released by those controls. In some situation we are adding global events but forgot to remove those events which result memory leakages.

Written By: Raju Mahato, Software Developer, Mindfire Solutions


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s