Skip to content

Commit 62c3e1d

Browse files
authored
Merge pull request #1533 from MicrosoftEdge/SetMemoryUsageLevel-draft
Set memory usage level draft
2 parents 7f3fb50 + da9af59 commit 62c3e1d

1 file changed

Lines changed: 158 additions & 0 deletions

File tree

specs/SetMemoryUsageLevel.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
/// Specifies normal memory usage target level.
101+
COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL,
102+
103+
/// Specifies low memory usage target level.
104+
/// Used for inactivate WebView for reduced memory consumption.
105+
COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW,
106+
107+
} COREWEBVIEW2_MEMORY_USAGE_LEVEL;
108+
109+
interface ICoreWebView2_6 : ICoreWebView2 {
110+
111+
/// `MemoryUsageTargetLevel` indicates desired memory comsumption level of WebView.
112+
HRESULT get_MemoryUsageTargetLevel([in] COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL* level);
113+
114+
/// An app may set `MemoryUsageTargetLevel` to indicate desired memory
115+
/// comsumption level of WebView. Scripts will not be impacted and continue to run.
116+
/// This is useful for inactive apps that still want to run scripts and/or keep
117+
/// network connections alive and therefore could not call `TrySuspend` and `Resume`
118+
/// to reduce memory consumption.
119+
/// These apps can set memory usage target level to `COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW` when
120+
/// the app becomes inactive, and set back to `COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL` when
121+
/// the app becomes active.
122+
/// It is not neccesary to set CoreWebView2Controller's IsVisible property to false when calling the API.
123+
/// It is a best effort operation to change memory usage level, and the API will return before the operation completes.
124+
/// Setting the level to `COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW` could potentially cause
125+
/// memory for some WebView browser processes to be swapped out to disk when needed. Therefore,
126+
/// it is important for the app to set the level back to `COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL`
127+
/// when the app becomes active again to have a smooth user experience.
128+
/// Setting memory usage level back to normal will not happen automatically.
129+
/// An app should choose to use either the combination of `TrySuspend` and `Resume` or the combination
130+
/// of setting MemoryUsageTargetLevel to low and normal. It is not advisable to mix them.
131+
/// The TrySuspend and Resume methods will change the MemoryUsageTargetLevel.
132+
/// TrySuspend will automatically set MemoryUsageTargetLevel to low while Resume on suspended WebView
133+
/// will automatically set MemoryUsageTargetLevel to normal.
134+
/// Calling `Resume` when the WebView is not suspended would not change MemoryUsageTargetLevel.
135+
/// Setting MemoryUsageTargetLevel to normal on suspended WebView will auto resume WebView.
136+
HRESULT put_MemoryUsageTargetLevel([in] COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL level);
137+
}
138+
139+
```
140+
141+
## .NET WinRT
142+
```c#
143+
namespace Microsoft.Web.WebView2.Core
144+
{
145+
public enum CoreWebView2MemoryUsageTargetLevel
146+
{
147+
///
148+
Normal = 0,
149+
///
150+
Low = 1,
151+
}
152+
public partial class CoreWebView2
153+
{
154+
// There are other API in this interface that we are not showing
155+
public CoreWebView2MemoryUsageTargetLevel MemoryUsageTargetLevel { get; set; };
156+
}
157+
}
158+
```

0 commit comments

Comments
 (0)