Android Question Used memory

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Looking at used and available memory and using this code, copied from here:
https://www.b4x.com/android/forum/threads/memory-usage.97349/

B4X:
Sub LogMemory
 
 Dim r As Reflector
 Dim iFreeMemory As Int
 Dim iMaxMemory As Int
 Dim iTotalMemory As Int
 Dim iUsedMemory   As Int
 Dim iTotalFreeMemory As Int

 Dim strPrompt As String

 r.Target = r.RunStaticMethod("java.lang.Runtime", "getRuntime", Null, Null)
 
 iFreeMemory = r.RunMethod("freeMemory") / (1024 * 1024)
 iMaxMemory = r.RunMethod("maxMemory") / (1024 * 1024)
 iTotalMemory = r.RunMethod("totalMemory") / (1024 * 1024)
 
 iUsedMemory   = iTotalMemory - iFreeMemory
 iTotalFreeMemory   = iMaxMemory - iUsedMemory
 
 strPrompt =   "Max Memory  = " & iMaxMemory & " Mb" &  _
                 CRLF & "Total Free        = " & iTotalFreeMemory & " Mb" & _
                 CRLF & "Total Memory = " & iTotalMemory & " Mb" & _      
                 CRLF & "Used Memory = " & iUsedMemory & " Mb" & _
                 CRLF & "Free Memory  = " & iFreeMemory& " Mb"       

 RunLog(strPrompt)
 
End Sub

I get these values:

*** Service (starter) Create ***
Max Memory = 256 Mb
Total Free = 254 Mb
Total Memory = 7 Mb
Used Memory = 2 Mb
Free Memory = 5 Mb

after application fully loaded:
Max Memory = 256 Mb
Total Free = 250 Mb
Total Memory = 11 Mb
Used Memory = 6 Mb
Free Memory = 5 Mb

The last 4 values can vary by a few Mb, this is after the app is fully loaded.

Now, I am not sure what the exact meaning is of these 5 values.
Could somebody explain this to me?

RBS
 

emexes

Expert
Licensed User
Well, that's some interesting stuff. On my usual phone here:

Max Memory seems to be the maximum amount of memory that the program can use, and if I Dim an array that exceeds it, then I get a beautiful java.lang.OutOfMemoryError. My test app has only 128 MB compared to your 256 MB.

Total Memory seems to be the current usage, but it doesn't go backwards if I Dim the array smaller (or perhaps Sleep(2222) is not long enough for the garbage collector to kick in). If I re-Dim the array back to a larger size, it reuses the memory that the garbage collector should have reclaimed, so: at least that works.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I don't deal with large images, but could get out of memory errors when moving large recordsets to a flexible table. I am avoiding that by simply avoiding the table display if rs.rowcount x rs.columncount > X.

RBS
 
Upvote 0
Top