harmony 鸿蒙@ohos.hidebug (HiDebug)

2022-08-09 浏览 (961)

@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

TypeDescription
bigintTotal 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

TypeDescription
bigintAllocated 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

TypeDescription
bigintFree 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

TypeDescription
bigintSize 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

TypeDescription
bigintSize 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

TypeDescription
bigintSize 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

TypeDescription
numberCPU 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

NameTypeMandatoryDescription
serviceidnumberYesObtains the system service information based on the specified service ID.
fdnumberYesFile descriptor to which data is written by the API.
argsArray<string>YesParameter list corresponding to the Dump API of the system service.

Error codes

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

IDError Message
11400101the service id is invalid
401the 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

NameTypeMandatoryDescription
filenamestringYesUser-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.

IDError Message
401the 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

NameTypeMandatoryDescription
filenamestringYesUser-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

NameTypeMandatoryDescription
filenamestringYesUser-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.

IDError Message
401the 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

NameTypeMandatoryDescription
filenamestringYesUser-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

NameTypeMandatoryDescription
filenamestringYesUser-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)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/6a981327905b43d8b67a2e6f1ce6b6d4