Help Fixing Memory Leak in b4a app

yonson

Active Member
Licensed User
Longtime User
Hi
I have an app out on google play and have reached my (limited!) limits with my B4A expertise.

The app is live and working fine, however within a few minutes of operation it slows down rapidly between Activity screens, and eventually crashes. I'm reasonably sure that its the result of the rendered images not been removed from memory correctly but there may be a number of underlying issues.

The link to the app is https://play.google.com/store/apps/details?id=uk.co.iyork.iyork_android&hl=en

I would be looking for someone to help just clean up the errors, this might not necessarily be an actual fix to the complete app, but certainly identifying the specific causes and the necessary code fixes to implement, as it is quite a large app.

If people could get in touch with some quotes and ideas, happy to send through the project files etc
Thanks
John
 

yonson

Active Member
Licensed User
Longtime User
thank you I really appreciate your reply - I'll give it a go this afternoon, I assume its required in all the Activities?
 

eps

Expert
Licensed User
Longtime User
I don't think this will sort out the issue for you. Whilst it is a step in the right direction there's a difference between indicating that something can be recycled and the recycling being performed. You will almost certainly get the same issues.

Saying that there are steps which you can use to minimise the use of memory with regards to graphics. If you want, I am happy to look at the code and recommend some changes. If they make a difference then pay what you feel is appropriate. No fix, nothing to pay. PM me if interested.

Are the problem devices Samsung btw? They seem to cause no end of issues.

Have you run Memory Usage whilst using the App to check peak memory usage?
 

LucaMs

Expert
Licensed User
Longtime User
However, if the images are not huge (and it is useless to have gigantic images that you can not show), I think that when an activity is finished, the objects are destroyed (and thus also the views).

The memory consumed by the entire app increases if you incorporate many image files, in which case you can just download them instead of incorporating them into the project.

Reuse the bitmaps, when possible, of course.
 

yonson

Active Member
Licensed User
Longtime User
Hi guys
thanks again for all your help - yeah the same problem is persisting even with the GC call. And yes could well be a Samsung thing, how annoying!

I will start to look into reusing the bitmaps but its a major faff, on the iphone version the list only seems to grab / use the images when the are actually visible, whereas on Android it seems like they all have to be in memory at once, so if the list has 100s of entries it always falls over. We're limiting lists to around 30 results at the moment and its really not ideal for what the app is supposed to do.

Anyway have contacted eps about if they can look into it, if anyone else fancies a stab at it you can reach me on admin@iyork.co.uk

Really appreciate the help and advice!
Cheers
John
 

cbal03

Member
Licensed User
Longtime User
For android:
I find that inspecting the the heap in the Dalvik Debug Monitor (or whatever the latest tool is) while performing certain actions in the software (or set breakpoints for iterations) can shed some more light on the problem. The heap will adjust upward until an out of memory error occurs. You can also see when memory is released back to the system.

