excessive RAM usage

Monkey Forums/Monkey Programming/excessive RAM usage

Duke87(Posted 2015) [#1]
Hi guys, i got just a few little questions.

i wrote a little GalleryView for myself.
1. My problem: When i Load the gallery with 10 Files (all about 3,6mb) it uses 71mb RAM,
by using 20 Files (6,7mb) its about 109mb, by 40 Files it uses like 400mb.
All files are .jpg.

2. it doesn't load all files. it's like my App Skip's some files. (is it a Fileproblem?)

Code: Mainfile
[CODE]
#rem
This Example shows how to use the GalleryViewer.
#end
Import mojo
Import maniac
#GLFW_USE_MINGW=True
#GLFW_WINDOW_TITLE="GalleryViewer Example"
#GLFW_WINDOW_WIDTH=1024
#GLFW_WINDOW_HEIGHT=800
#GLFW_WINDOW_SAMPLES=0
#GLFW_WINDOW_DECORATED=False
#GLFW_WINDOW_RESIZABLE=False
#GLFW_WINDOW_FULLSCREEN=false
#GLFW_SWAP_INTERVAL=-1
#OPENGL_GLES20_ENABLED=false
#OPENGL_DEPTH_BUFFER_ENABLED=false
#MOJO_AUTO_SUSPEND_ENABLED=True
#MOJO_IMAGE_FILTERING_ENABLED=True

#TEXT_FILES="*.txt|*.xml|*.json"
#IMAGE_FILES="*.png|*.jpg"
#SOUND_FILES="*.wav|*.ogg"
#MUSIC_FILES="*.wav|*.ogg"
#BINARY_FILES="*.bin|*.dat"

Function Main:Int()
New Example
Return 0
End


Class Example Extends App
Field gv:ManiacGallery

Method OnCreate:Int()
SetUpdateRate( 60 )
initManiac()
gv = New ManiacGallery(DW*0.1,DH*0.1,DW*0.8,DH*0.7,"monkey://data/GFX/Test/")
End Method

Method OnUpdate:Int()
maniacUpdate()
gv.Update()
End Method

Method OnRender:Int()
Cls
gv.Draw()
End Method

End Class
[/CODE]

ManiacGallery:
[CODEBOX]
Class ManiacGallery Implements IOnLoadImageComplete
Global loadedImages:Int = 0
Global l:Int = 0
Field arrImg:Image[500]
Field maniacID:Int
Field X:Float,Y:Float,Width:Float,Height:Float
Field ViewMode:Int = 1
Field dist:Int = 10
Field imgW:Float,imgH:Float
Field BtnRight:ManiacButton
Field BtnLeft:ManiacButton
Field BtnSwitchViewStyle:ManiacButton
Field ScrolledImages:Int = 0
Field tp:Int = 0
Field lastSwitch:Int
Field switchIntervall:Int = 500
Field Alpha:float

Method New(_X:Float,_Y:Float,_Width:Float,_Height:Float,_loadPath:String = "GFX/")
X = _X
Y = _Y
Width =_Width
Height = _Height
loadFromPath(_loadPath)
Local bW:Float = DW*0.15
Local bH:Float = DH*0.07
imgW = ll_Width(_Width,3,dist)
imgH = (_Height-bH)/2.5


BtnLeft = New ManiacButton(_X + bW/2 + _Width*0.02 ,_Y+_Height-bH/2, bW, bH," << ",ALIGNMENT_MIDDLE,COLOR_BLUE,True,1.5)
BtnRight = New ManiacButton(_X + _Width - bW/2 - _Width*0.02 ,_Y+_Height-bH/2, bW, bH," >> ",ALIGNMENT_MIDDLE,COLOR_BLUE,True,1.5)
BtnSwitchViewStyle = New ManiacButton(_X + _Width*0.5 ,_Y+_Height-bH/2, bW, bH," Switch ",ALIGNMENT_MIDDLE,COLOR_BLUE,True,1.5)


End Method

Method loadFromPath:Void(_Path:String="monkey://external/" )
Print "Get Folder"
Local arrStr:String[] = GetFolderFiles(_Path)
Print "Found: " + arrStr.Length() + " files"
l = arrStr.Length()
For Local ls:Int = 0 Until l
Print "load " + ls
LoadImageAsync(arrStr[ls], 1, 0, Self)
Next
End Method

Method Draw()
Drw_Rect(X,Y,Width,Height,3)
SetColor 255,255,255
SetAlpha 1
Select ViewMode
Case 0 '3x2 overview
Local i:Int = 0
For Local x:Int = 0 To 2
For Local y:Int = 0 To 1
Drw_Rect(X+dist+x*(imgW+dist) , Y+dist+y*(imgH+dist), imgW,imgH,2)
'Print "i: " + i
if arrImg[i+ScrolledImages] = Null
DrawText "No Image",X+dist+x*(imgW+dist) , Y+dist+y*(imgH+dist)
Else
DrawImage ( arrImg[i+ScrolledImages] , X+dist+x*(imgW+dist) , Y+dist+y*(imgH+dist),0, imgW/arrImg[i+ScrolledImages].Width(), imgH/arrImg[i+ScrolledImages].Height() )
Endif
i +=1
Next
Next

Case 1
If arrImg[tp] <> Null
SetAlpha 1
DrawImage arrImg[tp], X,Y,0,Width*1/arrImg[tp].Width(),Height*0.87/arrImg[tp].Height()
Endif
If arrImg[tp+1] <> Null
SetAlpha Alpha
DrawImage arrImg[tp+1], X,Y,0,Width*1/arrImg[tp+1].Width(),Height*0.87/arrImg[tp+1].Height()
Endif
End Select

BtnLeft.Draw()
BtnRight.Draw()
BtnSwitchViewStyle.Draw()

DrawText("Mode: " + ViewMode+ " | View: " + tp + "/"+loadedImages , X +Width*0.5,Y)
End Method

Method Update:Int()
If BtnLeft.Update() = 101 And gl_mousereleased
if ScrolledImages <= 0

Else
ScrolledImages -= 1
Endif
Endif

If BtnRight.Update() = 101 And gl_mousereleased
If (ScrolledImages + 6) >= loadedImages

else
ScrolledImages += 1
Endif
Endif

If BtnSwitchViewStyle.Update() = 101 And gl_mousereleased
If ViewMode = 0
ViewMode = 1
Else
ViewMode = 0
Endif
Endif

Select ViewMode
Case 0
Case 1
If Millisecs() - lastSwitch >= switchIntervall
If (tp + 1) >= loadedImages
tp = 0
else
tp +=1
Endif
Alpha = 0
lastSwitch = Millisecs()
Endif

Alpha += EqToFPS(1.0/(switchIntervall/1000.0))' 0.02
End Select
End Method

Method OnLoadImageComplete:Void(_image:Image, path:String, source:IAsyncEventSource)
If _image <> Null
arrImg[loadedImages] = _image
loadedImages += 1
Else
Print "no image : "+loadedImages
Endif

End
End Class
[/Codebox]
UpdateAsyncEvents() is called within the ManiacUpdate()

GetFolderFiles(_Path) returns all filenames within a folder. (even those, which are not loaded correctly << so this should not be the error).

is something wrong with loadFromPath:Void() ,or OnLoadImageComplete:Void() ?
Or is the massiv usage of RAM normal?


Midimaster(Posted 2015) [#2]
the RAM usage is "normal", because the compressed jpg files become uncompressed images. Maybe you really have a RAM problem.

I would prefer to scale down each jpg inside your app. After it is loaded grab it to a thumbnail and only keep this in memory.


EdzUp(Posted 2015) [#3]
All graphic files are converted to RGBA values so each pixel is four bytes in ram, this makes a simple image that looks like 19kb in file size blow up to massive memory usage when converted to gpu compatible images.

Also excessive ram usage will kill an app on mobile targets.


Duke87(Posted 2015) [#4]
thank you for your quick answers.
i got just one little question, how do i use GrabImage for rescaling(downsize) an Image?

i tried (for testing purpose)
origImg = LoadImage("monkey://data/GFX/Test/060124_174356.jpg")
nImg = origImg.GrabImage( 0, 0, origImg.Width(),origImg.Height(),1,0 )

how do i downsize nImg?