Android Question Map list Crash

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

Just got a crash in my Google Play Console.

Looks like I have too many items in a Map list and caused an out of memory error.

Currently I have no limit on the number of items I am putting in the map, which I think it is causing it to crash since the device got out of memory.

I am thinking of only adding a maximum of 1000 items in the map, and when it gets 1000 in the list remove the oldest item in the map..

B4X:
If Map.Size > 1000 Then
   Dim lastitemKey As Int = Map.GetKeyAt(0)
   Map.Remove(lastitemKey)
End If
Map.Put("item,"item")

However the question I have, what happens if 1000 is still too many? Is there a way to detect if it's reaching the devices memory limit and reduce the amount of items being added to the map ?
 

aaronk

Well-Known Member
Licensed User
Longtime User
java.lang.OutOfMemoryError
anywheresoftware.b4a.objects.collections.Map$MyMap.put

upload_2017-5-23_17-27-14.png
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Are you storing bitmaps in this map?
No, only storing text.

This issue doesn't happen on my device, and only noticed this has been reported by 1 user in the Google Play Console.

Just looking at what can be done to prevent it from happening again. (hopefully this is a one off crash)
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Large text?
map.put("approx 10 ASCII characters here","approx 30-40 ASCII characters here")

What's the best way in detecting the available memory before a crash happens?
Is there a way to detect the out of memory before it happens, so that I can display a message to the user that the app is about to crash due to the low memory available ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
See this post: https://www.b4x.com/android/forum/t...-to-check-memory-available.58925/#post-371102

map.put("approx 10 ASCII characters here","approx 30-40 ASCII characters here")
A map with 1000 items this size will not cause a memory issue.

Tested with 100,000 items:
B4X:
Dim m As Map
m.Initialize
For i = 1 To 100000
   m.put("approx 10 ASCII characters here" & i,"approx 30-40 ASCII characters here" & i)
Next
Log(m.Size)

Setting the largeheap attribute can be useful to prevent such issues, assuming that there is no memory leak.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
this has been reported by 1 user in the Google Play Console

A map with 1000 items this size will not cause a memory issue.

Tested with 100,000 items

1) are you using only that map or also a View to display map data?
2) can you test your app on an emulator of the user's device?
3) can the user device be defective?
4) can the user be a "disturber"? :)
5) can I avoid writing this kind of post? (probably not :p)
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
A map with 1000 items this size will not cause a memory issue.
I agree, since I have tested this on a number of my own devices with any issue plus have a few guys in the office with Samsung devices and no one else has reported this issue before.

I guess it's just this one user and hopefully it's a one off issue. I will monitor it and see how it goes.

1) are you using only that map or also a View to display map data?
2) can you test your app on an emulator of the user's device?
3) can the user device be defective?
4) can the user be a "disturber"? :)
5) can I avoid writing this kind of post? (probably not :p)
I can't seem to reproduce this issue, so I am guessing it's just this one device with this issue and hopefully it's a one off thing.
 
Upvote 0
Top