harmony 鸿蒙@ohos.hidebug (HiDebug)

  • 2022-08-09
  • 浏览 (771)

@ohos.hidebug (HiDebug)

NOTE

The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

The hidebug module provides APIs for you to obtain the memory usage of an application, including the static heap memory (native heap) and proportional set size (PSS) occupied by the application process. You can also export VM memory slices and collect VM CPU profiling data.

Modules to Import

import hidebug from '@ohos.hidebug';

hidebug.getNativeHeapSize

getNativeHeapSize(): bigint

Obtains the total heap memory size of this application.

System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug

Return value

Type Description
bigint Total heap memory size of the application, in bytes.

Example

  let nativeHeapSize: bigint = hidebug.getNativeHeapSize();

hidebug.getNativeHeapAllocatedSize

getNativeHeapAllocatedSize(): bigint

Obtains the allocated heap memory size of this application.

System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug

Return value

Type Description
bigint Allocated heap memory of the application, in bytes.

Example

  let nativeHeapAllocatedSize: bigint = hidebug.getNativeHeapAllocatedSize();

hidebug.getNativeHeapFreeSize

getNativeHeapFreeSize(): bigint

Obtains the free heap memory size of this application.

System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug

Return value

Type Description
bigint Free heap memory size of the application, in bytes.

Example

  let nativeHeapFreeSize: bigint = hidebug.getNativeHeapFreeSize();

hidebug.getPss

getPss(): bigint

Obtains the size of the physical memory actually used by the application process.

System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug

Return value

Type Description
bigint Size of the physical memory actually used by the application process, in KB.

Example

  let pss: bigint = hidebug.getPss();

hidebug.getSharedDirty

getSharedDirty(): bigint

Obtains the size of the shared dirty memory of a process.

System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug

Return value

Type Description
bigint Size of the shared dirty memory of the process, in KB.

Example

  let sharedDirty: bigint = hidebug.getSharedDirty();

hidebug.getPrivateDirty9+

getPrivateDirty(): bigint

Obtains the size of the private dirty memory of a process.

System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug

Return value

Type Description
bigint Size of the private dirty memory of the process, in KB.

Example

  let privateDirty: bigint = hidebug.getPrivateDirty();

hidebug.getCpuUsage9+

getCpuUsage(): number

Obtains the CPU usage of a process.

For example, if the CPU usage is 50%, 0.5 is returned.

System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug

Return value

Type Description
number CPU usage of the process.

Example

  let cpuUsage: number = hidebug.getCpuUsage();

hidebug.getServiceDump9+

getServiceDump(serviceid : number, fd : number, args : Array<string>) : void

Obtains system service information.

Required permissions: ohos.permission.DUMP

System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug

Parameters

Name Type Mandatory Description
serviceid number Yes Obtains the system service information based on the specified service ID.
fd number Yes File descriptor to which data is written by the API.
args Array<string> Yes Parameter list corresponding to the Dump API of the system service.

Error codes

For details about the error codes, see HiSysEvent Error Codes.

ID Error Message
11400101 the service id is invalid
401 the parameter check failed

Example

import fs from '@ohos.file.fs'
import hidebug from '@ohos.hidebug'
import common from '@ohos.app.ability.common'
import { BusinessError } from '@ohos.base'

