[Python] reloading script with images increases memory usage

Hi guys!

Im just curious if anyone has noticed (or knows a solution to) how when embedding an image with cmds.image() or cmds.iconTextButton() (or the MEL counterparts), this increases mayas memory usage of Maya?

Reloading the script doesnt seem to flush the memory automatically, but instead continually increases mayas memory usage on every reload. Is there’s a way to detect when a script is being reloaded to do this?

Basically im going through and displaying images within a folder with the use of iconTextButton(), these are only low-res, but the memory increases is fairly high (50-100MB on each reload). Having a look at more popular, public scripts and plugins, a lot seem to have the same issue - but usually with a smaller footprint due to being small icons, etc.

Hopefully this post makes sense - I would really appreciate any suggestions on fixing this! :slight_smile:

Something light this might work


from maya import cmds
try:
    for image in _images:
        cmds.deleteUI(image)
except NameError:
    pass
finally:
   _images = []


# all your wacky GUI code
some_image = cmds.image(path_to_some_image)
_images.append(some_image)

To break down how this works. The first time you run the script, _images won’t exist yet, which will trigger the NameError. But whenever you reload the module, _images will exist, and any widges in that array will be deleted. This is because reload doesn’t actually reset a module, it just re-runs all the code in the modules current namespace.

No promises on this actually fixing the memory leak though, I haven’t tested that part…

Thanks a lot R.White for the reply :slight_smile:

So what you’ve pointed out I’ve actually tried playing with previously as well. Unfortunately I can’t quite share the code I’ve got, but I’ve made another example using your suggestions here:


from maya import cmds

def UI():
	if(cmds.window("JMSMain", exists=True)):
		cmds.deleteUI("JMSMain")
	try:
		for image in _images:
			cmds.deleteUI(image)
	except NameError:
		pass
	finally:
		_images = []
	mainWin = cmds.window("JMSMain", title="Test", sizeable=False, tlb=False, menuBar=True, mnb=True)
	cmds.columnLayout()
	albedo = cmds.image(image="E:/4K_Albedo.jpg")
	_images.append(albedo)
	cmds.showWindow(mainWin)
UI()

The image is a 4k texture, sits at around 1.5MB is size. Below is an image of Mayas memory usage after reloading the script 3 times.


So…yeah, haha.

So it won’t work in a function, as the functions namespace gets recreated each time you call it.
You’d need to leave _images at module level.
But it’s very possible this is just Maya being crappy with releasing image memory.
I’ve seen a similar problem with PSDs in File nodes, each time you reload you get a bit more memory that never seems to get freed. This gets real bad if you’ve got the auto-refresh option enabled from the new modeling tools.

Hey again,

I thought I actually replied to this comment, I came back to let you know that I’ve worked out what the cause is (However, not on how to fix it)

Basically my script is going through folders and pulling out any image with “Preview” in the name - and is then applying the image to a button to be used for calling a function.

Anyway, decided to take another dig at finding out the cause - and I’ve noticed this:

Once the amount of buttons I have, with an image attached goes over 24 - I get this strange memory leak above, where reloading the script increases the memory used by maya. I’ve tried the technique above amongst other things - but no matter what Ive tried, nothing works.

Upon loading a script with more than 24 buttons increases memory usage significantly right off that bat. This is a shame, since I need to load many at the same time.

Hey,

Just wanted to confirm i had this during GUI dev too. Didnt really resolve it (it didnt burn outside dev iters, though i can see yours would)… it may have been due to the image type however (svg i reckon). Anyway, just a note to share you weren’t in isolation.