Skip to content

Commit f38a92e

Browse files
authored
Merge pull request #1545 from MicrosoftEdge/SetMemoryUsageLevel
WebView API for set memory usage target level
2 parents 44a0d69 + 3dbf874 commit f38a92e

1 file changed

Lines changed: 163 additions & 0 deletions

File tree

specs/MemoryUsageTargetLevel.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# Background
2+
Some WebView2 apps want to continue to run scripts while inactive and therefore cannot make usage of `TrySuspend` and `Resume` APIs to reduce resource consumption.
3+
We are introducing WebView2 API to reduce memory usage only for this type of inactive WebViews.
4+
5+
# Description
6+
You may set the `MemoryUsageTargetLevel` property to have the WebView2 consume less memory and going back to normal usage. This is useful when your Win32 app becomes invisible,
7+
but still wants to have script running or monitoring requests from network.
8+
9+
# Examples
10+
## .NET, WinRT
11+
```c#
12+
async protected void OnBecomingInactive()
13+
{
14+
// CanSuspendWebView() uses app specific logic to check whether the current web contents in the WebView2 can be suspended.
15+
if (CanSuspendWebView())
16+
{
17+
await webView.CoreWebView2.TrySuspendAsync();
18+
}
19+
else
20+
{
21+
webView.CoreWebView2.MemoryUsageTargetLevel = CoreWebView2MemoryUsageTargetLevel.Low;
22+
}
23+
}
24+
async protected void OnBecomingActive()
25+
{
26+
if (webView.CoreWebView2.IsSuspended)
27+
{
28+
webView.CoreWebView2.Resume();
29+
}
30+
else if (webView.CoreWebView2.MemoryUsageTargetLevel == CoreWebView2MemoryUsageTargetLevel.Low)
31+
{
32+
webView.CoreWebView2.MemoryUsageTargetLevel = CoreWebView2MemoryUsageTargetLevel.Normal;
33+
}
34+
}
35+
```
36+
## Win32 C++
37+
```cpp
38+
bool ViewComponent::HandleWindowMessage(
39+
HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT* result)
40+
{
41+
if (message == WM_SYSCOMMAND)
42+
{
43+
if (wParam == SC_MINIMIZE)
44+
{
45+
OnBecomingInactive();
46+
}
47+
else if (wParam == SC_RESTORE)
48+
{
49+
OnBecomingActive();
50+
}
51+
}
52+
}
53+
54+
void ViewComponent::OnBecomingInactive()
55+
{
56+
// CanSuspendWebView() uses app specific logic to check whether the current web contents in the WebView2 can be suspended.
57+
if (CanSuspendWebView())
58+
{
59+
CHECK_FAILURE(m_webView->TrySuspend(nullptr));
60+
}
61+
else
62+
{
63+
CHECK_FAILURE(m_webView->put_MemoryUsageTargetLevel(COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW);
64+
}
65+
}
66+
67+
void ViewComponent::OnBecomingActive()
68+
{
69+
BOOL isSuspended = FALSE;
70+
CHECK_FAILURE(m_webview->get_IsSuspended(&isSuspended));
71+
if (isSuspended)
72+
{
73+
CHECK_FAILURE(m_webView->Resume());
74+
}
75+
else
76+
{
77+
COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL memoryUsageTargetLevel = COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW;
78+
CHECK_FAILURE(m_webview->get_MemoryUsageTargetLevel(&memoryUsageTargetLevel));
79+
if (memoryUsageTargetLevel == COREWEBVIEW2_MEMORY_USAGE_LEVEL_LOW)
80+
{
81+
CHECK_FAILURE(m_webView->put_MemoryUsageTargetLevel(COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL));
82+
}
83+
}
84+
}
85+
```
86+
87+
# Remarks
88+
See [API Details](#api-details) section below for more details.
89+
90+
# API Notes
91+
See [API Details](#api-details) section below for API reference.
92+
93+
# API Details
94+
95+
## Win32 C++
96+
```IDL
97+
/// Specifies memory usage target level of WebView.
98+
[v1_enum]
99+
typedef enum COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL
100+
{
101+
/// Specifies normal memory usage target level.
102+
COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL,
103+
104+
/// Specifies low memory usage target level.
105+
/// Used for inactivate WebView for reduced memory consumption.
106+
COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW,
107+
108+
} COREWEBVIEW2_MEMORY_USAGE_LEVEL;
109+
110+
interface ICoreWebView2_6 : ICoreWebView2
111+
{
112+
113+
/// `MemoryUsageTargetLevel` indicates desired memory comsumption level of WebView.
114+
HRESULT get_MemoryUsageTargetLevel([in] COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL* level);
115+
116+
/// An app may set `MemoryUsageTargetLevel` to indicate desired memory
117+
/// comsumption level of WebView. Scripts will not be impacted and continue to run.
118+
/// This is useful for inactive apps that still want to run scripts and/or keep
119+
/// network connections alive and therefore could not call `TrySuspend` and `Resume`
120+
/// to reduce memory consumption.
121+
/// These apps can set memory usage target level to `COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW` when
122+
/// the app becomes inactive, and set back to `COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL` when
123+
/// the app becomes active.
124+
/// It is not neccesary to set CoreWebView2Controller's IsVisible property to false when calling the API.
125+
/// It is a best effort operation to change memory usage level, and the API will return before the operation completes.
126+
/// Setting the level to `COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW` could potentially cause
127+
/// memory for some WebView browser processes to be swapped out to disk when needed.
128+
/// It is a best effort to reduce memory usage as much as possible. If script runs after we swapped
129+
/// related memory out, we will swap the memory in to ensure script can still run. But performance might
130+
/// be impacted.
131+
/// Therefore, it is important for the app to set the level back to `COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL`
132+
/// when the app becomes active again to have a smooth user experience.
133+
/// Setting memory usage target level back to normal will not happen automatically.
134+
/// An app should choose to use either the combination of `TrySuspend` and `Resume` or the combination
135+
/// of setting `MemoryUsageTargetLevel` to low and normal. It is not advisable to mix them.
136+
/// Trying to set `MemoryUsageTargetLevel` while suspended will be ignored.
137+
/// The TrySuspend and Resume methods will change the MemoryUsageTargetLevel.
138+
/// TrySuspend will automatically set MemoryUsageTargetLevel to low while Resume on suspended WebView
139+
/// will automatically set MemoryUsageTargetLevel to normal.
140+
/// Calling `Resume` when the WebView is not suspended would not change MemoryUsageTargetLevel.
141+
HRESULT put_MemoryUsageTargetLevel([in] COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL level);
142+
}
143+
144+
```
145+
146+
## .NET WinRT
147+
```c#
148+
namespace Microsoft.Web.WebView2.Core
149+
{
150+
public enum CoreWebView2MemoryUsageTargetLevel
151+
{
152+
///
153+
Normal = 0,
154+
///
155+
Low = 1,
156+
}
157+
public partial class CoreWebView2
158+
{
159+
// There are other API in this interface that we are not showing
160+
public CoreWebView2MemoryUsageTargetLevel MemoryUsageTargetLevel { get; set; };
161+
}
162+
}
163+
```

0 commit comments

Comments
 (0)