鸿蒙开发5.0【Debug调试】运维

Debug

介绍

本示例通过使用@ohos.hidebug接口来获取当前应用内存使用情况。

)效果预览

1

使用说明

1.点击界面右上方Debug图标,弹出Debug信息界面,显示当前应用内存使用情况;

2.再次点击右上方Debug图标,Debug信息界面消失。

具体实现

  • 本实例获取Debug信息的功能结主要封装在Index,源码参考:[Index.ets]。
/*
 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { hidebug } from '@kit.PerformanceAnalysisKit';
import DebugInfo from '../component/DebugInfo';

@Entry
@Component
struct Index {
  @State pssMemory: number = 0;
  @State sharedMemory: number = 0;
  @State privateMemory: number = 0;
  @State debugWindow: boolean = false;

  build() {
    Column() {
      Row() {
        Text($r('app.string.MainAbility_label'))
          .margin(4)
          .fontSize(20)
          .fontColor(Color.White)
          .textAlign(TextAlign.Center)

        Blank()

        Image($r('app.media.debug'))
          .id('btnDebug')
          .width('10%')
          .margin({ right: 4 })
          .objectFit(ImageFit.Contain)
          .onClick(() => {
            this.debugWindow = !this.debugWindow
            if (this.debugWindow) {
              let pss = hidebug.getPss();
              this.pssMemory = Number(pss !== null ? pss : -1); // -1代表getPss()接口返回值错误
              let sharedDirty = hidebug.getSharedDirty();
              this.sharedMemory = Number(sharedDirty !== null ? sharedDirty : -1); // -1代表getSharedDirty()接口返回值错误
              let privateDirty = hidebug.getPrivateDirty();
              this.privateMemory = Number(privateDirty !== null ? privateDirty : -1); // -1代表getPrivateDirty()接口返回值错误
            }
          })
      }
      .height('6%')
      .width('100%')
      .padding({ right: 10 })
      .backgroundColor('#0D9FFB')
      .constraintSize({ minHeight: 50 })

      // 显示Debug信息窗口
      DebugInfo({
        pssMemory: $pssMemory,
        sharedMemory: $sharedMemory,
        debugWindow: this.debugWindow,
        privateMemory: $privateMemory
      })
    }
    .width('100%')
    .height('100%')
  }
}
  • 获取Debug信息:在Index页面中通过点击事件调用hidebug.getPss()、hidebug.getSharedDirty()、hidebug.getPrivateDirty()等方法能够获取应用进程实际使用的物理内存大小、进程的共享脏内存大小、进程的私有脏内存大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值