If you find that the code is working properly then (as others have suggested) possibly consider where your images are coming from or the file type used. I generally use .png type of files with non image data stripped away and the image sized for the view in use (resizing on the fly as B4A does still requires some processing power and memory usage above simply loading an image. Most imaging programs allow for this type of manipulation.
I don't have much experience with iOS but it seems to me that if the problem is cured in android then iOS stands a better chance of working too.

More Info:
I wrote a program that pulls records from a database and assigns an image to each in a list. So far I have a little over 200,000 records. So I load 100 into the list and use forward and back buttons to load either the next 100 or the previous 100 records. This limits the list to a maximum of 100 images. The list is overwritten (reused) to control memory problems.
 

Mark Zraik

Member
Licensed User
Longtime User
Hi yonson,

I downloaded this app from the PlayStore and I have to say "Wow" that's a great app!
I know it may not help me here in the States, but I've been a coder for quite some time, so I think I can look at this app in a technical manner and say Great Job!

I am running this on my Acer A200 with a custom ROM UltraBase with Android 4.0.3, and so far it is going along just fine.
I know it's a huge project, but I would recommend allowing a way to change the font sizes for some of the category and other info labels. Attached is a screenshot image.
As for the memory leak.. Did you ever solve that??
 

Attachments

  • Screenshot_2014-08-31-22-14-11.png
    331.8 KB · Views: 292

yonson

Active Member
Licensed User
Longtime User
Hi there
thanks for your kind comments its much appreciated!

Yes, we found the main problem was that I was loading all the images into the listview, and this listview could (in some instances) have 100s of entries, but of course only 5-10 or so are on screen at any one time. To Fix this we used the 'UltimateListView' library which is superb and fixed this straight away.

However, there were other problems found with memory leaks - the problem? Not actually the app, but actually Samsung(!) which have had some problems with memory leaks on the s3/note 2 which is what we developed the app on, and aren't getting as many problems elsewhere.

Finally however, I'd like to recommend Phil (eps on this thread) who was fantastic and took the time to review the code for us, he really knows his stuff and was a great help, I'd very much suggest anyone in any sort of trouble with their app hire his services as he was brilliant.

Thanks for the heads up on the font size issue, I'd be quite keen to fix this if possible and we did try to do something for this earlier on in the year, obviously it isn't quite right for all handsets.

We control the text size based on the screen width of the phone, and make it bigger for tablets etc :-

Dim width As Int =100%x

If (width>800) Then
Textsize = 14dip
Else
Textsize = 10dip
End If

sounds like we need to look at a more elegant solution. I'll start looking into it. Thanks for letting us know about the issue!
 
Reactions: eps

Mark Zraik

Member
Licensed User
Longtime User
I put this in one of my apps and it seems work pretty well as a Menu addition.
in the Activity_Create
B4X:
Activity.AddMenuItem("Device Font Size", "font_changer")

Then I added 2 subs, the first pops a msgbox for the user to choose the device type
B4X:
Sub font_changer_Click
myFont = "14"

Dim res As Int
res = Msgbox2("Select Your Type of Device"&CRLF&"If you are not sure, Choose Cancel", "Choose Your Device Type", "Phone","Cancel", "Tablet", Null)
If res = DialogResponse.POSITIVE Then
   myFont = "14"
     Else If res = DialogResponse.NEGATIVE Then
     myFont = "24"
       Else If res = DialogResponse.CANCEL Then
       myFont = IXStorage.GetSetting("safeFont")
End If
CallSubDelayed(Me, "saveMysettings")
CallSubDelayed(Me, "changeMyFont")'puts it in queue as a message, implemented at resume
End Sub

The second runs through all of the elements and forces the chosen font size.
B4X:
Sub changeMyFont
Dim thefont As String
thefont = IXStorage.GetSetting("safeFont")
'must run this or casting error in java runtime - Invalid Double
If IsNumber(thefont) Then 'handles the casting, thanks Erel!
'Log("IsNumber: " &thefont)
myFont = thefont
Else
myFont = 14
End If
'centerPanel
L1.TextSize = myFont
L2.TextSize = myFont
L3.TextSize = myFont
L4.TextSize = myFont
L5.TextSize = myFont
L8.TextSize = myFont
paidmiles.TextSize = myFont
offeredrate.TextSize = myFont
fuelcost.TextSize = myFont
deadhead.TextSize = myFont
moneyoffered.TextSize = myFont
efcost.TextSize = myFont

'cpanel
Label4.TextSize = myFont
Label3.TextSize = myFont
myrate.TextSize = myFont
mpg.TextSize = myFont
'exPanel
L6.TextSize = myFont
L7.TextSize = myFont
exprate.TextSize = myFont
exprice.TextSize = myFont
diffbyrate.TextSize = myFont
diffmoney.TextSize = myFont
'wifPanel
whatifBtn.TextSize = myFont
wiftotal.TextSize = myFont
testrate.TextSize = myFont
L11.TextSize = myFont
L10.TextSize = myFont
'calc and reset
rstBtn.TextSize = myFont
calcBtn.TextSize = myFont
'wizpnl
titlelbl.TextSize = myFont
sqrbottom.TextSize = myFont
wtext.TextSize = myFont
'help and quit???
hlpBtn.TextSize = myFont
quitApp.TextSize = myFont
End Sub

I suppose I could have cut out some of the parts for the posting here, but...I even left my comments.

It uses a stored value that is kept in a settings file. There are many other ways to do this I'm sure, and I would expect others to chime in

It allows you to code the monster once, and let the user decide. I chose 2 font sizes for mine, one is default, and the other was 24.

It's a nice feature to add, and it makes your app something that says to the user "Hey, they thought this through".
If this helps in any way, then that's awesome!
Thanks for your earlier reply,
Coroner
 

yonson

Active Member
Licensed User
Longtime User
Mark that is fantastic thank you so much for your help - we'll get something done with this over the next few days and if its ok I'll get back to you to see if it helps on your device.

Thanks again very much appreciated!
John
 

ivan.tellez

Active Member
Licensed User
Longtime User
That is not really a solution, nowadays, there are phones whith screens much bigger than some tablets, also, tablets could be 2560x1600.

For a real solution, you need to consider the actual size and the resolution of the screen to scale the fonts perfectly for each device.

If you are interested, I culd help whit this scalling isue on the fonts
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…