• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Obtaining Application and File System Space Statistics
2
3This topic describes how to obtain statistics on the space occupied by your application and the free space and total space of a file system.
4
5## Available APIs
6
7For 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).
8
9**Table 1** APIs for application and file system space statistics
10
11| Module| API| Description|
12| -------- | -------- | -------- |
13| \@ohos.file.storageStatistics | getCurrentBundleStats | Obtains the storage space of the current application, in bytes.|
14| \@ohos.file.storageStatistics | getFreeSize | Obtains the total space of the built-in storage, in bytes. This API returns the result asynchronously.|
15| \@ohos.file.storageStatistics | getFreeSizeSync | Obtains the total space of the built-in storage, in bytes. This API returns the result synchronously.|
16| \@ohos.file.storageStatistics | getTotalSize | Obtains the available space of the built-in storage, in bytes. This API returns the result asynchronously.|
17| \@ohos.file.storageStatistics | getTotalSizeSync | Obtains the available space of the built-in storage, in bytes. This API returns the result synchronously.|
18| \@ohos.file.statvfs | getFreeSize | Obtains the free space of a file system, in bytes.|
19| \@ohos.file.statvfs | getTotalSize | Obtains the total space of a file system, in bytes.|
20
21**Table 2** Attributes for application space statistics
22
23| BundleStats Attribute| Description| Directory for Statistics|
24| -------- | -------- | -------- |
25| appSize | Size of the application installation files, in bytes.| Application installation file directory:<br>**/data/storage/el1/bundle**|
26| cacheSize | Size of the application cache files, in bytes.| Application cache file directories:<br>**/data/storage/el1/base/cache**<br>**/data/storage/el1/base/haps/entry/cache**<br>**/data/storage/el2/base/cache**<br>**/data/storage/el2/base/haps/entry/cache**|
27| dataSize | Size of other files of the application, 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/base**<br>**/data/storage/el2/base**<br>- Distributed application directory:<br>/data/storage/el2/distributedfiles<br>- Database directories:<br>**/data/storage/el1/database**<br>**/data/storage/el2/database**|
28
29## Development Example
30
31- Obtain the free space of **/data** of the file system.
32
33  ```ts
34  import { statfs } from '@kit.CoreFileKit';
35  import { BusinessError } from '@kit.BasicServicesKit';
36  import { common } from '@kit.AbilityKit';
37
38  let context = getContext(this) as common.UIAbilityContext;
39  let path = context.filesDir;
40  statfs.getFreeSize(path, (err: BusinessError, number: number) => {
41    if (err) {
42      console.error(`Invoke getFreeSize failed, code is ${err.code}, message is ${err.message}`);
43    } else {
44      console.info(`Invoke getFreeSize succeeded, size is ${number}`);
45    }
46  });
47  ```
48
49- Obtain the space occupied by the current application.
50
51  ```ts
52  import { storageStatistics } from '@kit.CoreFileKit';
53  import { BusinessError } from '@kit.BasicServicesKit';
54
55  storageStatistics.getCurrentBundleStats((err: BusinessError, bundleStats: storageStatistics.BundleStats) => {
56    if (err) {
57      console.error(`Invoke getCurrentBundleStats failed, code is ${err.code}, message is ${err.message}`);
58    } else {
59      console.info(`Invoke getCurrentBundleStats succeeded, appsize is ${bundleStats.appSize}`);
60    }
61  });
62  ```
63
64- Obtain the total space of the built-in storage asynchronously.
65
66  ```ts
67  import { storageStatistics } from '@kit.CoreFileKit';
68  import { BusinessError } from '@kit.BasicServicesKit';
69
70  storageStatistics.getTotalSize().then((number: number) => {
71    console.info("getTotalSize successfully:" + JSON.stringify(number));
72  }).catch((err: BusinessError) => {
73    console.error("getTotalSize failed with error:"+ JSON.stringify(err));
74  });
75  ```
76
77- Obtain the total space of the built-in storage synchronously.
78
79  ```ts
80  import { storageStatistics } from '@kit.CoreFileKit';
81  import { BusinessError } from '@kit.BasicServicesKit';
82
83  try {
84    let number = storageStatistics.getTotalSizeSync();
85    console.info("getTotalSizeSync successfully:" + JSON.stringify(number));
86  } catch (err) {
87    let error: BusinessError = err as BusinessError;
88    console.error("getTotalSizeSync failed with error:" + JSON.stringify(error));
89  }
90  ```
91
92- Obtain the available space of the built-in storage asynchronously.
93
94  ```ts
95  import { storageStatistics } from '@kit.CoreFileKit';
96  import { BusinessError } from '@kit.BasicServicesKit';
97
98  storageStatistics.getFreeSize().then((number: number) => {
99    console.info("getFreeSize successfully:" + JSON.stringify(number));
100  }).catch((err: BusinessError) => {
101    console.error("getFreeSize failed with error:" + JSON.stringify(err));
102  });
103  ```
104
105- Obtain the available space of the built-in storage synchronously.
106
107  ```ts
108  import { storageStatistics } from '@kit.CoreFileKit';
109  import { BusinessError } from '@kit.BasicServicesKit';
110
111  try {
112    let number = storageStatistics.getFreeSizeSync();
113    console.info("getFreeSizeSync successfully:" + JSON.stringify(number));
114  } catch (err) {
115    let error: BusinessError = err as BusinessError;
116    console.error("getFreeSizeSync failed with error:" + JSON.stringify(error));
117  }
118  ```
119