How to free memory usage after closing document?

I am using scripter to copy and paste many layer into one document and then closing the document and then starting a new one on the same scripter. But, what happens is that even though i have closed the document the memory usage keep on adding up. But this problem gets solved when i start a new scripter. Is there a way to clear the memory usage after i have closed the document? so that i can keep using the same scripter for further copying and pasting for new document? I have 1000s of documents to do so and i have written the commands in order so that it keeps on copying and pasting and then close the document and start a new one. And i cant change the scripter everytime. Please Help.

That apparent increase in RAM use as time goes by has been discussed before and it is puzzling.

One thing you could try, which may or may not help, is in Settings → Configure Krita → General → Miscellaneous tab: set the Undo stack size to zero and restart krita.

2 Likes

I tried but it doesn’t help either. I do not know what else to do? Closing scripter after every successful copy paste is time consuming and troublesome.

It’s a known issue, at the least for me. Someone in this bugreport suggested using QTimers: 412740 – Krita leaks memory on exporting multiple pages from the comics manager , but then halla tried fixing the issue, and that was in a period when I couldn’t test it. It’s still a problem as far as I can tell.

1 Like

I am really sorry but i am very new to coding in python or krita. I can not understand a word in that thread later on. Could anyone please simplify that up for a layman like me.

1 Like

It’s a bug, likely, or at the very least super unintuitive, in which case I think we should improve usability.

1 Like

If you are doing batch processing, then maybe kritarunner is what you need?

Just out of curiosity though, what happens if you run it through tenscripts? Does same thing happen?

1 Like

If you clean the variables your using it does not help?

I haven’t used 10 scripts. But will try, Lets see what happens.

No it does not

Tenscripts is not working

What is krita runner?

Krita runner lets you run scripts from commandline.

Also, can you post a copy of the script you are running?

Is it possible in windows?
And here is my script.

from krita import *
app = Krita.instance()
Ba = app.openDocument(‘Address of file’)
app.activeWindow().addView(Ba)
Ha = app.openDocument(‘Adress of file’)
app.activeWindow().addView(Ha)
app.action(‘copy_layer_clipboard’).trigger()
Ha.waitForDone()
Ha.close()
app.action(‘paste_layer_from_clipboard’).trigger()
Ba.waitForDone()
Ho = app.openDocument(‘Address of file’)
app.activeWindow().addView(Ho)
app.action(‘copy_layer_clipboard’).trigger()
Ho.waitForDone()
Ho.close()
app.action(‘paste_layer_from_clipboard’).trigger()
Ba.waitForDone()
Mo = app.openDocument(‘Address of file’)
app.activeWindow().addView(Mo)
app.action(‘copy_layer_clipboard’).trigger()
Mo.waitForDone()
Mo.close()
app.action(‘paste_layer_from_clipboard’).trigger()
Ba.waitForDone()
Cl1 = app.openDocument(‘Address of file’)
app.activeWindow().addView(Cl1)
app.action(‘copy_layer_clipboard’).trigger()
Cl1.waitForDone()
Cl1.close()
app.action(‘paste_layer_from_clipboard’).trigger()
Ba.waitForDone()
Ac = app.openDocument(‘Address of file’)
app.activeWindow().addView(Ac)
app.action(‘copy_layer_clipboard’).trigger()
Ac.waitForDone()
Ac.close()
app.action(‘paste_layer_from_clipboard’).trigger()
Ba.waitForDone()
Cl = app.openDocument(‘Address of file’)
app.activeWindow().addView(Cl)
app.action(‘copy_layer_clipboard’).trigger()
Cl.waitForDone()
Cl.close()
app.action(‘paste_layer_from_clipboard’).trigger()
Ba.waitForDone()
Bo = app.openDocument(‘Address oif file’)
app.activeWindow().addView(Bo)
app.action(‘copy_layer_clipboard’).trigger()
Bo.waitForDone()
Bo.close()
app.action(‘paste_layer_from_clipboard’).trigger()
Ba.waitForDone()
currentDocument = app.activeDocument()
currentDocument.setBatchmode(True)
currentDocument.saveAs(‘Adress of file.png’)
Ba.waitForDone()
currentDocument.close()

I have to run thousands of such scripts just to copy and paste layers but had to open a new scripter to run another script because running on the same scripter does not free memory space.

Can you write and run a Windows system level batch file, parameterised to loop through a .kra file collection, probably in a folder, that uses Krunner once then krita closes (and frees up RAM) for each individual .kra file?

It would have a restart time overhead but you could leave it running overnight.

Well first of all, why are you repeating things multiple times? There is such a thing as a loop you know right? I would also isolate things in their own function or namespace.

from krita import *

def startCommand(orgfile, newfile, files):
  app = Krita.instance()
  doc = app.openDocument(orgfile)
  app.activeWindow().addView(doc)

  for fileName in files:
    doc2 = app.openDocument(fileName)
    app.activeWindow().addView(doc2)
    app.action('copy_layer_clipboard').trigger()
    doc2.waitForDone()
    doc2.close()
    app.action('paste_layer_from_clipboard').trigger()
    doc.waitForDone()

  currentDocument = app.activeDocument()
  currentDocument.setBatchmode(True)
  currentDocument.saveAs(newfile)
  doc.waitForDone()
  currentDocument.close()

startCommand(
'original file',
'new file',
[
  'file name 1',
  'file name 2',
  'file name 3'
 ]
)

This should also insure the gc is doing its work.

Try this, I haven’t tested it but it should be equivalent(Sorry it is 6am here).

=> Garbage Collector
The Python process to free memory of “not used anymore memory resource”… (In simple terms :slight_smile: )

Grum999

1 Like

Even if i do that it doesnt free up the memory and requires me to open the new scripter.

I am unsure if i can do that. If only i know what krunner is and how does it work?