PDA

View Full Version : <<Fix for memory leak>>> every dev read


whise
August 24th, 2007, 09:40 PM
the title says it all ... after alot of testing i found out that the text drawing using ctx causes a memory because the ctx memory isnt cleared , we got to fix it
you will only ontice it on screenlets that update very quickly
here is why is appends

traditionaly python has problems freeing memory it uses because it normally store vars forever , here's what appends in the text memory leak
in def on_draw
p_layout = ctx.create_layout()

in here python stores the p_layout , the next time it redraws it stores it again and again and again , thats why we get a memory leak

how do we fix it , simple

first provide a global var

class NetmonitorScreenlet(screenlets.Screenlet):

p_layout = None

then in on_draw

if self.p_layout == None :

self.p_layout = ctx.create_layout()
else:

ctx.update_layout(self.p_layout)

thats it , now we just update the var instead of creating a new one ;)

whise
August 25th, 2007, 03:33 AM
i think everyone should update their screenlets like this ...

James
August 25th, 2007, 04:16 AM
My screenlet was having this exact memory problem.

I've tried your fix and I can confirm it does work.

Good work on the find!

-- James

cbudden
August 26th, 2007, 03:25 PM
Is there a list of screenlets that have been updated to include this fix?

Chris

whise
August 26th, 2007, 04:23 PM
actually none except mine , i guess people will start to use this now , and also you wont really notice it unless the screenlet updates really frequently

RYX
August 27th, 2007, 10:14 PM
Thanks for tracing it down, whise - good work!

You can also create the PangoLayout inside the constructor (be getting it from self.window.window). I already did the same for the ClockScreenlet before the last release, I guess that's why the Clock doesn't have the leak ...

How about another attribute to the constructor to (optionally) automatically create self.p_layout? Would remove the need for creating the layout in all screenlets.

:)

whise
August 27th, 2007, 10:30 PM
i guess that would be a good idea , would improve the development easiness and would also stop people from making that error , also i hope it wont break backwards compatibility

whise
September 7th, 2007, 10:02 PM
could this be a sticky please

jsf
October 11th, 2007, 12:58 AM
Thanks immensely for this tip, whise. I was wondering where all the memory was going and dreading tracking it down. This information needs to be un-missable for developers of 3rd party screenlets. What about just adding this to the main "Infos for writing/releasing own screenlets" thread? Or even to the documentation section on the screenlets.org website?

whise
October 11th, 2007, 01:09 AM
thats what i think too

nomego
October 11th, 2007, 09:30 PM
Isn't it just enough to do a p_layout = None after ctx.restore() or even ctx.show_layout() ?

davim
October 11th, 2007, 10:26 PM
I think not because you wont be freeing the memory, just pointing to another place....

nomego
October 12th, 2007, 08:09 AM
Could anyone verify I made it right in my Terminals screenlet (http://screenlets.org/index.php/Terminals) so I know that I'll modify my other screenlets correctly?

whise
October 12th, 2007, 12:46 PM
i checked your code its correct , RYX already knows about this issue , im thinking a Draw_text function could be integrated into the core so it could be used by developers an not mislead them

nomego
October 12th, 2007, 02:10 PM
Ok I will update my other screenlets accordingly.

taril
February 5th, 2008, 09:10 PM
This is not enough!

You have to search for all the p_layout values in the file, and you have to insert before it the "SELF.", so it has to look like this:

self.p_layout

aantn
March 26th, 2008, 01:29 PM
Whise, there seems to be a memory leak in the functions theme.get_text_width and theme.get_text_extents.

I'm going to fix it in my branch, and you should do so in trunk.

As a side note, I'll give you advance warning that I'm making a bunch of the theme methods into static methods. The advantage is that they can now be used by classes that don't have their own theme. (Right now, that's only applicable in my branch.)

i was thinking about doing that for some time now ... thanks

aantn
March 26th, 2008, 03:13 PM
Actually, I don't think it's really a memory leak. It's not the most efficient way of handling things, but it shouldn't cause leakage.

I'll add on an optional parameter in case the pango layout was already created. It should help in many cases.

whise
March 26th, 2008, 04:38 PM
thanks i didnt notice it,
about the themes , i wass thinking about that for a long time , good job

Ashy
March 30th, 2008, 11:14 PM
Yes, move those methods, silly having them in theme and has caused me a few headaches :)

aantn
April 6th, 2008, 04:04 PM
I'm running into some annoyances by keeping them in the Screenlet class.

Do you have any objection to make them module functions, or at the very least static class functions?

whise
April 6th, 2008, 06:05 PM
do you mean keeping them on the screenlettheme class?

i dont mind , but we must think of a better solution , maybe rethink the whole screenlettheme class

aantn
April 13th, 2008, 09:24 AM
You're right. Perhaps it's time to merge SreenletTheme into Screenlet and move the drawing functions into their own module so that they can be shared and used in other places.

aantn
April 13th, 2008, 05:32 PM
Ignore my last post. After the conversation about global themes, I've changed my mind. Let's leave the ScreenletTheme class in a separate module.

mrthefter
April 17th, 2008, 03:46 AM
The countdown screenlet still appears to have this problem.

aantn
April 17th, 2008, 07:04 AM
I'll give it a look later.

I haven't quite gotten rid of the ScreenletTheme class, but I've rewritten most of the theming code so that themes can now be used outside of Screenlets. It's past time to take over the desktop. :-D