Android Question out of memory error

mterveen

Member
Licensed User
Longtime User
I need some guidance on trying to resolve my out of memory error issue.

I have a gauge class that creates a "gauge" using 5 transparent panels, each panel the same size and "stacked" on top of the previous to create layers that hold unique info for each gauge (e.g. frame, ticks, numbers, etc). each panel has a canvas used to draw on that panel. these panels are added to a base panel to create the total gauge display (i.e. one unique gauge). so basically 6 panels per gauge.

in the activity to display the gauges there is a main panel using a scrollview2d. on this main panel i add gauges in this type of fashion. so the scrollview can hold several gauges at once.


B4X:
sub displaylayout
  mainpanel.removeallviews
  dim gauge1,gauge2,gauge3 as clsgauge
  gauge1.type = "digital"
  gauge2.type = "analog"
  gauge3.type = "digital"
  mainpanel.addview(gauge1, x, y, w, h)  etc etc
  mainpanel.addview(gauge2, x, y, w, h)  etc etc
  mainpanel.addview(gauge3, x, y, w, h)  .....
end sub

(so in this instance i would have a scrollview with 3 gauges on it, with each gauge utilizing 6 panels and 5 canvases.)

the sub displaylayout can be called multiple times based on a spinner from which the user can display their layout preference.

i have no problem displaying the layout the first time. however, based on the complexity of the layout (and corresponding memory required) i often have OOM errors when trying to display a new layout.

so is there a better way to minimize memory? does each panel require its own canvas or should i use a global canvas and just assign it to a panel when i need to update the specific panel layer. at what point in time is the lion's share of memory consumed, e.g. when the canvas is initialized to a panel?
 

mterveen

Member
Licensed User
Longtime User
so i changed it to use a global canvas that is initialized to the appropriate panel layer (0 - 3 - reduced one panel) when a draw to the panel is required. still having memory errors.

question i have is -> if the panel if 500 x 500, is the memory consumed when the panel is first added to the activity? OR is it consumed when i initialize the canvas with this panel and the bitmap is created to be drawn on? does the program consume 1,000,000 bytes (500x500x4)? so if i have 4 panels for each gauge then size i assume then that each gauge will take 4,000,000 bytes?
 
Upvote 0

mterveen

Member
Licensed User
Longtime User
not loading a bitmap directly. i have a global canvas that i initialize with a panel when i need to draw on the panel. each gauge is setup with panels as follows:

panel0 -> frame, face, tick marks/numbers, etc
panel1 -> min / max values that change infrequently as new data is acquired
panel2 -> updates needle or numeric value depending on gauge type
panel3 -> turns red if alarm point is reached - may not be used at all if gauge alarm is not turned on.

so when i need to draw to the panel i use:

cvs.initialize(panel0)

then i use the various canvas draw commands, e.g. drawoval, drawline, drawtext, etc.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Each time you initialize a canvas on a panel you create a new bitmap.
In this case, for each gauge, you have 4 panels i.e. 4 bitmaps.
Maybe if you did all of the work on a single panel you can reduce your memory footprint 4 times.

Considering at the moment one gauge is taking 4 MB. Depending on the device you might be limited to 4, 8 or 16 gauges.
 
Upvote 0

mterveen

Member
Licensed User
Longtime User
initially it seemed the best and easiest way to program this. however it is looking like redrawing everything each time the gauge is updated may be the only solution. or at least get it down to just 2 panels. bummer. hopefully a full redraw won't bog the cpu too much.

thx for everyone's input.
 
Upvote 0
Top