Android Question Show available memory to detect memory leak

wimpie3

Well-Known Member
Licensed User
Longtime User
I seem to be getting an out of memory after adding and removing about 50 bitmaps. There somehow seems to be a memory leak.

I've tried to log the current memory usage by using this sub:
B4X:
Sub GetFreeMemory As String
    Dim ph As ICOSPhone
    Return ph.UsedMemory & "/" & ph.AvailMemory & "/" & ph.TotalMemory
End Sub

However, the returned values show there is plenty of memory available (several GB's), even when the app crashes with an out of memory.

I probably need to log another value, but how?
 

wimpie3

Well-Known Member
Licensed User
Longtime User
This does the trick:
B4X:
Sub GetFreeMemory As String
    Dim r As Reflector
    r.Target = r.RunStaticMethod("java.lang.Runtime", "getRuntime", Null, Null)
    Return ("available Memory = " & ((r.RunMethod("maxMemory") - r.RunMethod("totalMemory"))/(1024*1024)) & " MB")
End Sub
 
Upvote 0

MaFu

Well-Known Member
Licensed User
Longtime User
On Android, Java cannot use all available memory. Therefore if an app uses many or big images it runs often out of memory.
If you have no memory leak and the bitmaps are the problem then try to add
SetApplicationAttribute(android:largeHeap, "true")
in the manifest editor.
 
Upvote 0
Top