1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question Used memory

Discussion in 'Android Questions' started by RB Smissaert, Jul 16, 2019.

  1. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    Looking at used and available memory and using this code, copied from here:

    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(
     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"       

    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?

  2. emexes

    emexes Well-Known Member 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.
  3. Erel

    Erel Administrator Staff Member Licensed User

    Which problem are you trying to solve with this code?
  4. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    No particular problem. Just gaining more insight.

  5. Erel

    Erel Administrator Staff Member Licensed User

    Tracking memory usage can be confusing.
    Related tips:

    1. 95% of the memory issues are related to large images (large dimensions, the file size doesn't matter).
    2. If you are not experiencing memory errors then you probably don't need to worry about the memory usage.
  6. RB Smissaert

    RB Smissaert Well-Known Member Licensed 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.

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice