Android Question Validation needed on OOM issues

Himred

Member
Licensed User
Longtime User
Hi guys (and ladies)

I'm facing Out of memory issues on my first game app and i'm having hard time fixing it.
As I dont allocate anything excepted views, this must come form the grahical part of the App.
The OOM occurs after somehow long time of playing (30 mins+), for me it's when the user plays a lot of levels.

Let me explain what I do in my code and if someone expert can validate me it is the right (or not) way that would help a lot.

When the app start the first time (and only the first time), I load 14 small 128x128 bitmaps using LoadBitmapSample.

Then I call CreateLevel which create the interface for a given level
The function initialize between 16 and 144 labels (depending on level size) and set the background image of each label with one of the loaded bitmaps.

When a user click on a label, I only change the background image of the touched label.
When the user wins I call DestroyLevel which removeviews all labels
Then I run CreateLevel again for the next level.

Here are some extract of the code:

B4X:
Sub Activity_Create(FirstTime As Boolean)
         
    If FirstTime Then
        ........
        LoadIcons()
        CreateLevel(LastWorldPlayed,LastLevelPlayed)
    Else
        CreateLevel(curworld,curlvl)
    End If
End Sub

Sub LoadIcons
img0=LoadBitmapSample(File.DirAssets,themes.Get("theme"&selected_theme&"-0"),128,128)
img1=LoadBitmapSample(File.DirAssets,themes.Get("theme"&selected_theme&"-1"),128,128)
img2=LoadBitmapSample(File.DirAssets,themes.Get("theme"&selected_theme&"-2"),128,128)
End Sub  

Sub CreateLevel(world as int, lvl as int)

......
......
For x=0 To sizeX-1
        For y=0 To sizeY-1  
            labels(x,y).Initialize("Label_Event")
            pnl.AddView(labels(x,y), BlockX*x, BlockY*y, BlockX-margin, BlockY-margin)
            UpdateLabelBackground(x,y)
        Next
    Next
....
....
End Sub

Sub UpdateLabelBackground (x As Int,y As Int)
    If state(x,y)=0 Then labels(x,y).SetBackgroundImage(img0)
    If state(x,y)=1 Then labels(x,y).SetBackgroundImage(img1)
    If state(x,y)=2 Then labels(x,y).SetBackgroundImage(img2)
End Sub

Sub DestroyLevel

Dim GCollector As Reflector
    
    'MAXS is the biggest level I can have ie 12x12.
   
    For x=0 To MAXS-1
        For y=0 To MAXS-1
            If labels(x,y).IsInitialized Then
                labels(x,y).SetBackgroundImage(Null)
                labels(x,y).RemoveView
                End If
        Next
    Next
     
    GCollector.RunStaticMethod("java.lang.System", "gc", Null, Null)
End Sub

Is there something I forgot to do ?
Is remove view is enough to free the labels in DestroyLevel ?

Thanks for any help you could provide.

Regards,
 
Last edited:

Himred

Member
Licensed User
Longtime User
I have the feeling that I'm running initialize method on views in the wrong place.
Should I run the initialize only on first time in place of each time a start a new level ?
 
Upvote 0
Top