public boolean checkMemory(int memoryNeeded) {
        long beginTime = System.currentTimeMillis();
        int count = 0;
        // try and allocate memory, but quit after ten tries.
        while (!canAllocate(memoryNeeded, count > 0)) {
            // cache files on memory check
            boolean reducedSomething = reduceMemory();
            if (!reducedSomething && count > 0) {
                finishedMemoryProcessing(beginTime);
                return false;
            }
            count++;
            if (count > 10) {
                finishedMemoryProcessing(beginTime);
                return false;
            }
        }
        finishedMemoryProcessing(beginTime);
        return true;
    }