1# Obtaining Application and File System Space Statistics 2<!--Kit: Core File Kit--> 3<!--Subsystem: FileManagement--> 4<!--Owner: @wang_zhangjun; @zhuangzhuang--> 5<!--Designer: @wang_zhangjun; @zhuangzhuang; @renguang1116--> 6<!--Tester: @liuhonggang123; @yue-ye2; @juxiaopang--> 7<!--Adviser: @foryourself--> 8 9This topic describes how to obtain statistics on the space occupied by your application and the free space and total space of a file system. 10 11## Available APIs 12 13For details about APIs, see [ohos.file.statvfs](../reference/apis-core-file-kit/js-apis-file-statvfs.md) and [ohos.file.storageStatistics](../reference/apis-core-file-kit/js-apis-file-storage-statistics.md). 14 15**Table 1** APIs for application and file system space statistics 16 17| Module| API| Description| 18| -------- | -------- | -------- | 19| \@ohos.file.storageStatistics | getCurrentBundleStats | Obtains the storage space of the current application, in bytes.| 20| \@ohos.file.storageStatistics | getFreeSize | Obtains the available space of the built-in storage, in bytes. This API returns the result asynchronously.<br>**Note**: This API is supported since API version 15.| 21| \@ohos.file.storageStatistics | getFreeSizeSync | Obtains the available space of the built-in storage, in bytes. This API returns the result synchronously.<br>**Note**: This API is supported since API version 15.| 22| \@ohos.file.storageStatistics | getTotalSize | Obtains the total space of the built-in storage, in bytes. This API returns the result asynchronously.<br>**Note**: This API is supported since API version 15.| 23| \@ohos.file.storageStatistics | getTotalSizeSync | Obtains the total space of the built-in storage, in bytes. This API returns the result synchronously.<br>**Note**: This API is supported since API version 15.| 24| \@ohos.file.statvfs | getFreeSize | Obtains the free space of a file system, in bytes.| 25| \@ohos.file.statvfs | getTotalSize | Obtains the total space of a file system, in bytes.| 26 27**Table 2** Attributes for application space statistics 28 29> **NOTE** 30> 31> The directories involved in the statistics path column in the table refer to the sandbox paths of applications. Before viewing the paths, you need to access the corresponding sandbox space by running the following commands: 32> 33> 1. hdc shell 34> 2. nsenter -t {pid} -m sh 35 36| BundleStats Attribute| Description| Directory for Statistics| 37| -------- | -------- | -------- | 38| appSize | Size of the application installation files, in bytes.| Application installation file directory:<br>**/data/storage/el1/bundle**| 39| cacheSize | Size of the application cache files, in bytes.| Application cache file directories:<br>/data/storage/\${el1-el5}/base/cache<br>/data/storage/\${el1-el5}/base/haps/\${moduleName}/cache<br>/data/storage/el2/sharefiles/cache<br>/data/storage/el2/sharefiles/haps/${moduleName}/cache<br> **NOTE**<br>**\${el1-el5}** denotes the directories [el1, el2, el3, el4, el5](./app-sandbox-directory.md#application-file-directory-and-application-file-path). **\${moduleName}** indicates the module name.| 40| dataSize | Size of application files (excluding application installation files), in bytes.| The files include local files, distributed files, and database files of the application.<br>- Local file directories (parent directories of the **cache** directories):<br>/data/storage/\${el1-el5}/base<br>- Distributed application directory:<br>/data/storage/el2/distributedfiles<br>- Database directories:<br>/data/storage/\${el1-el5}/database<br> **NOTE**<br>**\${el1-el5}** denotes the directories [el1, el2, el3, el4, el5](./app-sandbox-directory.md#application-file-directory-and-application-file-path).| 41 42## How to Develop 43 44- Obtain the free space of **/data** of the file system. 45 46 ```ts 47 import { statfs } from '@kit.CoreFileKit'; 48 import { BusinessError } from '@kit.BasicServicesKit'; 49 import { common } from '@kit.AbilityKit'; 50 51 // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext. 52 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 53 let path = context.filesDir; 54 statfs.getFreeSize(path, (err: BusinessError, number: number) => { 55 if (err) { 56 console.error(`Invoke getFreeSize failed, code is ${err.code}, message is ${err.message}`); 57 } else { 58 console.info(`Invoke getFreeSize succeeded, size is ${number}`); 59 } 60 }); 61 ``` 62 63- Obtain the space occupied by the current application. 64 65 ```ts 66 import { storageStatistics } from '@kit.CoreFileKit'; 67 import { BusinessError } from '@kit.BasicServicesKit'; 68 69 storageStatistics.getCurrentBundleStats((err: BusinessError, bundleStats: storageStatistics.BundleStats) => { 70 if (err) { 71 console.error(`Invoke getCurrentBundleStats failed, code is ${err.code}, message is ${err.message}`); 72 } else { 73 console.info(`Invoke getCurrentBundleStats succeeded, appsize is ${bundleStats.appSize}`); 74 } 75 }); 76 ``` 77 78- Obtain the total space of the built-in storage asynchronously. 79 80 ```ts 81 import { storageStatistics } from '@kit.CoreFileKit'; 82 import { BusinessError } from '@kit.BasicServicesKit'; 83 84 storageStatistics.getTotalSize().then((number: number) => { 85 console.info(`getTotalSize successfully, number is ${number}`); 86 }).catch((err: BusinessError) => { 87 console.error(`getTotalSize failed with error, code is ${err.code}, message is ${err.message}`); 88 }); 89 ``` 90 91- Obtain the total space of the built-in storage synchronously. 92 93 ```ts 94 import { storageStatistics } from '@kit.CoreFileKit'; 95 import { BusinessError } from '@kit.BasicServicesKit'; 96 97 try { 98 let number = storageStatistics.getTotalSizeSync(); 99 console.info(`getTotalSizeSync successfully, number is ${number}`); 100 } catch (err) { 101 let error: BusinessError = err as BusinessError; 102 console.error(`getTotalSizeSync failed with error, code is ${error.code}, message is ${error.message}`); 103 } 104 ``` 105 106- Obtain the available space of the built-in storage asynchronously. 107 108 ```ts 109 import { storageStatistics } from '@kit.CoreFileKit'; 110 import { BusinessError } from '@kit.BasicServicesKit'; 111 112 storageStatistics.getFreeSize().then((number: number) => { 113 console.info(`getFreeSize successfully, number is ${number}`); 114 }).catch((err: BusinessError) => { 115 console.error(`getFreeSize failed with error, code is ${err.code}, message is ${err.message}`); 116 }); 117 ``` 118 119- Obtain the available space of the built-in storage synchronously. 120 121 ```ts 122 import { storageStatistics } from '@kit.CoreFileKit'; 123 import { BusinessError } from '@kit.BasicServicesKit'; 124 125 try { 126 let number = storageStatistics.getFreeSizeSync(); 127 console.info(`getFreeSizeSync successfully, number is ${number}`); 128 } catch (err) { 129 let error: BusinessError = err as BusinessError; 130 console.error(`getFreeSizeSync failed with error, code is ${error.code}, message is ${error.message}`); 131 } 132 ``` 133