如何在结束阶段后清除Javafx Webview内存使用情况 [英] How To Clear Javafx Webview Memory usage after closing stage

查看:778
本文介绍了如何在结束阶段后清除Javafx Webview内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在JavaFX中使用webview来创建UI,但是有一个问题,当使用弹出窗口打开大图像时,内存使用量非常大,并且当弹出窗口关闭时,内存使用量不会下降,我通过Windows中的任务管理器看到内存使用情况,

I tried to make the UI by using webview in JavaFX, but there is one problem, when a large image is opened using popup, memory usage is very large, and when the popup is closed, the memory usage does not go down, I see the memory usage via the Task Manager in Windows ,

当使用webview打开大小约为8MB的图像时,内存在使用中达到300MB,我不知道明白为什么这么大,

when the image with a size of approximately 8MB opened using webview, memory reached 300MB in use, I do not understand why it is so large,

我试过

stage.setOnCloseRequest(new EventHandler<WindowEvent>() {

        @Override
        public void handle(WindowEvent event) { 
            webengine.load(null);   

        }
    });

但仍然不要释放内存

推荐答案

您可能正在看一个的实例RT-33729 WebView泄漏WCImgDecoderImpl实例,目前针对Java 8u20进行修复。在RT-33729中,开发人员(Leonid Popov)写道:

Possibly, you are seeing an instance of RT-33729 WebView leaks WCImgDecoderImpl instances, currently targeted to be fixed for Java 8u20. In RT-33729, a developer (Leonid Popov) writes:


Webkit具有用于网页的资源缓存,包括图像。
它继续保留未使用(死)资源,即使它们不再是
引用,以防用户按下返回以避免
加载已经加载的资源,而它们不会不会留下太多的
堆空间。请参阅Webkit源代码中的注释中的详细信息:

Webkit has a cache of resources used for web pages, including images. It continues to keep unused ("dead") resources even after they are not referenced anymore just in case the user presses "Back" to avoid loading of already loaded resources, while they don't retain too much heap space. See details in comments in the Webkit source code:

http://trac.webkit.org/browser/trunk/Source/WebCore/loader/cache/MemoryCache.h

WCImgDecoderImpl的实例通过JNI引用
ImageSource由BitmapImage引用,由CachedImage的
引用存储在MemoryCache中。我通过修改缓存来测试它,以便
完全清除其缓存的资源,并观察到所有
WCImgDecoderImpl已经消失了。虽然可以从WebKit本机代码访问WCImgDecoderImpl
的实例,但它并非无用。点
是WebKit通过JNI来规定WCImgDecoderImpl
的生命周期。关于如何在WebView删除时强行刷新这个缓存
的调查仍在继续。

Instances of WCImgDecoderImpl are referenced through JNI by ImageSource's referenced by BitmapImage's referenced by CachedImage's stored in MemoryCache. I tested it with modifying the cache so that it completely swept out its cached resources, and observed that all WCImgDecoderImpl's have gone. While an instance of WCImgDecoderImpl can be accessed from WebKit native code, it's not useless. The point is that it is WebKit who rules the lifecycle of WCImgDecoderImpl through JNI. Investigation of how this cache could be flushed forcibly on WebView removal, is still going on.

从描述中谈论测试应用程序。当您从场景中删除
WebView时,它不会立即被删除(因此
不会释放任何资源)。相反,它有一个处置器在处理器线程(类似于AWT使用的那个)注册了
,应该在一定时间内将
调回来并导致
WebEngine使用本机资源来释放。

Talking about the test app from the description. When you remove a WebView from the scene, it doesn't get deleted immediately (and so it doesn't release any resources). Instead, it has a disposer registered at the disposer thread (similar to the one used by AWT) that should call it back in certain time and cause native resources being used by WebEngine to release.

另外,我建议在删除
WebView之前导航到about:blank;它会立即释放一些资源。

Also, I'd recommend to navigate to "about:blank" before removing WebView; it would make it to release some resources immediately.

但是,也许你完全看到别的东西,我无法明确地告诉你,例如由于WebView / Webkit内存泄漏而导致 RT-35262 JVM崩溃的原因,也是因为Java回调是Java 8u20或 RT-34630 WebView内存泄漏已分配,因为Java 9已解决。

But, maybe you are seeing something else entirely, I couldn't tell you definitively, such as whatever causes RT-35262 JVM crash due to memory leak in WebView/Webkit, also due to be resolved for Java 8u20 or RT-34630 WebView memory leak when JavaScript callbacks are assigned, due to be resolved for Java 9.

这篇关于如何在结束阶段后清除Javafx Webview内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