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.
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.
The limit is 16 MB on very old devices, 24 MB or 32 MB on newer ones.
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.
Finding a specific method memory usage is sort if not so possible but there is a small trick to get a probable estimate:
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With