My service is taking too much memory

Inman

Well-Known Member
Licensed User
Longtime User
My app has a service running as well. The problem is when I check the "Running" settings of my phone, my service is taking considerably more RAM than other services. Most of the other services are in around 5 MB to 10 MB range (except Facebook which uses 15 MB) while my app uses 23 MB! And possibly due to this heavy usage, my service is getting killed frequently.

What happens in my service is that I am accessing an sqlite database to get some urls (about 4), then download those json files using HTTPUtils service (so basically there is a service calling another service), parse the files using jsonparser, show a notification if necessary and in the end update the same sqlite database with new values.

All this takes up more than 20 MB of RAM. Is this normal? What normally increases RAM usage in a service?
 

Inman

Well-Known Member
Licensed User
Longtime User
The app does involve downloading and displaying some bitmaps in 500x500 range. But the service module doesn't touch those.

I noticed another thing just now. This service is set to start at boot. When the phone is restarted, the service gets started as well and at that time it takes only about 5 MB. But when I open the app and then hit back button to exit, the memory usage jumps to 15 MB. And unfortunately even though I closed the app, the usage still stays at 15 MB.

I thought the memory used by service is based on the actions performed in the service alone.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
I have some Bitmap variables (Dim b as bitmap) in my activities, which might be using RAM. How can I unallocate these variables after displaying the bitmap on imageviews and listviews?
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
I dug further into my "service getting stopped frequently" issue and here is what I noticed on logcat file:

B4X:
No longer want <mypackagename> (pid 21444): hidden #16
Scheduling restart of crashed service <mypackagename/myservicename> in 5000ms

This scheduled restarting is not happening in my case, although in case of some other apps I saw the above lines but immediately followed by:

B4X:
Start proc <anotherpackagename>/<anotherservice>: pid=24292 uid=10099 gids={3003, 1015}

But in my app's case that Start proc is not happening automatically after service crash. I looked further and found this thread at stackoverflow

Android: How to force restart service (OS killing on low memory behavior) - Stack Overflow

The person there is talking about starting the service with onStartCommand() using "START_STICKY" flag. Is that how you are doing it, Erel?
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
I am trying to figure that out.

In the mean time I went in detail through my logcat file. I have a Samsung Galaxy Note running Android 4.0.4 AOKP based ROM. In my "Running" tab I can see 12 services that include the likes of Facebook, Swype, Maps, TvOut and of course my app etc...Now I checked my logcat for the last 3 hours and I noticed that almost all of these services have crashed at least once or twice. This might be an issue with my phone alone, probably due to the custom ROM and kernel I am using.

But the thing is, even though all 12 services crashed, 11 of them came back within 5 seconds, as mentioned in the logcat entry (Scheduling restart of crashed service <mypackagename/myservicename> in 5000ms). My service didn't come back, which got me thinking.

Also even though logcat says the service has crashed, I never saw the "process has crashed" screen, either for my service or for others. I think this crashing might be a natural process as part of the OS, may be to free memory. Also in the logcat file, these entries are not marked as errors but as warnings.

Please check the screenshot given below, of my logcat file showing what happened to Facebook service. This crashing and restarting happened to all other services except for my app's. The crashing mentioned there happened just 30 minutes back while the phone was idle on charging dock.

http://i.imgur.com/8GnmS.png
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Yea I thought so. Fortunately I found agraham's code to recycle bitmaps. I believe we don't need to do this normally but in my case the activity deals with about 65 images that are about 300x300 in size. Recycling those bitmaps have brought down the memory foot print by at least 40%.

Now the issue that remains is service crash. If only we had the option to start services in sticky mode, we wouldn't have to worry about the services getting killed by OS. Will it be possible for you to add to B4A's upcoming update, an option to start a service in sticky mode, if it is not too much trouble for you to implement? I believe it will only work on Android 2.1 and above, but that market is like 99% today.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
I do have it scheduled to run every 3 hours using StartServiceAt. But here is my doubt. If my service is not present in the list of running services, will it execute after 3 hours as per my schedule?
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
To answer my own question, yes it will. Even if your service is not in the list of running services at some point of time, if you have a schedule in place using StartServiceAt, the service will start at the right time automatically.

So, you were right Erel. The correct way to make sure that a service keeps running is by using Service.StartForeground or StartServiceAt. Sorry for all the panic and confusion.
 
Upvote 0
Top