• 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 the APIs, see [ohos.file.statvfs](../reference/apis/js-apis-file-statvfs.md) and [ohos.file.storageStatistics](../reference/apis/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.statvfs | getFreeSize | Obtains the free space of a file system, in bytes.|
15| \@ohos.file.statvfs | getTotalSize | Obtains the total space of a file system, in bytes.|
16
17**Table 2** Attributes for application space statistics
18
19| BundleStats Attribute| Description| Directory for Statistics|
20| -------- | -------- | -------- |
21| appSize | Size of the application installation files, in bytes.| /data/storage/el1/bundle|
22| cacheSize | Size of the application cache files, in bytes.| /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|
23| 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 file directory:<br>**/data/storage/el2/distributedfiles**<br>- Database directories:<br>**/data/storage/el1/database**<br>**/data/storage/el2/database**|
24
25## Development Example
26
27- Obtain the free space of **/data** of the file system.
28
29  ```ts
30  import statvfs from '@ohos.file.statvfs';
31  import { BusinessError } from '@ohos.base';
32  import common from '@ohos.app.ability.common';
33
34  let context = getContext(this) as common.UIAbilityContext;
35  let path = context.filesDir;
36  statvfs.getFreeSize(path, (err: BusinessError, number: number) => {
37    if (err) {
38      console.error(`Invoke getFreeSize failed, code is ${err.code}, message is ${err.message}`);
39    } else {
40      console.info(`Invoke getFreeSize succeeded, size is ${number}`);
41    }
42  });
43  ```
44
45- Obtain the space occupied by the current application.
46
47  ```ts
48  import storageStatistics from "@ohos.file.storageStatistics";
49  import { BusinessError } from '@ohos.base';
50
51  storageStatistics.getCurrentBundleStats((err: BusinessError, bundleStats: storageStatistics.BundleStats) => {
52    if (err) {
53      console.error(`Invoke getCurrentBundleStats failed, code is ${err.code}, message is ${err.message}`);
54    } else {
55      console.info(`Invoke getCurrentBundleStats succeeded, appsize is ${bundleStats.appSize}`);
56    }
57  });
58  ```
59