1

I am using Delphi 10.1.2 Berlin. I need to determine the size of the internal memory in a Delphi Android application project, but I have not found an example even though I searched on Google. I need your help in this regard.

Codder71
  • 23
  • 1
  • 9
  • Not sure how useful it might be to Delphi Android apps, however there's a gist here of some Java code: https://gist.github.com/loopj/2821941, and I've added an import of the Debug class here: https://github.com/DelphiWorlds/KastriFree/blob/master/API/DW.Androidapi.JNI.Debug.pas – Dave Nottage Jan 07 '18 at 01:29
  • tanks but I could not make a conclusion from them. :( – Codder71 Jan 07 '18 at 19:50

2 Answers2

0

Not sure how useful it might be to Delphi Android apps, however I've added an import of the Debug class here:

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

I'm assuming this is the relevant class to use, since Delphi generates native executables. Here's some example code:

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;
Dave Nottage
  • 3,411
  • 1
  • 20
  • 57
  • thank you for your reply but it shows the wrong values. Some information from MB is incorrect, unfortunately :( – Codder71 Jan 07 '18 at 22:31
  • I wasn't sure that it would be accurate anyway, however I am curious: how do you know the values are wrong? i.e. what are you comparing them against? – Dave Nottage Jan 07 '18 at 23:29
  • wrong because the total memory of my device is 32 gb, the information returned from the application is 20 mb and the values change when I open or close an application. I think they are about the ram. – Codder71 Jan 08 '18 at 21:13
  • 1
    The figures provided by those calls are for the app (which is what you asked for in your question), not for the whole phone – Dave Nottage Jan 09 '18 at 02:44
  • But I need the whole phone. How can I do that? Is there a way? Note: I apologize for my bad english :) – Codder71 Jan 09 '18 at 21:35
  • Perhaps use [this Java code](https://stackoverflow.com/a/32094165/2817399) as a template, and translate to Delphi accordingly? – blong Jan 30 '18 at 20:33
  • Documentation of the helper object you get back is [here](https://d.android.com/reference/android/app/ActivityManager.MemoryInfo.html) and you'll find it in the Delphi RTL unit Androidapi.Jni.App.pas. – blong Jan 30 '18 at 20:39
  • @blong If Codder71 is referring to a value of 32GB, I suspect it's storage space, not available memory. This link might have a more complete answer: https://stackoverflow.com/questions/4799643/getting-all-the-total-and-available-space-on-android, however there's going to be some Java2OP work to be done to import the relevant classes. – Dave Nottage Jan 30 '18 at 21:02
  • @DaveNottage, in the answer I linked there was a RAM part and a storage part. The storage part gives *a* number (again with a requirement to import the [`StatFs`](https://dandroid.com/reference/android/os/StatFs.html) type). Just FYI BTW JIC FWIW. Granted your linked answer has more options and is ∴ fuller. – blong Jan 30 '18 at 21:25
0

Works in Delphi 10 and 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;

Works in 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;
Greg T
  • 122
  • 7