Page 1 of 1

memory management

Posted: Sat Dec 03, 2011 4:03 pm
by hungerburg
I use the commandline mode to batch-create dxf drawings. Top (the taskmanager) shows that RAM used continously grows. Fortunately, there are not enough iterations to exceed physical RAM. Still, I am curious. Is this some lazy garbage collector in the ECMA interpreter, or do I have to somehow close documents created inside of a loop?

Posted: Thu Mar 01, 2012 11:39 am
by andrew
I'm looking into memory management in the ECMAScript wrappers at the moment.

Usually, nothing has to be cleaned up as the garbage collector takes care of cleaning up object instances of strings, numbers, RVector, RLine, RLineData, etc.

However, there are some types of objects which have to be cleaned up manually if they are created in ECMAScript:

- RMemoryStorage
- RSpatialIndexNavel
- RDocument
- RDocumentInterface

Documents can for example be created like this:

Code: Select all

var st = new RMemoryStorage();
var si = new RSpatialIndexNavel();
var d = new RDocument(st, si);
var di = new RDocumentInterface(d);
The memory storage and spatial index are owned by the document. The document is owned by the document interface, so cleaning up the document interface is enough in this case:

Code: Select all

di.destroy();
Scenes and views attached to the document interface are also cleaned up automatically.

If there is no document interface, you need to destroy the document directly. Similarly, if there is no document (unlikely), you need to destroy the storage and spatial index.

There are also appear to be some Qt classes that need to be cleaned up manually if created in ECMAScript:

- QPrinter
- QPainter
- QXmlResultItems
- QXmlStreamWriter
- possibly more..?

Some of the ECMAScripts from QCAD self need to be fixed to take these things into account, so if you still experience leaks, that might be the reason.

Posted: Wed Mar 14, 2012 9:43 pm
by hungerburg
Thank You Andrew for the advice. I immediately changed my scripts to destroy any or most of the documentInterfaces created and let it run. I have not taken notes before, only account from my memory, but the savings are worth it. Hundreds of MB turned into tens, whith close to 1000 drawings created.