Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure the amount of memory webview consumes?

Would there be a way to programmatically measure and monitor the memory consumed by

WebView.loadUrl

when we open a webview activity in our Android app?

I have noticed that for some reason the webpage that I open using WebView.loadUrl has some memory leak and the amount of memory it uses keeps on increasing under random conditions. I wanted to programatically catch that and run some method if the memory usage of my WebView page goes beyond a certain range.

like image 963
C graphics Avatar asked May 21 '19 19:05

C graphics


People also ask

How do I discover memory usage of my application in android?

Tap Developer options and then tap Memory. In the resulting screen (Figure B), you'll see a list of the average memory used by the device in the past three hours (you can adjust the time frame, by tapping the time drop-down at the top). The Memory usage window in Android 12.

How much memory should an application use?

The limit is 16 MB on very old devices, 24 MB or 32 MB on newer ones.

What is the maximum memory limit given for each process or application in Android?

16 MB is the maximum memory limit given to any given android application. Some second generation phones will return 24 MB of memory for each process or even more.


1 Answers

Finding a specific method memory usage is sort if not so possible but there is a small trick to get a probable estimate:

  1. Run the following code and save it somewhere
  2. Run the webview.loadurl("...")
  3. Rerun the following code and subtract the new memory usage from previous to get an idea of how much memory the webview.loadUrl max use at maximum

    final Runtime runtime = Runtime.getRuntime(); final long usedMemInMB=(runtime.totalMemory() - runtime.freeMemory()) / 1048576L; final long maxHeapSizeInMB=runtime.maxMemory() / 1048576L; final long availHeapSizeInMB = maxHeapSizeInMB - usedMemInMB;

like image 99
Saswata Avatar answered Oct 06 '22 00:10

Saswata