   _createCacheBitmap: (element, size) ->
     @_evictCacheBitmaps size if !@_roomInCacheForNewBitmap size
 
+    @_validateCacheByteSize()
     @_bitmapsCreated++
     getGlobalEpochCycle().logEvent "createCacheBitmap", "createCacheBitmap"
     bitmap = element.getBitmapFactory().newBitmap size
     @_cachedBitmaps.set element, cachedBitmap = new CacheBitmap element, bitmap, @_currentFrameNumber
     @_cacheByteSize += cachedBitmap.getByteSize()
+    @_validateCacheByteSize()
     bitmap
+
+  _validateCacheByteSize: ->
+    # # disabled for now; seems OK August 19, 2017
+    # cachedBitmaps = []
+    # unusedCacheByteSize = 0
+    # cacheByteSize = 0
+    # @_cachedBitmaps.forEach (bitmap, element) ->
+    #   cacheByteSize += bitmap.byteSize
+    #   cachedBitmaps.push {bitmap, element:element.inspectedName}
+
+    # for b in @_unusedCacheBitmaps
+    #   unusedCacheByteSize += b.byteSize
+
+    # unless (
+    #     @_cacheByteSize + @_unusedCacheByteSize <= @_maxCacheByteSize &&
+    #     @_cacheByteSize == cacheByteSize
+    #     @_unusedCacheByteSize == unusedCacheByteSize
+    #     )
+    #   log.error {
+    #     tracked: @_cacheByteSize, @_unusedCacheByteSize
+    #     actual: {cacheByteSize, unusedCacheByteSize}
+    #     cachedBitmaps:cachedBitmaps.length
+    #     unusedCacheBitmaps: @_unusedCacheBitmaps.length
+    #   }
+    #   throw new Error "bad _cacheByteSize"
+
+  canUseBitmap = (cachedBitmap, requestedSize) ->
+    {size} = cachedBitmap
+    size.area < requestedSize.area * 2 && size.gte requestedSize
 
   # return a recycledCachedbitmap with the right size (removing it from @_unusedCacheBitmaps)
   # or null if there is no matching recycledCacheBitmap
   _getUnusedCacheBitmap: (size) ->
-    for cachedBitmap, i in @_unusedCacheBitmaps when cachedBitmap.bitmap.size.eq size
+    for cachedBitmap, i in @_unusedCacheBitmaps when canUseBitmap cachedBitmap, size
       @_unusedCacheBitmaps = remove @_unusedCacheBitmaps, i
       @_unusedCacheByteSize -= cachedBitmap.getByteSize()
       return cachedBitmap

    valuesIterator = @_cachedBitmaps.values()
    while !(vi = @_cachedBitmaps.next()).done
      cachedBitmap = vi.value
      if cachedBitmap.lastFrameUsed < currentFrameNumber - 1 && cachedBitmap.bitmap.size.eq size
        return cachedBitmap

  # return true if we have space to allocate a bitmap of the specified 'size'

  # remove oldest bitmaps from the cache until we have enough from for a new bitmap of the specified size
  # return null
  _evictCacheBitmaps: (size) ->
    @_validateCacheByteSize()
    byteSize = byteSizeFromSize size
    reduceToAtLeast = @_maxCacheByteSize - size
    for cachedBitmap in @recycleableSortedCacheBitmaps
      if cachedBitmap = mapRemove @_cachedBitmaps, cachedBitmap.element
        cachedBitmap.elementDoneWithCacheBitmap()
        @_cacheByteSize -= cachedBitmap.getByteSize()
        break if @_cacheByteSize <= reduceToAtLeast

    @_validateCacheByteSize()
    null
