Reference

allocateBytes() / getAllocatableBytes()

Android StorageManager APIs that report and reserve free space. getAllocatableBytes() returns bytes an app could obtain (including space the system can reclaim from clearable caches), and allocateBytes() asks the OS to free that space before you write a large file.

Android developmentAndroid

allocateBytes() / getAllocatableBytes()

Also known as: allocatable bytes android, reclaim space api, android allocatebytes, StorageManager allocateBytes

Android StorageManager APIs that report and reserve free space. getAllocatableBytes() returns bytes an app could obtain (including space the system can reclaim from clearable caches), and allocateBytes() asks the OS to free that space before you write a large file.

  • Introduced in Android 8.0 (API 26) on android.os.storage.StorageManager.
  • getAllocatableBytes() can exceed normal free space because it counts reclaimable cache.
  • allocateBytes() makes the OS delete clearable caches to guarantee room before a large write.

What these APIs do

Since Android 8.0 (API 26), StorageManager exposes a space-management pair built around storage UUIDs. getAllocatableBytes(UUID) returns the number of bytes your app could allocate right now, and crucially this number can be *larger* than File.getUsableSpace() because it includes space the platform can reclaim by deleting clearable cache files from your app and other apps.

allocateBytes(FileDescriptor, long) (or the UUID-based overload) tells the OS to actually clear enough reclaimable cache to satisfy the request, throwing if it cannot. The idea is to stop apps from racing the user to disk: instead of failing with ENOSPC mid-write, you reserve room first, then write.

Typical usage pattern

Get the UUID for the volume backing a file with storageManager.getUuidForPath(file), call getAllocatableBytes(uuid) to check headroom, and if it is enough call allocateBytes(uuid, bytesNeeded) before opening your output stream. The reserved space is associated with the FileDescriptor, so once you write your data it is no longer reclaimable.

This is the *programmatic* counterpart to what the cleaner UI does manually. Rather than prompting the user to free space, a well-behaved app can have the system purge cache itself, which is exactly the cache that storage cleaners target when they free room on a full device.

Related terms

Keep reading the reference.

Act on it

Guides and tools for this topic.