Gimp-Forum.net
Gimp Script Memory Problem Question - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: Gimp Script Memory Problem Question (/Thread-Gimp-Script-Memory-Problem-Question)

Pages: 1 2


Gimp Script Memory Problem Question - KoalaBear2022 - 12-18-2021

Hello,

I am producing a batch of images with Python/Gimp Scriptfu

But it seems on every round of the loop I am accumulating unnecessary Memory. Eventually this leads to a Memory Error. 

It looks like pdb.gimp_image_delete(image) is not working. It should return True on success but that isn't the case.

Anyone experienced similar issues or knows whats wrong? I am using Gimp 2.1 on MacOS

Or is there another way to simply clear the memory cache of GIMP after each round of the loop or whenever needed?

Thanks a lot!w_

Timage)


RE: Gimp Script Memory Problem Question - Ofnuts - 12-19-2021

Can you show the actual code that loads the image, and the code that deletes it?

You can also check if according to Gimp the length of the image list (obtained by gimp.image_list() isn't increasing indefinitely).

"It should return True on success but that isn't the case." I don't see this documented in the procedure browser... For me it doesn't return anything.



Tried this:
Code:
for i in range(10):
    image=gimp.Image(30000,30000,RGB)
    layer=gimp.Layer(image,"Filler",10000,10000,RGB_IMAGE,100,NORMAL_MODE)
    image.add_layer(layer,0)
    pdb.gimp_drawable_fill(layer,FILL_PATTERN)

for image in gimp.image_list():
    print image.layers

# A bit later

for image in gimp.image_list():
    gimp.delete(image)
With my system monitor running, I definitely see a memory increase, and a final sharp drop:

[attachment=7246]

While doing this, I found that the Image list dockable dialog displays the image thumbnails, even if they have no associated Display, so perhaps just by opening this dialog you can check if your images are adequately removed (I can see the images appear in the display one by one, and then all disappear at the end).


RE: Gimp Script Memory Problem Question - KoalaBear2022 - 12-19-2021

Thanks a lot for that quick reply! Didn't expect that.
So I tried that.
gimp.image_list():
Seems its working fine because it returns always
Image List: (1, (1,)) and not increasing. Not sure what 1, 1 is for if this array is empty.

I am now testing for another Bug in my Python Code which is quite big. Maybe a different Array not related to GIMP is was causing this error inside of the loop and causing this huge memory increase from 3GB to 60GB.

Thanks a lot this function really helped to test for it.
I will let you know once I am able to fix it.


RE: Gimp Script Memory Problem Question - Ofnuts - 12-19-2021

(12-19-2021, 02:38 PM)KoalaBear2022 Wrote: Thanks a lot for that quick reply! Didn't expect that.
So I tried that.
gimp.image_list():
Seems its working fine because it returns always
Image List: (1, (1,)) and not increasing. Not sure what 1, 1 is for if this array is empty.

You ask in Python and you get an answer in Scheme? Looks like a list of one image where the first image ID is 1.

By the way the Python script runs in its own process you can tell in your System monitor is the RAM is used by  your Python script or by Gimp itself.


RE: Gimp Script Memory Problem Question - Ofnuts - 12-19-2021

Did this with a 1690x2225px images and 10K iterations, and I can just see a small 300MB increase since I started (and since I have other apps running, it may not be due to Gimp)


RE: Gimp Script Memory Problem Question - KoalaBear2022 - 12-20-2021

I am getting still the same result. I even disabled/enabled the Undo with image.disable_undo()/ image.enable_undo() which could possibly reduce memory consumption.

Are you modifying/processing your image at each iteration?

I am guessing GIMP is saving some Data once you change/modify something on your image.
ChameleonScales mentioned the use of pdb.gimp_item_delete because GIMP apparently does save this information on each iteration. But I am not sure on what "items" I would need to delete. I am not even sure what exactly an item is. Total Noob but also almost no info on the net.

***
I just tested again and apparently gimp.image_list() returns increasing numbers just before I perform image.delete at each iteration;

So this would be for iteration 37 before image_delete: Image List: (2, (38, 1))
and after using pdb.gimp_image_delete(new_image): Image List: (1, (1,))

This would be for iteration 38 before image_delete: Image List: (2, (37, 1))
and after using pdb.gimp_image_delete(new_image): Image List: (1, (1,))

and so on:
And iteration 100 would be Image List: (2, (99, 1))
and again after pdb.gimp_image_delete(new_image) Image List: (1, (1,))

and so on.. so that means Image List is actually increasing somehow even though it would show (1, (1,)) immediately after using pdb.gimp_image_delete(new_image)


RE: Gimp Script Memory Problem Question - Ofnuts - 12-21-2021

(12-20-2021, 08:11 PM)KoalaBear2022 Wrote: I am getting still the same result. I even disabled/enabled the Undo with image.disable_undo()/ image.enable_undo() which could possibly reduce memory consumption.

Are you modifying/processing your image at each iteration?

I am guessing GIMP is saving some Data once you change/modify something on your image.
ChameleonScales mentioned the use of pdb.gimp_item_delete because GIMP apparently does save this information on each iteration. But I am not sure on what "items" I would need to delete. I am not even sure what exactly an item is. Total Noob but also almost no info on the net.

***
I just tested again and apparently gimp.image_list() returns increasing numbers just before I perform image.delete at each iteration;

So this would be for iteration 37  before image_delete: Image List: (2, (38, 1))
and after using pdb.gimp_image_delete(new_image): Image List: (1, (1,))

This would be for iteration 38  before image_delete: Image List: (2, (37, 1))
and after using pdb.gimp_image_delete(new_image): Image List: (1, (1,))

and so on:
And iteration 100 would be Image List: (2, (99, 1))
and again after pdb.gimp_image_delete(new_image) Image List: (1, (1,))

and so on.. so that means Image List is actually increasing somehow even though it would show (1, (1,)) immediately after using pdb.gimp_image_delete(new_image)

You have all of my code. Do you see a memory creep when you use it?

These increasing numbers are expected, The first image has ID 1, the next ID 2, the next ID 3, etc... So you see a list of two images with your initial image (ID 1 and the one for the current iteration (ID 37).

Also, is the moroy creep in the Gimp process or in the Pythin one?


RE: Gimp Script Memory Problem Question - KoalaBear2022 - 12-21-2021

Thanks for your replies and infos. This is very useful.

So I tested your code as well. Your code has a big memory increase too from 1 to 10 GB on the GIMP process and then reduces when the loop finishes. But this iterates only 10 times. I guess on a larger iteration it would create similar problems.

The memory creep is definitely on the GIMP Process. 

Attached you can see a Screenshot on the Error when GIMP crashes.





RE: Gimp Script Memory Problem Question - Ofnuts - 12-21-2021

What is your Gimp version? I tested on 2.10.22 (not on 2.10.28 yet).


RE: Gimp Script Memory Problem Question - KoalaBear2022 - 12-21-2021

(12-21-2021, 05:03 PM)Ofnuts Wrote: What is your Gimp version? I tested on 2.10.22 (not on 2.10.28 yet).

 It is: GIMP 2.10.24