Out of Memory

Djembefola

Active Member
Licensed User
Longtime User
At times i get an "Out of memory"-Exception, when i restart my application.

I can start my program and work with it, but when i finish it with the Back Button and then try to restart it, it crashes.

The second attempt to restart it (after the Crash) is always succesful.

The exception happens only on a tablet with 1GB RAM. On devices with smaller screens i never had this problem.

Is there any way to catch an Out of Memory-Exception?
 

ondesic

Active Member
Licensed User
Longtime User
Correct, that's why I posted here. I don't know how to declare the panels and canvases to reuse them, seeing as the seem to stay in memory even after I close the app
 
Last edited:
Upvote 0

ondesic

Active Member
Licensed User
Longtime User
Agraham, I posted before I was done with my question. Please reread my last question.

I really did read this thread, I probably just don't understand it as well as you. Sorry if I sounded snippy.
 
Upvote 0

ondesic

Active Member
Licensed User
Longtime User
I tried the code by exectuting it when the BackKey was pressed:

B4X:
Dim Obj1 As Reflector
Obj1.Target = cnvMap
Obj1.Target = Obj1.GetField("bw")
Obj1.Target = Obj1.RunMethod("getObject")
Obj1.RunMethod("recycle")

However, on the second execution of the app, I get the same memory error in the log:(
 
Upvote 0

ondesic

Active Member
Licensed User
Longtime User
I am trying a couple of different things with no luck. Next I am trying what was suggested with the process globals.

Once I place my mutable bitmaps in the process globals, how do I reuse them on the next execution?

Do I still initialize the bitmap with: bmp.InitializeMutable(1000,1400)?
Do I still call canvas.Initialize2(bmp)? Or is that wrong?

EDIT: figured it out. YES to canvas.Init2 but NO to bitmap.Init
 
Last edited:
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
It might be the classic "large native allocated bitmap is wrapped by a small managed bitmap wrapper and so the VM garbage collector is not collecting the wrapper because it doesn't think it's out of memory yet while the native heap is exhausted" problem. You could try explicitly freeing up the bitmaps and see if that helps.

My PageTurnView library has a RecycleBitmap function you could use to try freeing a bitmap when you know you don't need it or it's trivial to do it with my Reflection library.
B4X:
Dim Obj1 As Reflector
Obj1.Target = bmp ' bmp is the unwanted Bitmap
Obj1.RunMethod("recycle")

You can get the bitmap from a canvas but note that if the canvas is initialised on a view you will need to give that view another drawable or initialize a canvas on it before it is drawn again or you will get a "Canvas: trying to use a recycled bitmap " exception and your app will force close.
B4X:
Obj1.Target = canv
Obj1.Target = Obj1.GetField("bw")
Obj1.Target = Obj1.RunMethod("getObject")
Obj1.RunMethod("recycle")

Excellent! This has brought down the memory usage of my imageviews. Is it possible to do this on bitmaps within a listview as well?
 
Upvote 0

mcmanu

Active Member
Licensed User
Longtime User
Hi

Hi,
Have a Problem, i have to put exitapplication in activity_pause because i have to free ram. Now i need the recycle method too to freeing bitmaps in webview.
But if i use recycle, exit application wont work
Does anybody knows why?

-->

B4X:
Sub Activity_Pause (UserClosed As Boolean)




If UserClosed=True Then
TimerMinute.Enabled=False
TimerSecond.Enabled=False


webchart.LoadUrl("")
webchart.RemoveView
If  File.Exists(File.DirInternal,"note.ini")=False  Then
 Dim o3 As Reflector
o3.Target = webchart
o3.RunMethod2("recycle","True","java.lang.boolean")
ExitApplication
else
image1.SetBackgroundImage(Null)
image2.SetBackgroundImage(Null)

end if

I have to use 'If File.Exists(File.DirInternal,"note.ini")=False Then' because when i only use exitapplication my service doesnt work.

The problem is when i use webview and load a bitmap into it and start my app again it chrashes :-(

Hope somebody can help me out :)
 
Last edited:
Upvote 0

FJS

Active Member
Licensed User
Longtime User
It might be the classic "large native allocated bitmap is wrapped by a small managed bitmap wrapper and so the VM garbage collector is not collecting the wrapper because it doesn't think it's out of memory yet while the native heap is exhausted" problem. You could try explicitly freeing up the bitmaps and see if that helps.

My PageTurnView library has a RecycleBitmap function you could use to try freeing a bitmap when you know you don't need it or it's trivial to do it with my Reflection library.
B4X:
Dim Obj1 As Reflector
Obj1.Target = bmp ' bmp is the unwanted Bitmap
Obj1.RunMethod("recycle")

You can get the bitmap from a canvas but note that if the canvas is initialised on a view you will need to give that view another drawable or initialize a canvas on it before it is drawn again or you will get a "Canvas: trying to use a recycled bitmap " exception and your app will force close.
B4X:
Obj1.Target = canv
Obj1.Target = Obj1.GetField("bw")
Obj1.Target = Obj1.RunMethod("getObject")
Obj1.RunMethod("recycle")



Good afternoon,

I have a app with gv. and acsf. and I would like to know how to erase the information of this bitmap,

maybe someone like
obj1.target= gv.bitmapdata
obj1.runmethod("recycle")

Thank you for your help

Best regards
 
Upvote 0
Top