In theory, what you're doing is perfectly fine, and you shouldn't need to change your code at all. In practice, WebKit has a lot of memory leaks, and programatically loading many new URIs in the same web view is eventually going to be problematic, as you've found.
My recommendation is to periodically, every so many page loads, create a new web view that uses a separate web process, and destroy the original web view. (That will also reset the back/forward list to stop it from growing, though I suspect the memory lost to the back/forward list is probably not significant compared to memory leaks when rendering the page.) I filed Bug 151203 - [GTK] Start a new web process when calling webkit_web_view_load functions? to consider having this happen automatically; your issue indicates we may need to bump the priority on that. In the meantime, you'll have to do it manually:
- Before doing anything else in your application, set the process model to
WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES
using webkit_web_context_set_process_model()
. (If you are not creating your own web contexts, you'll need to use the default web context webkit_web_context_get_default()
.)
- Periodically destroy your web view with
gtk_widget_destroy()
, then create a new one using webkit_web_view_new()
et. al. and attach it somewhere in your widget hierarchy. (Be sure NOT to use webkit_web_view_new_with_related_view()
as that's how you get two web views to use the same web process.)
If you have trouble getting that solution to work, an extreme alternative would be to periodically send SIGTERM to your web process to get a new one. Connect to WebKitWebView::web-process-crashed
, and call webkit_web_view_load_uri()
from there. That will result in the same web view using a new web process.