Learn to get Memory Info at runtime on Android

Written by ssaurel | Published 2017/09/05
Tech Story Tags: android-app-development | programming | android-apps | androiddev | android

TLDRvia the TL;DR App

In that tutorial, you are going to discover how to get Memory Information concerning a smartphone or tablet on Android.

In an Android application, you can get information concerning :

  • The available memory on the system
  • The total memory accessible by the kernel
  • The maximum number of bytes the heap can expand to
  • The current number of bytes taken by the heap
  • The current number of those bytes actually used by live objects

These information can be retrieved via two objects of the Android Standard SDK : MemoryInfo and Runtime.

Note that you can discover this tutorial in video on YouTube :

User Interface

First, we need to create a simple user interface to display the memory information on the screen. This UI will consist of a TextView centered on the main layout.

Get the Memory Info

Now, we can write the Java code of the Main Activity to get Memory Information. First, we create a MemoryInfo object in which we load the memory information by calling the getMemoryInfo method of the ActivityManager system service.

Then, we get the instance of the Runtime object by calling the getRuntime method of the Runtime object. Last step is just to format all these information and return that to display them on the screen.

That gives us the following code :

<a href="https://medium.com/media/34e5078a2e6f109184715b00a5645268/href">https://medium.com/media/34e5078a2e6f109184715b00a5645268/href</a>

By running the application, we can enjoy the memory information displayed on the screen :

That’s all for that tutorial. To discover more tutorials on Android Development, don’t hesitate to subscribe to the SSaurel’s Channel.

Sylvain Saurel

You can also visit the SSaurel’s Blog : https://www.ssaurel.com/blog


Written by ssaurel | Bitcoiner. Freedom Maximalist. Author of "The Truth About Bitcoin". Editor, In Bitcoin We Trust Newsletter.
Published by HackerNoon on 2017/09/05