Real Software Forums
http://forums.realsoftware.com/

How do I clear the memory leak when loading pic in Loop
http://forums.realsoftware.com/viewtopic.php?f=1&t=43478
Page 1 of 2

Author:  eddy2099 [ Wed Apr 04, 2012 9:42 pm ]
Post subject:  How do I clear the memory leak when loading pic in Loop

This is a subset of my codes basically I narrowed down to this

dim noofpic as integer
dim picfile as folderitem
noofpic = listbox1.listcount-1
dim p as picture

label1.text = "Enjoy the Show"
for y as integer = 0 to noofpic

p = new picture(324,286,32)

picfile = getfolderitem(listbox1.cell(y,0),folderitem.PathTypeShell)

p = picture.Open(picfile)
imagewell1.image = p
imagewell1.Invalidate

for wait as integer = 0 to 10000
// wait
next

imagewell1.image = nil

next

label1.Text = "Done"


Basically, what I am just doing here is looping through the photos from a Listbox and showing it in an ImageWell, actually it does not matter if I load it to the imagewell or not. What happens is that the under Activity Monitor, the Real Memory Usage just goes and grows even after the loop is complete, it is still not released. When I start the loop again, the memory usage grows.

I try looking around but do not see anywhere I could use to free this memory. In my actual app, the memory can swell to over 1.6gb and then it exhibits weird behavior like global variables starts to be set to nil.

Is this a bug or I am missing up somewhere on memory management ?

I only tested in Cocoa and Carbon and both shows the same issues in Real Studio 2011 R4.3

Thanks for any help.

Author:  DaveS [ Thu Apr 05, 2012 12:04 am ]
Post subject:  Re: How do I clear the memory leak when loading pic in Loop

Not positive here... but I don't think you need the line that says

p = new picture(324,286,32)

This allocates a buffer area for a picture.... but then the next line

p = picture.Open(pic file)

Allocates the one the picture actually goes in.... and since you never set the first one to NIL... it stays allocated ... until it goes out of scope.. which isn't until the end of your procedure....

I might even try this instead

imagewell1.image = picture.Open(pic file)

and remove ALL references to "p"

Author:  timhare [ Thu Apr 05, 2012 12:07 am ]
Post subject:  Re: How do I clear the memory leak when loading pic in Loop

Sounds like a bug. In fact, I think there was one reported recently. Check Feedback.

p = picture.Open(pic file)

This *should* destroy the previously allocated picture object, so while
p = new picture(324,286,32)

is extraneous, it shouldn't cause any memory issues. If you remove it, you will leak memory more slowly, but it shouldn't be leaking at all.

Author:  charonn0 [ Thu Apr 05, 2012 12:32 am ]
Post subject:  Re: How do I clear the memory leak when loading pic in Loop

timhare wrote:
p = picture.Open(pic file)

This *should* destroy the previously allocated picture object

I've done precisely this quite a lot and it is destroyed.

Author:  eddy2099 [ Thu Apr 05, 2012 12:59 am ]
Post subject:  Re: How do I clear the memory leak when loading pic in Loop

I've modified the codes to

dim noofpic as integer
dim picfile as folderitem
noofpic = listbox1.listcount-1
dim p as picture

for y as integer = 0 to noofpic

picfile = getfolderitem(listbox1.cell(y,0),folderitem.PathTypeShell)
p = picture.open(picfile)
imagewell1.image = p
imagewell1.Invalidate
p = nil

for wait as integer = 0 to 10000
// wait
next

imagewell1.image = nil

next


I modified the codes to the above and it is still leaking. I tried without using p but for some weird reason, the app crashed. I think it was to do with the color space or something to that nature.

The leak seems to be pretty much the same. It looks like 'nil' doesn't seems to do anything. Only when the entire thread ends that the memory goes back to the pre-execution stage.

Apart from allocating 'nil' is there anything which I should be doing to free the memory ?

Author:  timhare [ Thu Apr 05, 2012 1:29 am ]
Post subject:  Re: How do I clear the memory leak when loading pic in Loop

No. You shouldn't even need to set it to nil.

Author:  eddy2099 [ Thu Apr 05, 2012 2:31 am ]
Post subject:  Re: How do I clear the memory leak when loading pic in Loop

Thanks. I posted this codes to Feedback 18572. I guess there is nothing much I could do apart from waiting for RS to plug the leak.