let applicationContext: common.Context|null = null;
try {
  applicationContext = this.context.getApplicationContext();
} catch (error) {
  console.info(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
}

let filesDir: string = applicationContext!.filesDir;
let path: string = filesDir + "/serviceInfo.txt";
console.info("output path: " + path);
let file = fs.openSync(path, fs.OpenMode.READ_WRITE|fs.OpenMode.CREATE);
let serviceId: number = 10;
let args: Array<string> = new Array("allInfo");

try {
  hidebug.getServiceDump(serviceId, file.fd, args);
} catch (error) {
  console.info(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
}
fs.closeSync(file);

hidebug.startJsCpuProfiling9+

startJsCpuProfiling(filename : string) : void

Starts the profiling method. startJsCpuProfiling() and stopJsCpuProfiling() are called in pairs. startJsCpuProfiling() always occurs before stopJsCpuProfiling(); that is, calling the functions in the sequence similar to the following is prohibited: start->start->stop, start->stop->stop, and start->start->stop->stop.

System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug

Parameters

Name Type Mandatory Description
filename string Yes User-defined profile name. The filename.json file is generated in the files directory of the application based on the specified filename.

Error codes

For details about the error codes, see HiSysEvent Error Codes.

ID Error Message
401 the parameter check failed

Example

import hidebug from '@ohos.hidebug'
import { BusinessError } from '@ohos.base'

try {
  hidebug.startJsCpuProfiling("cpu_profiling");
  // ...
  hidebug.stopJsCpuProfiling();
} catch (error) {
  console.info(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
}

hidebug.stopJsCpuProfiling9+

stopJsCpuProfiling() : void

Stops the profiling method. startJsCpuProfiling() and stopJsCpuProfiling() are called in pairs. startJsCpuProfiling() always occurs before stopJsCpuProfiling(); that is, calling the functions in the sequence similar to the following is prohibited: start->start->stop, start->stop->stop, and start->start->stop->stop.

System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug

Parameters

Name Type Mandatory Description
filename string Yes User-defined profile name. The filename.json file is generated in the files directory of the application based on the specified filename.

Example

import hidebug from '@ohos.hidebug'
import { BusinessError } from '@ohos.base'

try {
  hidebug.startJsCpuProfiling("cpu_profiling");
  // ...
  hidebug.stopJsCpuProfiling();
} catch (error) {
  console.info(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
}

hidebug.dumpJsHeapData9+

dumpJsHeapData(filename : string) : void

Exports the heap data.

System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug

Parameters

Name Type Mandatory Description
filename string Yes User-defined heap file name. The filename.heapsnapshot file is generated in the files directory of the application based on the specified filename.

Error codes

For details about the error codes, see HiSysEvent Error Codes.

ID Error Message
401 the parameter check failed

Example

import hidebug from '@ohos.hidebug'
import { BusinessError } from '@ohos.base'

try {
  hidebug.dumpJsHeapData("heapData");
} catch (error) {
  console.info(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
}

hidebug.startProfiling(deprecated)

startProfiling(filename : string) : void

NOTE
This API is deprecated since API version 9. You are advised to use hidebug.startJsCpuProfiling.

Starts the profiling method. startProfiling() and stopProfiling() are called in pairs. startProfiling() always occurs before stopProfiling(); that is, calling the functions in the sequence similar to the following is prohibited: start->start->stop, start->stop->stop, and start->start->stop->stop.

System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug

Parameters

Name Type Mandatory Description
filename string Yes User-defined profile name. The filename.json file is generated in the files directory of the application based on the specified filename.

Example

hidebug.startProfiling("cpuprofiler-20220216");
// code block
// ...
// code block
hidebug.stopProfiling();

hidebug.stopProfiling(deprecated)

stopProfiling() : void

NOTE
This API is deprecated since API version 9. You are advised to use hidebug.stopJsCpuProfiling.

Stops the profiling method. startProfiling() and stopProfiling() are called in pairs. startProfiling() always occurs before stopProfiling(); that is, calling the functions in the sequence similar to the following is prohibited: start->start->stop, start->stop->stop, and start->start->stop->stop.

System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug

Example

hidebug.startProfiling("cpuprofiler-20220216");
// code block
// ...
// code block
hidebug.stopProfiling();

hidebug.dumpHeapData(deprecated)

dumpHeapData(filename : string) : void

NOTE
This API is deprecated since API version 9. You are advised to use hidebug.dumpJsHeapData.

Exports the heap data.

System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug

Parameters

Name Type Mandatory Description
filename string Yes User-defined heap file name. The filename.heapsnapshot file is generated in the files directory of the application based on the specified filename.

Example

hidebug.dumpHeapData("heap-20220216");

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