@bearishbull I don’t know anything about kritarunner and it’s a very long time since I wrote a looping batch file to run as a Windows command and even then it was a simple one.
How to use .deleteLater() and sip.delete(). Would you please share an example?
As wolthera suggested Qtimers but i do not know how to use it or where to use it in windows 11.
You can use QTimer via:
QTimer.singleShot(1, lambda: startCommand(
'original file',
'new file',
[
'file name 1',
'file name 2',
'file name 3'
]
))
as for .deleteLater and sip.delete(), obj.deleteLater()
and import sip
then sip.delete(obj)
but I would be careful with these.
That said, in my windows VM, I tried to emulate your issue and the memory usage is returning just fine when I close things. Closing scripter has no impact one way or the other either…
In task manager it does free memory but in krita down below as shown in picture, it keeps on increasing until the script ends and then krita ultimately crashes once its full.
That should be the individual document resources. Are you trying to put like 1000 pictures into a single document? and are just breaking it up step by step?
See here the resources after the docs close:
i am copying 8 layers one by one like you are doing and closing it before pasting it in the original document and after that closing the original document and then repeating the process for several other original documents. But even when i am closing the original document, krita’s memory keeps building up until it crashes or freezes. I have documents of size 4000*4000 300dpi. So they are little bigger but not that much that it will crash the krita. Normally without using scriptter when i do the process manually by clicking, after closing document memory gets freed but not when i am using scripter .
My original document after copying and pasting takes 1.8 gb memory but it doesnt get freed when i close it using scripter but manually it does. I do not know why
It’s hard to debug something when we can’t emulate it. But if you are saying that closing it manually works, you can close it manually programatically too.
from krita import *
qwin = Krita.instance().activeWindow().qwindow()
mdi = qwin.findChild(QMdiArea)
mdi.closeActiveSubWindow()
This will close the current document you are on
Hi
On my side I can reproduce the problem.
I’ve made 2 simple tests:
- Loop over 63 documents, open/close => no memory leak (Krita stay around 2~3% of 64GB in this case during script execution)
- Loop over 63 documents, open/add view/close => memory leak (Krita slowy grows to 30% of 64GB in this case at the end of script execution)
The problem seems related to view: once document is added to a view, closing document doesn’t free memory.
Tried del
and deleteLater()
on view & documents, but doesn’t change anything…
Didn’t tested on windows, only with Linux appimage.
Here the script:
from krita import *
def startCommand(files):
app = Krita.instance()
i=0
for fileName in files:
print(fileName, i, len(files))
doc2 = app.openDocument(fileName)
v2=app.activeWindow().addView(doc2)
doc2.close()
i+=1
startCommand(
[
"/home/grum/xxxxxxxxxxxxxxxx.png",
"/home/grum/xxxxxxxxxxxxxxxx.png",
]
)
→ Comment/Uncomment v2=app.activeWindow().addView(doc2)
to check the difference
Grum999
Using this didnt solve the problem either. I do not know why but manual closing of documents works.
Yeah may be its addView thing that keeps the image in the memory unitl script is done. Is there a way to copy layer from another document without adding view to it?
can you try just running your script for heavy files taking around 500 mb of memory each. You will see difference. Since your files had low memory usage you couldnt spot the difference. You can increase the loop number too if not file with large memory usage. Difference will be spotted.
I mean to say that by using scripter, memory usage just keeps on building and building even if you close documents by whatever means you try on scripter. It will be freed only and only when script ends. Either this is because of addView as suggested by Grum999 or any other operation.
Here an example of how to work without view&actions
Should be faster and less memory consumption.
Note: as layers are added to document, memory will finally grow, but not as fast as than before. At least, memory is freed at end of execution
from krita import *
def startCommand(newDocFileName, files):
app = Krita.instance()
doc = app.createDocument(1500,1500, "Test", "RGBA", "U8", "", 300.0)
i=0
for fileName in files:
print(fileName, i, len(files))
doc2 = app.openDocument(fileName)
pixels=doc2.pixelData(0,0,doc2.width(),doc2.height())
node=doc.createNode(f"From file: {fileName}", "paintlayer")
node.setPixelData(pixels,0,0,doc2.width(),doc2.height())
doc.rootNode().addChildNode(node, None)
doc2.close()
i+=1
doc.setBatchmode(True)
doc.saveAs(newDocFileName)
doc.close()
print('done')
startCommand(
"/home/grum/Temporaire/test.kra",
[
"/home/grum/xxxxxxxxxxxxxxxx.png",
"/home/grum/xxxxxxxxxxxxxxxx.png",
]
)
Grum999
@Alex_Mehler1 has written a restart plugin for Krita, it is a piece of code in an early stage, but maybe it is suitable for your needs, and you can implement it in your process of scripts. It wouldn’t solve the problem itself if this would be usable, but maybe it helps as a workaround?
&
Michelist
Thnx a ton. It seems to work fine till now. Sped up my process of copying and pasting 10 times faster and also memory usage is not the problem anymore. So if anyone wants to just copy and paste and doesn’t want the memory to get used up please see through @Grum999 script. Addview was the main problem I think in memory usage
Thnx Everyone for sharing your thoughts and giving me your precious time.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.