Trolltech Home | Qt4-preview-feedback Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt4-preview-feedback Archive, April 2007
Re: QT4.3 QScriptEngine Garbage Collecting


Message 1 in thread

Simon Bourne wrote:
> Thanks, that works a treat. Will be nicer from C++ API later, but
> perfectly good solution for now.
>
> Regards,
> Simon
Hi Simon,
QScriptEngine::gc() has been added; it's been in the snapshots for a few
days.
Also, the GC behavior has been improved, so that in cases such as yours
(creating large arrays) you should see that GC is performed
automatically at a much earlier time than before (should be in
tomorrow's snapshots).

Regards,
Kent

>
> On 22/03/07, Kent Hansen <khansen@xxxxxxxxxxxxx> wrote:
>> Simon Bourne wrote:
>> > Hi,
>> >
>> > Many thanks for your help in the past regarding this subject but I
>> > have to admit that I'm still struggling to understand the
>> > ScriptEngine's decision making process regarding garbage collection.
>> > For example if I build a simple dialog with a button that triggers the
>> > engine to build a local variable that contains an array with a million
>> > numbers in it, via a for loop, like so:
>> >
>> > {
>> > var b=new Array();
>> > for(var i=0 ;i<999999;i++)
>> > { b[i]=1; }
>> > }
>> >
>> > I can imagine a new array is allocated and the entries added. I can
>> > confirm this in Windows with the memory growing by approx 50 MB. The
>> > array has immediately gone out of scope being a local variable so
>> > surely this would trigger it to be freed on next garbage collection?
>> > However the application then sits there, subsequent clicks of the
>> > button will allocate fresh batches of 50 MB. No garbage collection
>> > appears to be insight. Sometimes this takes around 10 - 20 minutes
>> > before a garbage collection is processed. Source code for this simple
>> > test application is attached below.
>> >
>> > This is a fundamental problem with this scripting engine as my
>> > applications rapidly run out of available memory. Can this be
>> > addressed in the short term? Maybe the garbage collection could be
>> > triggered more often, or even exposed to the QScriptEngine API
>> > allowing us to trigger this process when we know no time critical code
>> > is running, thus not hampering any application performance.
>> >
>> > Thanks,
>> > Simon Bourne
>> Hi Simon,
>> Thanks for your detailed report. I can reproduce this issue. You can
>> call gc() from script code to work around it for now; e.g.
>>
>> engine.evaluate("gc()");
>>
>> There is currently no corresponding method in the C++ API. We'll make
>> sure that this is improved for the final 4.3 release, by exposing the
>> relevant function(s) and/or making sure that the collector is triggered
>> more frequently in these situations. I'll post a follow-up once the
>> relevant changes make it into the snapshots.
>>
>> Regards,
>> Kent
>>
>> >
>> >
>> > ---------------------------------------------------------------------
>> >
>> > #include <QtGui/QDialog>
>> > #include <QtScript/QScriptEngine>
>> >
>> > class SlowGC : public QDialog
>> > {
>> >    Q_OBJECT
>> >
>> > public:
>> >    SlowGC(QWidget *parent = 0, Qt::WFlags flags = 0);
>> >
>> >   QScriptEngine engine;
>> >
>> > private:
>> >    Ui::SlowGCClass ui;
>> >
>> > private slots:
>> > void on_pushButton_clicked();
>> > };
>> >
>> > SlowGC::SlowGC(QWidget *parent, Qt::WFlags flags)
>> >    : QDialog(parent, flags)
>> > {
>> > ui.setupUi(this);
>> > }
>> >
>> > void SlowGC::on_pushButton_clicked()
>> > {
>> >    engine.evaluate("{ var b=new Array(); for(var i=0 ;i<999999;i++) {
>> > b[i]=1; } }"); // Allocs 50MB
>> > }
>> >
>> > int main(int argc, char *argv[])
>> > {
>> >    QApplication a(argc, argv);
>> >    SlowGC w;
>> >    w.show();
>> >    a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
>> >    return a.exec();
>> > }
>> >
>> > To unsubscribe - send "unsubscribe" in the subject to
>> > qt4-preview-feedback-request@xxxxxxxxxxxxx
>> >
>>
>> To unsubscribe - send "unsubscribe" in the subject to
>> qt4-preview-feedback-request@xxxxxxxxxxxxx
>>
>>

To unsubscribe - send "unsubscribe" in the subject to qt4-preview-feedback-request@xxxxxxxxxxxxx