Finding the amount of memory that can be used in a WebView

I'm currently developing an application that features a WebView running JavaScript code. This particular JavaScript code tends to be memory-intensive and can sometimes exceed the allotted memory, leading to crashes in the WebView's Chromium process and ultimately causing my app to crash.

Despite implementing listeners for onMemoryTrim in my app, I've noticed that it is not triggered on devices with more than 1GB of memory, even when reaching TRIM_MEMORY_RUNNING_LOW.

My main concern now is finding a way to detect when the WebView is running low on memory so that I can either terminate it or prompt it to free up memory. I attempted to monitor memory usage using performance.memory, but this approach was unsuccessful as executing a similar script within the WebView resulted in crashes:

var a = [];

var kek = () => {
  var b = [];
  for(var i = 0; i < 1024 * 1024 * 2; i++) b.push(Math.random());
  return b;
}

var ival = setInterval(() => {
  let m = performance.memory;
  if(m.jsHeapSizeLimit - m.usedJSHeapSize < 1e5) {
    console.log("Memory limited")
  } else {
    a.push(kek());
  }
});

Is there a reliable method to anticipate when memory is about to be exhausted so that I can address this issue proactively and prevent app crashes?

Answer №1

After consulting with both the Chromium and Android teams, it appears that they have confirmed what I suspected all along - this situation is deemed impossible.

Occasionally, the memory demands surpass JavaScript's capacity, leading to crashes in the WebView process of Chromium which subsequently crashes my application.

Fortunately, in Android 8.0 and above, it is possible to handle out of memory crashes using the new termination handle API. This workaround bypasses the need to constantly monitor available memory ahead of time.

By implementing the onRenderProcessGone method override, we are able to capture this issue and reconstruct the WebView accordingly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Presenting Invoice Information on an Invoice Template: A Step-by-Step Guide

Currently, I am working with Laravel 5.7 and VueJs 2.5.*, where I have a table of invoices. My goal is to create a new component that will display a specific invoice based on user selection, allowing them to view or print the chosen invoice. I am still ex ...

Failing to reach the nested if statements within a switch case block

Before removing my question, please read this. Despite testing with console.logs, my code does not enter the if statements. I have not come across a similar solution to my issue. In an attempt to address any timing or asynchronous problems, I added a use ...

Triggering a dynamically created event with the onchange event

I'm currently working on creating an input type file, and here is the code I have: var element = document.createElement("INPUT"); element.type = "file"; element.id = "element" + i; $("#media").append("<br>"); $("#media").append("<br>"); $ ...

When implementing Passport-jwt for fetching user data, req.user may sometimes be undefined

No matter how many answers I search for on Stackoverflow or read through the documentation, I still can't solve my problem. Signing in and signing up works perfectly fine - I have my token. But when I try to fetch my current_user using get('/isAu ...

Programmatically changing tabs using SlidingTabStrip and ViewPager

I'm encountering difficulties when attempting to switch SlidingTabStrip tabs programmatically. My approach involves using SlidingTabStrip in conjunction with viewpager. The code snippet I am currently employing is: mSlidingTabs.setViewPager(mPager) ...

Points in an array being interpolated

I am currently working with data points that define the boundaries of a constellation. let boundaries = [ { ra: 344.46530375, dec: 35.1682358 }, { ra: 344.34285125, dec: 53.1680298 }, { ra: 351.45289375, ...

The functionality of JSON.stringify does not take into account object properties

Check out the example on jsfiddle at http://jsfiddle.net/frigon/H6ssq/ I have encountered an issue where JSON.stringify is ignoring certain fields. Is there a way to make JSON.stringify include them in the parsing? In the provided jsfiddle code... <s ...

I'm looking for a way to retrieve mnist data using DataSetIterator in DL4J for Android

package com.example.minwoo_k.neural_network; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator; import or ...

Tips on utilizing jQuery or JavaScript to efficiently filter out outcomes exceeding a specified range

<input type="range" name="rangeInput" min="100000" max="750000" onchange="updateTextInput(this.value);"> Displayed above is the slider code that provides a value output below. <div id="sliderValue"> <p>Max Value £</p> <input t ...

Retrieve the file for saving using the HttpPost method in Asp.Net MVC

In my Asp.Net MVC project, there is a page where users can edit data loaded into a table, such as changing images, strings, and the order of items. Once all edits have been made, the client clicks on a Download button to save the resulting xml-file on the ...

Making a request using AJAX to retrieve data from an API

Looking to create an API GET request using ajax? Want a jquery function that takes an api key from the first input field and displays a concatenated result on the second input field (#2)? The goal is to make the get request to the value shown in the 2nd ...

Incorporate jQuery on elements that are dynamically loaded after the page has finished loading

I need to determine if a dynamically added button is enabled or disabled. How can this be achieved? My goal is to display a message when the button is disabled and remove it when the button becomes enabled. This is my current code: jQuery(document).read ...

Vue-based bot for telegram web application

Hey there, I've been working on integrating a web app with my chat bot, taking advantage of the new Telegram feature. Unfortunately, after adding the site, I'm encountering an issue where clicking the button opens up an empty page. It seems that ...

Encountering a problem with the message "Unable to resolve the type com.google.common.base.Function."

I recently integrated the latest version of Selenium (4.10.0) into my automation test code. Here is the pom.xml file for the parent project: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSch ...

The use of getString to access an if statement within a Json object is not functioning as expected

I am facing an issue while comparing two strings in the URL response. When it is executed, it directly goes to the "else" statement without executing the "if" statement. if(response.getString("status").equals("success") Here is my JSON: {"status":"succe ...

Display Content in a DIV When Form Field is Unfocused

After hours of searching, I still haven't found a solution! I am trying to create a form field (text) where users can type in text. I want the text they enter to appear in a div on the screen either as they type or after they finish typing. Maybe thi ...

Using the Observable/Subscriber pattern within an AsyncTask

My goal is to incorporate an Observable/Subscriber using RxJava within the onPostExecute() method of an AsyncTask, but I'm struggling to establish the connection. I am creating the Observable in the onPostExecute method and I want MyFragment to subscr ...

Extracting Vertices, Edges, Faces, and Triangles from GLB Model Using ThreeJS GLTFLoader

I am looking to incorporate basic information about the model into my model viewer. Specifically, I need to obtain the vertex, edge, face, and triangle count of the object, or at the very least, the vertex count. My attempted approach involves using the f ...

Learn how to create a broadcast receiver with alarm manager to send text to a server every 10 seconds

package com.frost.mqtttutorial; import android.Manifest; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.location.Location; import ...

Dealing with AngularJS memory leaks caused by jQuery

How can I showcase a custom HTML on an AngularJS page using the given service? app.service('automaticFunctions', function ($timeout) { this.init = function initAutomaticFunctions(scope, $elem, attrs) { switch (scope.content.type) { ...