Delphi Android应用程序的内存大小

9rnv2umw  于 2022-09-21  发布在  Android
关注(0)|答案(2)|浏览(270)

我正在使用Delphi 10.1.2柏林。我需要在Delphi Android应用程序项目中确定内存的大小,但即使我在谷歌上搜索,也没有找到一个例子。在这方面我需要你的帮助。

sbdsn5lh

sbdsn5lh1#

不确定它对Delphi Android应用程序有多大用处,但我在这里添加了Debug类的导入:

https://github.com/DelphiWorlds/KastriFree/blob/master/API/DW.Androidapi.JNI.Debug.pas

我假设这是要使用的相关类,因为Delphi生成本机可执行文件。以下是一些示例代码:

uses
  DW.Androidapi.JNI.Debug;

const
  cBytesPerMegabyte = 1024 * 1024;

procedure TForm1.ShowMemoryUse;
begin
  InfoMemo.Lines.Clear;
  InfoMemo.Lines.Add(Format('Total memory: %.2f MB', [TJDebug.JavaClass.getNativeHeapSize / cBytesPerMegabyte]));
  InfoMemo.Lines.Add(Format('Allocated memory: %.2f MB', [TJDebug.JavaClass.getNativeHeapAllocatedSize / cBytesPerMegabyte]));
  InfoMemo.Lines.Add(Format('Free memory: %.2f MB', [TJDebug.JavaClass.getNativeHeapFreeSize / cBytesPerMegabyte]));
end;
yrdbyhpb

yrdbyhpb2#

在Delphi 10和11中工作:

uses
  Androidapi.Helpers,
  Androidapi.JNIBridge,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.App;

var
  MemoryInfo: JActivityManager_MemoryInfo;
  TotalMb: Int64;
  AvailMb: Int64;
begin
  MemoryInfo:= TJActivityManager_MemoryInfo.JavaClass.init;
  TJActivityManager.Wrap((TAndroidHelper.Context.getSystemService(
    TJContext.JavaClass.ACTIVITY_SERVICE) as ILocalObject).GetObjectID)
    .getMemoryInfo(MemoryInfo);
  TotalMb:= MemoryInfo.totalMem;  // use shr 20 after totalmem to get MegaBytes
  AvailMb:= MemoryInfo.availMem;  // use shr 20 after availMem to get MegaBytes
end;

在Delphi XE中运行:

uses
  Androidapi.Helpers,
  Androidapi.JNIBridge,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.App;

function AndroidGetMemory: Int64;
var
  MemoryInfo: JActivityManager_MemoryInfo;
  TotalMb: Int64;
  AvailMb: Int64;
begin
  MemoryInfo:= TJActivityManager_MemoryInfo.JavaClass.init;

  TJActivityManager.Wrap((SharedActivityContext.getSystemService(
    TJContext.JavaClass.ACTIVITY_SERVICE) as ILocalObject).GetObjectID)
    .getMemoryInfo(MemoryInfo);
  TotalMb:= MemoryInfo.totalMem;  // use shr 20 after totalmem to get MegaBytes
  AvailMb:= MemoryInfo.availMem;  // use shr 20 after availMem to get MegaBytes
  result := AvailMb;
end;

相关问题