Android WebView - JavaScript Memory Leak - java

I am having an issue with Javascript in WebView (enabled using webview.getSettings().setJavascriptEnabled(true); ) The HTML pages with embedded Javascript loads and displays properly, however, what I am also doing is constantly refreshing the screen every 10 or 30 seconds. When I go into the shell of my test device (MK808 mini-tv) I use "dumpsys meminfo" in order to see the memory of the processes. Displaying the memory of my app's process every 5 seconds I can see that whenever the page refreshes, the app's memory usage jump multiple (4-10) megabytes. This continues for around 30 minutes or so until the app crashes with a Fatal Signal 11 error in the WebViewCoreThread, with no stack trace.
Constant Increasing Memory (started at around 15000kb):
None of these memory issues or errors occur when javascript is NOT enabled.
I clearCache(true) and freeMemory(), as well as every other database and cache clear, every single time before I refresh.
I have also tried destroying the WebView after each refresh and creating a brand new one, to no avail.
I can easily remake this issue by just making a new Application project and making a webview that reloads based on a handler controlling time..This seems like it should be a widespread issue but I can't find anything on the subject. Could it be my device?
Thanks in advance for the help, I'll be glad to add more information as needed!

try to add this also :
//Injects the supplied Java object into this WebView. The object is injected into the
//JavaScript context of the main frame, using the supplied name. This allows the
//Java object's public methods to be accessed from JavaScript.
WebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

Related

Switch Browser to "sleep mode" (disabled) and switch back

I am trying to embed a website as a user interface into a game I'm making using JxBrowser. The problem is, keeping the browser running in the background causes the game's FPS drops from 120 to 20.
Since I need to render the BufferedImage from the browser, I don't think I can switch to HeavyWeight mode for that GPU-acceleration (Please correct me if I can).
The plan is, the browser is only active under certain circumstances (e.g. player is looking at the interface). I wonder if there's any way that I can disable the browser, and resume to previous state when needed?
P.S. I do notice the browser.dispose() method, but that won't resume the browser to the previous state before disabled.
JxBrowser doesn't provide any API to "disable" the browser.
JxBrowser uses CPU to perform the off-screen rendering in the lightweight mode. Every time something changes on the loaded document(e.g. DOM updates, etc.) JxBrowser re-renders the web page content in memory.
Probably, this is the root-cause of the FPS decreasing.
I suggest that you find the way to display more statical content in the user interface.
Also, please take a look at the following article, maybe an accelerated rendering helps reduce this issue:
https://jxbrowser.support.teamdev.com/support/solutions/articles/9000104965-accelerated-lightweight-rendering

GWT load fragments after application start

I am creating an app in GWT, and I just recently implemented code splitting there. I reduced the application size from 1.1MB to 570kB which is nice, so the startup time of the application is now faster (we are using special server where 500kB really matters... not important for my question though ...). After the application starts (in other words user can see login page, can login and use basic parts of the app), I would like to download the rest of the fragments.
I know the fragment will be downloaded when the code in the fragment is needed. But one of the fragments is about 300kB. So when I click in the menu of my app on an item, that causes this fragment to be downloaded, there is a very noticeable delay (1 - 2s), before the user gets a response.
Now I understand that this will most likely happen only once and then the fragment will be cached for like a year, so it will load faster next time. But for example when I try it again on another device, I will have to download fragment for the first time again.
I just need to be sure, that when user launches my app on a phone/tablet using wifi, then disconnects from the wifi or gets out of its range, he will still be able to launch the code in the fragments, even if he didn't launch the things that cause downloading them while he was still connected to the server.
Now he would have to open 3 menu items, to download all the fragments which is annoying.
So in short:
I want my initial download to stay 570kB, and download the rest of the app as soon as possible on the background (if possible).
EDIT:
I found http://www.gwtproject.org/doc/latest/DevGuideCodeSplitting.html#sequence where you can setup initial loading sequence, so I guess it is what I am looking for. Not sure though if they are loaded asynchronously, because the login screen appears after the fragments are downloaded.
You can cause all fragments to be downloaded after the login panel is rendered. Simply call some method inside the rest of the code - it does not need to do something visible.
Also, if you plan to load all code this way, you only need to split one fragment - your entry point with the login panel. Each split point (a) slightly increases the overall size of the application, and (b) increases, sometimes significantly, the size of the leftover fragment which still needs to be loaded for the first fragment to show up. Thus, there is no point in having more than the initial fragment and the rest of the app, given your requirements.