Author:  DaveS [ Thu Apr 05, 2012 8:34 am ]
Post subject:  Re: How do I clear the memory leak when loading pic in Loop

Another guess here.......
but try putting the DIM statement INSIDE the loop...

Author:  eddy2099 [ Thu Apr 05, 2012 4:33 pm ]
Post subject:  Re: How do I clear the memory leak when loading pic in Loop

DaveS wrote:
Another guess here.......
but try putting the DIM statement INSIDE the loop...


Thanks. I tried the following

dim noofpic as integer

noofpic = listbox1.listcount-1


for y as integer = 0 to noofpic
dim p as picture
dim picfile as folderitem
picfile = getfolderitem(listbox1.cell(y,0),folderitem.PathTypeShell)
p = picture.open(picfile)
imagewell1.image = p
imagewell1.Invalidate
p = nil

for wait as integer = 0 to 10000
// wait
next

imagewell1.image = nil

next


But it does not seems to do anything to reduce the memory. It did however clear when the thread ended. I guess it is probably a real memory leak.

Author:  kermit [ Fri Apr 06, 2012 3:50 am ]
Post subject:  Re: How do I clear the memory leak when loading pic in Loop

Technically, its only a leak if the memory doesn't come back.
Since you reckon it does when the process ends, maybe its a good idea to split this into chunks.

First, add a timer to the window, set to fire every few seconds.
Add an integer property (called lstPlace) to the window, to record 'place in the listbox'

Now, in the timer's event, do this:

if lstPlace < listbox1.listcount-1 then
lstPlace +1
end if
imagewell1.image = picture.open(getfolderitem(listbox1.cell( lstPlace,0), folderitem.PathTypeShell))
imagewell1.Invalidate


So there are no intermediary images: just the one 'owned' by the image well
And everything goes out of scope at the end of the timer event, so garbage collection should kick in.

Author:  goldmagnet [ Fri Apr 06, 2012 4:05 am ]
Post subject:  Re: How do I clear the memory leak when loading pic in Loop

Hello

Image handling in RB is pretty broken, the from data method leaks too (but not in carbon).

viewtopic.php?f=7&t=41225&p=229353#p229353

Goldmagnet

Author:  eddy2099 [ Fri Apr 06, 2012 5:25 am ]
Post subject:  Re: How do I clear the memory leak when loading pic in Loop

I actually did experiment with breaking up the list into several chunks with each for next loop handling not more than 50 images. But the memory was not release until the last loop is completed. I used the sleepmbs in between those loops, thinking that the system with have some time to do its memory management.

Actually the whole process I wanted to achieve were to resize the images and adding some effects from Einhugur to the images before saving them. Whether I display the images or not, it does not actually make a difference to the memory usage.

Author:  timhare [ Fri Apr 06, 2012 2:02 pm ]
Post subject:  Re: How do I clear the memory leak when loading pic in Loop

eddy2099 wrote:
I actually did experiment with breaking up the list into several chunks with each for next loop handling not more than 50 images. But the memory was not release until the last loop is completed. I used the sleepmbs in between those loops, thinking that the system with have some time to do its memory management.

But that really doesn't separate them if they're still in the same method chain. Try the timer idea, that creates true separation.

Author:  eddy2099 [ Fri Apr 06, 2012 5:15 pm ]
Post subject:  Re: How do I clear the memory leak when loading pic in Loop

timhare wrote:
But that really doesn't separate them if they're still in the same method chain. Try the timer idea, that creates true separation.


Thanks.

I tried these

Window1 Control Timer1:
Sub Action()
if nowpic < noofpic+1 then
dim p as picture
dim picfile as folderitem
picfile = getfolderitem(listbox1.cell(nowpic,0),folderitem.PathTypeShell) p = picture.open(picfile)
imagewell1.image = p
imagewell1.Invalidate
nowpic = nowpic+1
timer1.enabled = true
else
timer1.enabled = false
end if
End Sub


But the memory usage till keeps going up.

Author:  lajdesign [ Sat Jun 01, 2013 4:18 pm ]
Post subject:  Re: How do I clear the memory leak when loading pic in Loop

Did you ever find a solution to this? I am running into the same thing with 12 r2.1 when using cocoa. Doesn't release the memory like it should even if you nil it out.

Page 1 of 2 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/