2

I found that it can be easily done using java from this answer but couldn't find yet how to do it if my applicating is written using C#.NET and xamarin libraries. How to get those same datas (current heap size, maximum heap size) using this framework?

Thanks in advance.

Community
  • 1
  • 1
Fnr
  • 2,096
  • 7
  • 41
  • 76
  • Note that those values are not especially useful in Android app development. In particular, the amount of reported free heap space does not indicate how much you can allocate in a single request, as Android's heap suffers from fragmentation much of the time. – CommonsWare Jan 18 '17 at 15:32
  • I see, I need those datas anyway since it was requested for me.. but thanks – Fnr Jan 18 '17 at 15:35

2 Answers2

3

You can do this:

//get max heap available to app
var activityManager = (ActivityManager)activity.GetSystemService(Context.ActivityService);
int maxHeap = activityManager.MemoryClass*1024; //KB

//get current heap used by app
Runtime runtime = Runtime.GetRuntime();
int usedHeap = (int)((runtime.TotalMemory() - runtime.FreeMemory()) / 1024.0f); //KB

//get amount of free heap remaining for app
int availableHeap = maxHeap - usedHeap;
Steven Mark Ford
  • 3,372
  • 21
  • 32
1

I suppose you are looking for something like this:

long memory = GC.GetTotalMemory(true);

It retrieves the number of bytes currently thought to be allocated.

There is also another option:

Process currentProc = Process.GetCurrentProcess();
long memoryUsed = currentProc.PrivateMemorySize64;

You determine memory usage by reading the PrivateMemorySize64 property. You need using System.Diagnostics; reference to support this action.

hcerim
  • 959
  • 1
  • 11
  • 27