Android Question Memory Profiling

DawningTruth

Active Member
Licensed User
Is there a simple way to determine the memory usage of an Object? Something like this:

B4X:
PseudoCode

myObject As Object
objectMemoryProfile As ObjectMemoryProfileClass

memoryUsage = ObjectMemoryProfile(myObject)
 

emexes

Well-Known Member
Licensed User
Is there a simple way to determine the memory usage of an Object?
<JOKE>In summary: Manfred's answer is that you can't, and my answer is that you don't want to.</JOKE> It's too painful. I can feel my shoulders sag sometimes when I think about how each say simple 32-bits in-theory number is blowing out under the hood to many multiples of that in size.

But it's always good to know thy enemy in (the coding) battle, and a super-good read is:

Sizeof for Java - Object sizing revisited

which saved me years ago from being tricked by the measurement-by-serialization method.
 

DawningTruth

Active Member
Licensed User
Thx Emexes I will read that article.

Erel, I need to decide the most efficient way to perform lazy loading of the objects. So let's say I have 1000 of these blobs. I only need to show 5 at a time to the user. So I need to store the other 995 objects somewhere so it does not cause memory issues. I also need to store them intact in the form of a UI element. The main consideration is speed in terms of retrieval for the lazy load. I don't want the user waiting too long during the lazy load.

I want to be able to make these decisions based on how much memory the object uses. So I need to be able to profile the size of the object in different configurations to determine which structure will work. Also there is the more general question of, how much memory is my App using. So I have arbitrarily chosen the number of 5. Perhaps I can even use 10 loaded objects at a time, but I don't have the data to base that decision on.
 

DawningTruth

Active Member
Licensed User
Sounds like a preoptimization. Start with the simplest implementation and see if it works good enough. In most cases it will.
Thx will do.

I did find a Profiling Tool in the Play store, which gives overall memory usage. Running my app increase RAM by about 600K. Running 100 of the objects without lazy loading increases RAM by 1.3GB and slows down my Note 8 test phone, making the UI hang. Loading 1000 saved objects in the background in RAM increases memory by 1.6GB. And has a slight delay in creating them of about 30 seconds. Considering that my Top End phone only has 7GM RAM, with 2GB free, I think memory may become an issue, especially with smaller and older phones.

I'm thinking perhaps I should just store the 1000 objects in an SQLite database. Most people have memory cards with significant storage so an extra gig or two should not be too much of an issue for them.

Do you perhaps know if there are significant performance hits if I store the 1000 objects in a SQlite database and then access them 5 at a time? i.e. Is Retrieval from SQL Lite fast?
 

emexes

Well-Known Member
Licensed User
Are these 1.6 MB objects images? Are you scaling them down to match the display resolution? (as in: displaying a 2000x1000 image in a 400x200 imageview = latent 96% reduction, and with zero visual difference)

Something I have occasionally done is to also save image thumbnails in the database, and then when displaying the data in a list, first paint the ones that have thumbnails, and then (while the user is admiring those ;-) load the non-thumbnailed elements (and at the same time: generate and save thumbnails for them for future use), and then finally, if the user is still with us and we've got nothing else to do, repaint the displayed thumbnails at better quality using the full image data.
 

emexes

Well-Known Member
Licensed User
I was quite surprised to find that the individual Blob fields are only about 1K each.
I thought I saw 1000 objects ~ 1.6 GB.

Speaking of memory (ab)use, I looked up a local bus timetable on the web today, everything went slow, and I eventually found that Chrome was using 520 MB (YES, OVER HALF A GIG) to display ONE text-only web page. Wtf?
 

DawningTruth

Active Member
Licensed User
I thought I saw 1000 objects ~ 1.6 GB.

Speaking of memory (ab)use, I looked up a local bus timetable on the web today, everything went slow, and I eventually found that Chrome was using 520 MB (YES, OVER HALF A GIG) to display ONE text-only web page. Wtf?
Yes, chrome is an insane memory hog. I'm sure of my current 5GB usage Chrome is about 4GB of them.
 
Top