How to schedule collection cycles for custom mark-sweep collector?

后端 未结 2 391
后悔当初
后悔当初 2021-01-14 04:06

I\'ve written a simple garbage collector for a Postscript virtual machine, and I\'m having difficulty designing a decent set of rules for when to do a collection (when the f

相关标签:
2条回答
  • 2021-01-14 04:38

    There are several applicable philosophies:

    • Do garbage collection as last-ditch avoidance of expanding the heap during an allocation. This is probably the most common strategy.
    • Do garbage collection periodically, like every hundredth allocation or deallocation. In some situations, this might decrease the overall effort of garbage collection by not letting fragmentation get out of hand.
    • Don't do any garbage collection. Always a possible strategy, especially for short-lived or simple programs.

    As a developer of garbage collection, it might be desirable to give the choice of strategy to the application since it might know which will be most effective. Of course, if it doesn't have a preference, you should choose a default.

    0 讨论(0)
  • 2021-01-14 04:57

    Here is the periodic collection strategy incorporated into the original code:

    enum { PERIOD = 10 };
    
    unsigned gballoc(mfile *mem, unsigned sz) {
        unsigned z = adrent(mem, FREE);
        unsigned e;
        static period = PERIOD;
        memcpy(&e, mem->base+z, sizeof(e));
    try_again:
        while (e) {
            if (szent(mem,e) >= sz) {
                memcpy(mem->base+z, mem->base+adrent(mem,e), sizeof(unsigned));
                return e;
            }
            z = adrent(mem,e);
            memcpy(&e, mem->base+z, sizeof(e));
        }
        if (--period == 0) {
            period = PERIOD;
            collect(mem, 0);
            goto try_again;
        }
        return mtalloc(mem, 0, sz);
    }
    
    0 讨论(0)
提交回复
热议问题