Android Question High Usage by Service

madSac

Active Member
Licensed User
Longtime User
I am a big fan of VB6. And "was" as fan of B4A.
I started developing few apps for lowend devices those who have low ram. And now i am thinking that i should give up my idea of developing apps using B4A as they have very high ram usage & would be unfit to develop.

An small application with nearly no code uses 26MB of ram. And till now i haven't seen <20MB
Is this cost of using B4A or a RAD ?
I have attached screenshot you can compare it with many big apps. And below is code.
If you think i am going wrong point me in right direction, I will be thankful as you will save my 1000 line code :(
Main:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
StartService("test")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Service Module :

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub
Sub Service_Create
Service.StartForeground(0,Null)
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub
 

Attachments

  • Screenshot_2014-03-21-09-02-26.png
    Screenshot_2014-03-21-09-02-26.png
    410.3 KB · Views: 313
  • Problem.zip
    356.5 KB · Views: 263

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is ZERO overhead in your project that is caused because of Basic4android. You can rewrite it in Java. The result will be exactly the same.

Generally speaking the memory footprint and performance of B4A apps is more or less the same as applications written in Java.

Make sure to test the memory usage in Release mode only. Tracking memory usage in garbage collected environments such as Android can be tricky. The memory can grow until the system decides that it needs to free available memory.

Note that passing Null to Service.StartForeground can be dangerous.
 
Upvote 0

madSac

Active Member
Licensed User
Longtime User
Thank you erel for reply.
Can you suggest any resource/document which will help me to reduce memory usage.

I have tested memory in all modes but this was the least in release mode.

I just want to optimize my Application for memory if you can suggest few tricks for B4A leaving bitmap and resources suggestions.

How can i profile my B4A code ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should only test the memory usage in release mode.

If you haven't seen any "out of memory" exception then in most cases you do not need to optimize anything. Specifically the code you posted above cannot be further optimized.

The main point of this thread is a claim that Basic4android adds a lot of overhead. This is completely not true. Both the performance and the memory footprint of Basic4android applications are similar to applications written in Java.

There are many threads in the forum about bitmaps. The main thing to do is to use LoadBitmapSample to avoid loading large images.

However if you have any other specific question then please start a new thread.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Hello @madSac
There's nothing wrong with B4A and it has absolutely nothing to do with your memory usage. If you stop using B4A it's your loss :(

After writing a few widget, I've found that my services run at between 1.8MB and 9MB. In all cases the memory is managed automatically by Android and will drop back down automatically.

I will admit it though, I've quickly tried your attached file and the service was running at 15MB. Two of my heavily graphical services are also viable on the screen shot below, 24th Century StarDate and TIX Clock. Both are relatively small in memory usage size, but both have lots of png files added to their assets folder.

BIG HINT: If you change the line in the manifest file from android:targetSdkVersion="14" to android:targetSdkVersion="13", your memory size WILL be smaller, about 6MB in my case with your downloaded file. Thus proving that it's the Android SDK from Google and not B4A, please see the screen shot below. BTW you should not be using Null in Service.StartForeground.

Screenshot_2014-03-21-18-41-05.jpg
 
Last edited:
Upvote 0

madSac

Active Member
Licensed User
Longtime User
@Peter Simpson Thank you for your reply.

My old compiled program,used around 6mb.I digged my old programs which i compiled using 2.3 gingerbread.
Now i am using KitKat so compiled to my current version. I was going to test the issue in Galaxy Y but each time i did i was busy some where else :),And to my bad luck i created a thread :).
I created thread because i didnot wanted to loose B4A :D

You just hit light on my brain there are many similar cases.
The final conclusion I made is More the features in your phone higher is memory usage.
Ex : Higher camera megapixel higher usage of camera control, more are sensor,higher is your android version

Thank you for helping me out.
 
Last edited:
Upvote 0

madSac

Active Member
Licensed User
Longtime User

Attachments

  • Screenshot_2014-03-23-08-30-08.png
    Screenshot_2014-03-23-08-30-08.png
    423.8 KB · Views: 249
Upvote 0
Top