I/Choreographer: Skipped x frames! The application may be doing too much work on its main thread [duplicate]

I am new to Android SDK/API environment. It's the first I am trying to draw a plot/chart. I tried running different kinds of sample codes on the emulator using 3 different free libraries, nothing is showing on the layout screen. The logcat is repeating the following message:
W/Trace(1378): Unexpected value from nativeGetEnabledTags: 0
I/Choreographer(1378): Skipped 55 frames! The application may be doing too much work on its main thread.
The problem didn't persist and the chart worked when I ran a sample code pertaining to an evaluation copy of a licensed library.
taken from : Android UI : Fixing skipped frames
Anyone who begins developing android application sees this message on
logcat “Choreographer(abc): Skipped xx frames! The application may be
doing too much work on its main thread.” So what does it actually
means, why should you be concerned and how to solve it.
What this means is that your code is taking long to process and frames
are being skipped because of it, It maybe because of some heavy
processing that you are doing at the heart of your application or DB
access or any other thing which causes the thread to stop for a while.
Here is a more detailed explanation:
Choreographer lets apps to connect themselves to the vsync, and
properly time things to improve performance.
Android view animations internally uses Choreographer for the same
purpose: to properly time the animations and possibly improve
performance.
Since Choreographer is told about every vsync events, I can tell if
one of the Runnables passed along by the Choreographer.post* apis
doesnt finish in one frame’s time, causing frames to be skipped.
In my understanding Choreographer can only detect the frame skipping.
It has no way of telling why this happens.
The message “The application may be doing too much work on its main
thread.” could be misleading.
source :
Meaning of Choreographer messages in Logcat
Why you should be concerned
When this message pops up on android
emulator and the number of frames skipped are fairly small (<100) then
you can take a safe bet of the emulator being slow – which happens
almost all the times. But if the number of frames skipped and large
and in the order of 300+ then there can be some serious trouble with
your code. Android devices come in a vast array of hardware unlike ios
and windows devices. The RAM and CPU varies and if you want a
reasonable performance and user experience on all the devices then you
need to fix this thing. When frames are skipped the UI is slow and
laggy, which is not a desirable user experience.
How to fix it
Fixing this requires identifying nodes where there is or
possibly can happen long duration of processing. The best way is to do
all the processing no matter how small or big in a thread separate
from main UI thread. So be it accessing data form SQLite Database or
doing some hardcore maths or simply sorting an array – Do it in a
different thread
Now there is a catch here, You will create a new Thread for doing
these operations and when you run your application, it will crash
saying “Only the original thread that created a view hierarchy can
touch its views“. You need to know this fact that UI in android can be
changed by the main thread or the UI thread only. Any other thread
which attempts to do so, fails and crashes with this error. What you
need to do is create a new Runnable inside runOnUiThread and inside
this runnable you should do all the operations involving the UI. Find
an example here.
So we have Thread and Runnable for processing data out of main Thread,
what else? There is AsyncTask in android which enables doing long time
processes on the UI thread. This is the most useful when you
applications are data driven or web api driven or use complex UI’s
like those build using Canvas. The power of AsyncTask is that is
allows doing things in background and once you are done doing the
processing, you can simply do the required actions on UI without
causing any lagging effect. This is possible because the AsyncTask
derives itself from Activity’s UI thread – all the operations you do
on UI via AsyncTask are done is a different thread from the main UI
thread, No hindrance to user interaction.
So this is what you need to know for making smooth android
applications and as far I know every beginner gets this message on his
console.
As others answered above, "Skipped 55 frames!" means some heavy processing is in your application.
For my case, there is no heavy process in my application. I double and triple checked everything and removed those process I think was a bit heavy.
I removed Fragments, Activities, Libraries until only the skeleton was left. But still the problem did not go away. I decided to check the resources and found some icons and background I use are pretty big as I forgot to check the size of those resources.
So, my suggestion is if none of the above answers help, you may also check your resource files size.
I too had the same problem.
Mine was a case where i was using a background image which was in drawables.That particular image was of approx 130kB and was used during splash screen and home page in my android app.
Solution - I just shifted that particular image to drawables-xxx folder from drawables and was able free a lot of memory occupied in background and the skipping frames were no longer skipping.
Update Use 'nodp' drawable resource folder for storing background drawables
files.
Will a density qualified drawable folder or drawable-nodpi take precedence?
Another common cause of delays on UI thread is SharedPreferences access. When you call a PreferenceManager.getSharedPreferences and other similar methods for the first time, the associated .xml file is immediately loaded and parsed in the same thread.
One of good ways to combat this issue is triggering first SharedPreference load from the background thread, started as early as possible (e.g. from onCreate of your Application class). This way the preference object may be already constructed by the time you'd want to use it.
Unfortunately, sometimes reading a preference files is necessary during early phases of startup (e.g. in the initial Activity or even Application itself). In such cases it is still possible to avoid stalling UI by using MessageQueue.IdleHandler. Do everything else you need to perform on the main thread, then install the IdleHandler to execute code once your Activity have been fully drawn. In that Runnable you should be able to access SharedPreferences without delaying too many drawing operations and making Choreographer unhappy.
I had the same problem. Android Emulator worked perfectly on Android < 6.0. When I used emulator Nexus 5 (Android 6.0), the app worked very slow with I/Choreographer: Skipped frames in the logs.
So, I solved this problem by changing in Manifest file hardwareAccelerated option to true like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application android:hardwareAccelerated="true">
...
</application>
</manifest>
Update Jan 2022. According to the comment from #M.Ed: Hardware acceleration is enabled by default if you're targeting APIs >= 14.
Try to use the following strategies in order to improve your app performance:
Use multi-threading programming if possible. The performance benefits are huge, even if your smart phone has one core (threads can run in different cores, if the processor has two or more). It's useful to make your app logic separated from the UI. Use Java threads, AsyncTask or IntentService. Check this.
Read and follow the misc performance tips of Android development website. Check here.
I am not an expert, but I got this debug message when I wanted to send data from my android application to a web server. Though I used AsyncTask class and did the data transfer in background, for getting the result data back from server I used get() method of the AsyncTask class which makes the UI synchronous which means that your UI will be waiting for too long. So my advice is to make your app do every network oriented tasks on a separate thread.
I had the same problem. In my case I had 2 nested Relative Layouts. RelativeLayout always has to do two measure passes. If you nest RelativeLayouts, you get an exponential measurement algorithm.
Optimize your images ... Dont use images larger than 100KB ... Image loading takes too much CPU and cause your app hangs .
this usually happens when you are executing huge processes in main thread. it's OK to skip frames less than 200. but if you have more than 200 skipped frames, it can slow down your application UI thread. what you can do is to do these processes in a new thread called worker thread and after that, when you want to access and do something with UI thread(ex: do something with views, findView etc...) you can use handler or runOnUiThread(I like this more) in order to display the processing results.
this absolutely solves the problem. using worker threads are very useful or even must be used when it comes to this cases.
https://stacklearn.ir
I had the same problem. When I ran the code on another computer, it worked fine. On mine, however, it displayed "The application may be doing too much work on its main thread".
I solved my problem by restarting Android studio [File -> Invalidated caches / Restart -> click on "Invalidate and Restart"].
My app had same problem. But it was not doing other than displaying list of cards and text on it. Nothing running in background. But then after some investigation found that the image set for card background was causing this, even though it was small(350kb). Then I converted the image to 9patch images using
http://romannurik.github.io/AndroidAssetStudio/index.html.
This worked for me.
In my case, it was because I had accidentally set a breakpoint on a method. Once I cleared it, the message went away and performance improved a lot.
As I did first preferably use SVG images instead of all other types, If not possible compress all of your PNG and JPG resources using some image processing tools such as Adobe Photoshop or Fotosizer. one of the easiest ways is online image compressing tools like this which helped me to decrease all my image files to almost 50% of their initial size.
This is actually not a problem. This happens when you have the debugger for a long time. Remove the brake point and check again.
I got same issue while developing an app which uses a lot of drawable png files on grid layout. I also tried to optimize my code as far as possible.. but it didn't work out for me.. Then i tried to reduce the size of those png.. and guess its working absolutely fine.. So my suggestion is to reduce size of drawable resources if any..
After doing much R&D on this issue I got the Solution,
In my case I am using Service that will run every 2 second and with the runonUIThread, I was wondering the problem was there but not at all.
The next issue that I found is that I am using large Image in may App and thats the problem.
I removed the Images and set new Images.
Conclusion :- Look into your code is there any raw file that you are using is of big size.
First read the warning. It says more load on main thread. So what you have to do is just run functions with more work in a thread.
Have not resolved yet but will do. For my tiny project with one composable function (button) and logic to check if "com.whatsapp" packages exists on device (emulator) i have the following in the same log while starting simulator:
I/Choreographer: Skipped 34 frames! The application may be doing too much work on its main thread.
For me that was RoundedBackgroundColorSpan ! in textview, I remove it so (burn my brain to find it because It doesn't appear in real smartphones like Pixel 4 Xl or Samsung note 10+ also in emulator but in chip device this slow a view).
This is normal if you are using async/await functionalities in your application.

Chrome stucks after 30 or 40 minutes of work

In my tests chrome stucks after 30 or 40 minutes of work with error:
[710.758][SEVERE]: Unable to receive message from renderer
My current functionally is to take some txt data from web page and add it to inputs using executeScript. Can someone to help with this issue? Or just tell me correct way to resolve this problem
As far as I can remember, Chrome uses its own task manager... it can be opened by pressing shift+esc, assuming you don't have another program hooking that hotkey pair. Open the task manager and monitor the different tasks to see which one is bogging.
(couldn't leave a comment with low rep)
In my experience, this kind of error happens when you have something on the browser side which is preventing the part of Selenium that lives in the browser from doing its work. Whenever I've seen it, it was because some code went into an infinite loop. This would prevent the JavaScript engine from giving control to anything else and would prevent Selenium from being able to poll the renderer.
Besides the message Unable to receive message from renderer the other symptom would be a relatively high CPU usage from the test browser. Linux was pretty good about preventing the runaway browser from completely hosing my machine. I had to take a look specifically at the browser's processes to see the problem.

restart java.exe from an applet

I have an applet packaged with a third part dll (from JTwain). My applet scans documents from the TWAIN compatible default printer. The applet fails on a paper jam and won't recover. The user navigates away from the page and the applet is destroyed. When returning to the page it fails again. Closing the browser (which kills java.exe process on the pc), and then returning to the page clears the problem and everything works.
I want to restart everything without requiring users to close down the browser. I've added a GUID query string to the URL's from which the applets resources are loaded - so I know nothing is being cached. I've checked in the windows task manager and there is no process created by the dll, it's all happening within the main java.exe process. I tried wrapping the scanning process in a thread so I could interrupt it in the stop or destroy methods (just in case the applets thread weren't stopped when the applet was destroyed), but that didn't work.
Any suggest would be greatly appreciated. Ideally I'd like some way to restart java when the applet unloads (but I doubt that's possible).
UPDATE
I've spent a couple of days trying to identify what causes the applet to fail. I still don't know :(
When the paper jam occurs something (not my code), is producing a couple of popups. The first alerts the user of the jam, and can be closed by clicking the OK button. The second says 'reading from device' and hangs. It cannot be close with the red, close window, icon in the top corner - I kill it from the task manager and windows asks to send a report regarding the 'non-responsive program'. I assume these popups are produced by the dll. And given that the second hangs, my assumption is that a thread started by the dll has hung while retaining a lock on some component of the TWAIN application. I get
com.asprise.util.jtwain.JTwainException: Failed to open the specified data source:
Source: TW-Brother MFC-9970CDW LAN Thrown
..when I try to access the scanner.
I'm at a bit of a loss as to how I can get more information. I'm testing my applet on a windows virtual pc (so as to use ie7), and don't have a method for step debugging in this environment. (And it's crashing on third party code for which I have no source anyway)
I see only two practical options here:
Use an API that handles paper jam without problems. Of course, that is easy to say (get robust API), harder to find.
Launch the app. free floating using Java Web Start. If it freezes up, the user can kill it and click the link for another instance in a new JVM. Or the applet might also call BasicService.showDocument(URLof.jnlp) if it can detect a problem with the DLL and is not itself frozen.
Of course, you should also report the bug to the ..Asprise(?) developers. The optimal solution would be to have the problem fixed at its source. Anything we do here is a 'workaround'.

Categories

Resources