private boolean canAllocate(int bytes, boolean doGC) {
        long mem = runtime.freeMemory();

        // Enough memory?
        if ((mem - bytes) > minMemory) {
            return true;
        }

        // Do we allow the heap to grow?
        long total = runtime.totalMemory();

        if (maxMemory > total) {
            mem += maxMemory - total;
            if ((mem - bytes) > minMemory) {
                return true;
            }
        }

        // This is so that checkMemory() can try to clear out
        //  some cached pages _before_ running the garbage collector,
        //  which saves a lot of CPU
        if (!doGC)
            return false;

        // lets purge some pages
        reduceMemory();

        // Nope, try GC
        System.gc();
        mem = runtime.freeMemory();

        // Enough memory?
        if ((mem - bytes) > minMemory) {
            return true;
        }

        // Nope, try heavy GC
        System.runFinalization();
        System.gc();
        mem = runtime.freeMemory();

        // Enough memory?
        if ((mem - bytes) > minMemory) {
            return true;
        }

        // We are low on memory!
        //System.out.println("Failed -not enough Available Memory " + mem);
        return false;
    }