Android Question OutOfMemoryError

Discussion in 'Android Questions' started by fasilosman, Jul 29, 2015.

  1. fasilosman

    fasilosman Member Licensed User

    I get the Out of memory error occurs randomly in my all activity.
    My all activity have the ahaActionBar and it has a bitmap.
    Most of the time it works and the error occurs randomly in different different activities and I have find the exact reason.

    Please help me in this regard

     
  2. sorex

    sorex Expert Licensed User

    how many images and what are their dimensions?

    do you reduce the size to what it is on screen to gain memory?
     
  3. fasilosman

    fasilosman Member Licensed User

    only one image is used. it is in .png format, 128 * 128 dimension.

    But the thing is most if the time is all are working fine. But in some instance it got and unfortunately stops error (out of memory).
     
  4. klaus

    klaus Expert Licensed User

    How often are you loading the image ?
     
  5. fasilosman

    fasilosman Member Licensed User

    The related code is
    Code:
    Sub Activity_Create(FirstTime As Boolean)

       
    AppIcon.Initialize(
    File.DirAssets,"icon.png")
    bmp.Initialize(
    File.DirAssets,"back1.png")
    AB.Initialize(
    Activity,Null   ,Me,"ActionBar1")
    AB.ABHeight = 
    48dip
    AB.ABIcon = AppIcon
    AB.AbTitle  = 
    " INVOICE"
    'AB.CreateNavigationDrawer("Main",Null)
    AB.ABBackColor  = Colors.RGB(122226255)
    AB.AddMenuItem(
    "Version" ,BlnkBmp ,"Version")
    'AB.AddNavigationDrawerItem("NDrawer" ,AppIcon,"NDrawer")
    AB.AddActionButton ("Back",bmp,"Back")
    AB.MenuItemBackColor = 
    Colors.RGB(122226255)

    SQL1.Initialize(DBFileDir, DBFileName, 
    False)
     
  6. klaus

    klaus Expert Licensed User

    What size in pixels is back1.png ?
    I would suggest, as a first step, to declare bmp as a Process_Global variable and initialize it with
    Code:
    If FirstTime Then    
    bmp.Initialize(
    File.DirAssets,"back1.png")
    End If
    Every time you turn the device or use another Activity and come back you define a new bitmap.
     
  7. fasilosman

    fasilosman Member Licensed User

    The Pixel size of Back1 is 140 * 94.
    And I use 'Project Attributes'
    #SupportedOrientationsPortrait
    so it can't be rotated.

    I use the above bitmap code in every activity.
     
  8. klaus

    klaus Expert Licensed User

    Have you tryed my suggestion ?
     
    RandomCoder likes this.
  9. fasilosman

    fasilosman Member Licensed User

    I am in that process. i will let you know.
    Thank you
     
  10. fasilosman

    fasilosman Member Licensed User

    Still I got the error message in different activity
    In that code I didn't use the back1.png
    I used only icon.png
    as above


     
  11. fasilosman

    fasilosman Member Licensed User

    when i opened/ load activities which have the bitmap for 9 times it gets out of memory.

    how can I reuse the bitmap I loaded/initialized in several activities. I have 15 activity all have actionbar with bitmap of 48dip
     
  12. DonManfred

    DonManfred Expert Licensed User

    Every time the same icon?

    In main Process globals define a bitmap (dim bmp as Bitmap)
    In main activity_create firttime = true you load the bitmap to the global var
    in all activities you use then this global loaded bitmap instead of loading a new one each time
     
    RandomCoder likes this.
  13. fasilosman

    fasilosman Member Licensed User


    I define in the main process globals - dim bmp as Bitmap
    in activity_create firsttime sub I did---
    If FirstTime Then
    bmp.Initialize(File.DirAssets,"icon.png")
    End If​

    then In activity1 or activity2 or ect....

    when I use the "AB.ABIcon = bmp" , error message says
    "Undeclared variable 'bmp' is used before it was assigned any value"

    what I have done wrong I can get it.
     
  14. DonManfred

    DonManfred Expert Licensed User

    i think it should be main.bmp instead of just bmp... The variable is declared in main. So you should prefix the variable with the activityname
     
    RandomCoder and Peter Simpson like this.
  15. fasilosman

    fasilosman Member Licensed User

    DonManfred: You are correct. It should be main.bmp.
    But still the same error.

    I did like this.

    I declared and initialized the bmp in the main activity
    and I used it in the main activity as well as in the activity1 also as main.bmp

    Then
    I open main activity then from main to activity1 then
    from activity1 to main then main to activity1 and so on.....
    when the activity(either main or activity1) is loaded at 9th time the out of memory occurs.

    now what might be the wrong..
     
  16. klaus

    klaus Expert Licensed User

    Can you post the project as a zip file.
     
  17. fasilosman

    fasilosman Member Licensed User

    I made a new sample project as I attached.
    when I load it main to activity and activity to main for approximately 50 times it get out of memory error.
    (may be it is irritating to do 50 times since my image size is small. sorry for inconvenient. Hope you will understand. Because I can upload the project with large image file. Max uplaod is 512 kb)
     

    Attached Files:

    • Bmp.zip
      File size:
      501.1 KB
      Views:
      7
    Last edited: Jul 29, 2015
  18. DonManfred

    DonManfred Expert Licensed User

    works fine here....
     
  19. fasilosman

    fasilosman Member Licensed User

    I tried twice

     
  20. fasilosman

    fasilosman Member Licensed User

    Now also I tried. you may try with large bitmap file to generate it quickly
     
Loading...