[mono-android] Webrequests, architecture and memory management.

Liam Houlahan liamhoulahan at gmail.com
Fri Jun 29 02:57:52 UTC 2012


Hi Everyone

I am having a lot of trouble with developing an app that efficiently uses
web requests frequently. I have looked at the Xamarin MWC sample
application and tried implementing the techniques used there for
downloading and updating the UI but my app is still using large amounts of
memory and running out of memory. The result of this is that the app
freezes and it not functional at all and the phone heats up.

To get a better understanding of how this stuff works with monodroid I
thought I would try asking here for some help.

The scenario that I have for my app is. On first start up each day the app
will do an initial large download. By large I mean a couple of hundred kbs.
After the large download when the user accesses parent activities for the
first time they will download all of the child data.
>From the child activities the user can update the information in the child
activities by clicking a button. These updates are much smaller but more
frequent.

I am using static service classes for webservice and database access.
Within each I am using a sync lock object to handle multi threading. To
download the data I am using WebClient.DownloadStringAsync (these have all
been taken from the MWC sample app).

After each webrequest the data that is downloaded is deserialized and saved
into the database. I am using sqlite-net for saving the data into the
database and NewtonSoft JSON.Net for deserializing the data into objects.

To update the UI for the large download I am using event handlers in my
activities assigned to a static event in my service class. For the other
smaller requests I am using callbacks to the Activities.

I can't really pin point exactly where or why so much memory is used but I
am hoping that someone may be able to give me some pointers or show me some
other sample apps that may help with the design of my app.

Some more specific things that I am wandering about are:

 - What is the best way to update the UI from async webrequest methods?
i.e. using action callbacks or events.

 - Should database access and async webrequests be initiated from threads
separate to the UI thread or is it best to just call them from the UI
thread? If it is better to use separate threads where should these threads
be started? e.g. in the activity.

 - For manually calling the Garbage Collector, should the GC be called
after each web request? If it should which part of the app should the GC be
called in and when? i.e in the service class after the download or in the
activity after displaying the updated results?

 - If a user opens an activity that starts a webrequest then goes back
opens another activity starting more requests how should the app deal with
this? Should the requests from the first activity be cancelled before
starting the next etc. If I am using a callback to the activity for an
async webrequest and the activity is destroyed before the webrequest
finishes what is the result of this?


Thanks
Liam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/monodroid/attachments/20120629/167ae826/attachment-0001.html>


More information about the Monodroid mailing list