How to Check if dispose() works?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to Check if dispose() works?

peterxu422
How can you check if your dispose() functions work in the OS classes?
Reply | Threaded
Open this post in threaded view
|

Re: How to Check if dispose() works?

cadet1620
Administrator
peterxu422 wrote
How can you check if your dispose() functions work in the OS classes?
The most important thing to test is that Array.dispose() and the underlying Memory.free() are properly returning memory blocks to the memory pool.

Array.dispose() is simple enough that I didn't bother to test it independently.  I wrote this routine to give Memory alloc() and free() a workout:
    function void leakTest() {
        var Array a, b, c, d, e, f;
        var int n, size;
        
        do Output.printString("Leak test");
        do Output.println();
        do Main.printFreeList();

        let size = 100;
        let n = 0;
        while (n < 1000) {
            let a = Array.new(size);
            let b = Array.new(size);
            let c = Array.new(size);
            let d = Array.new(size);
            let e = Array.new(size);
            let f = Array.new(size);
            do e.dispose();         // a b c d - f
            do b.dispose();         // a - c d - f
            do a.dispose();         // --- c d - f      coalesce with following free block
            do c.dispose();         // ----- d - f      coalesce with preceding free block
            do d.dispose();         // --------- f      coalesce with surrounding free blocks
            do f.dispose();
        
            let n = n+1;
            if (size < 1000) {
                let size = size+11; // Increasing size ensures that freed blocks must
            }                       // be coalesced, not just reused.
        }
    do Main.printFreeList();
    return;
    }

Memory.printFreeList() is a debug function I added to my Memoy.jack that prints the free memory list.  leakTest() is considered to succeed if the free list is the same before and after the test.

--Mark