Skip to content

Commit c567b53

Browse files
authored
Update SetMemoryUsageLevel.md
1 parent 1eb1801 commit c567b53

1 file changed

Lines changed: 59 additions & 23 deletions

File tree

specs/SetMemoryUsageLevel.md

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,28 @@ Some WebView2 apps want to continue to run scripts while inactive and therefore
33
We are introducing WebView2 API to reduce memory usage only for this type of inactive WebViews.
44

55
# Description
6-
You may call the `SetMemoryUsageLevel` API to have the WebView2 consume less memory and going back to normal usage. This is useful when your Win32 app becomes invisible,
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,
77
but still wants to have script running or monitoring requests from network.
88

99
# Examples
1010
## .NET, WinRT
1111
```c#
1212
async protected void OnBecomingInactive()
1313
{
14-
webView.CoreWebView2.SetMemoryUsageLevel(CoreWebView2MemoryUsageLevel.Idle);
14+
// CanSuspendWebPage() checks whether the current web contents in WebView can be suspended.
15+
if (CanSuspendWebView()) {
16+
await webView.CoreWebView2.TrySuspendAsync();
17+
} else {
18+
webView.CoreWebView2.MemoryUsageTargetLevel = CoreWebView2MemoryUsageTargetLevel.Low;
19+
}
1520
}
1621
async protected void OnBecomingActive()
1722
{
18-
webView.CoreWebView2.SetMemoryUsageLevel(CoreWebView2MemoryUsageLevel.Normal);
23+
if (webView.CoreWebView2.IsSuspended) {
24+
webView.CoreWebView2.Resume();
25+
} else if (webView.CoreWebView2.MemoryUsageTargetLevel == CoreWebView2MemoryUsageTargetLevel.Low) {
26+
webView.CoreWebView2.MemoryUsageTargetLevel = CoreWebView2MemoryUsageTargetLevel.Normal;
27+
}
1928
}
2029
```
2130
## Win32 C++
@@ -38,18 +47,28 @@ bool ViewComponent::HandleWindowMessage(
3847

3948
void ViewComponent::OnBecomingInactive()
4049
{
41-
HRESULT hr = webView->SetMemoryUsageLevel(COREWEBVIEW2_MEMORY_USAGE_LEVEL_IDLE);
42-
if (FAILED(hr))
43-
ShowFailure(hr, L"Failed to set SetMemoryUsageLevel to idle");
50+
// CanSuspendWebPage() checks whether the current web contents in WebView can be suspended.
51+
if (CanSuspendWebView()) {
52+
CHECK_FAILURE(m_webView->TrySuspend(nullptr));
53+
} else {
54+
CHECK_FAILURE(m_webView->put_MemoryUsageTargetLevel(COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW);
55+
}
4456
}
4557

4658
void ViewComponent::OnBecomingActive()
4759
{
48-
HRESULT hr = webView->SetMemoryUsageLevel(COREWEBVIEW2_MEMORY_USAGE_LEVEL_NORMAL);
49-
if (FAILED(hr))
50-
ShowFailure(hr, L"Failed to set SetMemoryUsageLevel to normal");
60+
BOOL isSuspended = FALSE;
61+
CHECK_FAILURE(m_webview->get_IsSuspended(&isSuspended));
62+
if (isSuspended) {
63+
CHECK_FAILURE(m_webView->Resume());
64+
} else {
65+
COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL memoryUsageTargetLevel = COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW;
66+
CHECK_FAILURE(m_webview->get_MemoryUsageTargetLevel(&memoryUsageTargetLevel));
67+
if (memoryUsageTargetLevel == COREWEBVIEW2_MEMORY_USAGE_LEVEL_LOW) {
68+
CHECK_FAILURE(m_webView->put_MemoryUsageTargetLevel(COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL));
69+
}
70+
}
5171
}
52-
5372
```
5473
5574
# Remarks
@@ -62,36 +81,46 @@ See [API Details](#api-details) section below for API reference.
6281
6382
## Win32 C++
6483
```IDL
65-
/// Specifies memory usage level of WebView.
84+
/// Specifies memory usage target level of WebView.
6685
[v1_enum]
67-
typedef enum COREWEBVIEW2_MEMORY_USAGE_LEVEL {
68-
/// Specifies normal memory usage level.
69-
COREWEBVIEW2_MEMORY_USAGE_LEVEL_NORMAL,
86+
typedef enum COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL {
87+
/// Specifies normal memory usage target level.
88+
COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL,
7089
71-
/// Specifies idle memory usage level.
90+
/// Specifies low memory usage target level.
7291
/// Used for inactivate WebView for reduced memory consumption.
73-
COREWEBVIEW2_MEMORY_USAGE_LEVEL_IDLE,
92+
COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW,
7493
7594
} COREWEBVIEW2_MEMORY_USAGE_LEVEL;
7695
7796
interface ICoreWebView2_6 : ICoreWebView2 {
7897
79-
/// An app may call the `SetMemoryUsageLevel` API to indicate desired memory
98+
/// `MemoryUsageTargetLevel` indicates desired memory comsumption level of WebView.
99+
HRESULT get_MemoryUsageTargetLevel([in] COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL level);
100+
101+
/// An app may set `MemoryUsageTargetLevel` to indicate desired memory
80102
/// comsumption level of WebView. Scripts will not be impacted and continue to run.
81103
/// This is useful for inactive apps that still want to run scripts and/or keep
82104
/// network connections alive and therefore could not call `TrySuspend` and `Resume`
83105
/// to reduce memory consumption.
84-
/// These apps can set memory usage level to `COREWEBVIEW2_MEMORY_USAGE_LEVEL_IDLE` when
85-
/// the app becomes inactive, and set back to `COREWEBVIEW2_MEMORY_USAGE_LEVEL_NORMAL` when
106+
/// These apps can set memory usage target level to `COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW` when
107+
/// the app becomes inactive, and set back to `COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL` when
86108
/// the app becomes active.
87109
/// It is not neccesary to set CoreWebView2Controller's IsVisible property to false when calling the API.
88110
/// It is a best effort operation to change memory usage level, and the API will return before the operation completes.
89-
/// Setting the level to `COREWEBVIEW2_MEMORY_USAGE_LEVEL_IDLE` could potentially cause
111+
/// Setting the level to `COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW` could potentially cause
90112
/// memory for some WebView browser processes to be swapped out to disk when needed. Therefore,
91-
/// it is important for the app to set the level back to `COREWEBVIEW2_MEMORY_USAGE_LEVEL_NORMAL`
113+
/// it is important for the app to set the level back to `COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL`
92114
/// when the app becomes active again to have smoothy user experience.
93115
/// Setting memory usage level back to normal will not happen automatically.
94-
HRESULT SetMemoryUsageLevel([in] COREWEBVIEW2_MEMORY_USAGE_LEVEL level);
116+
/// An app should choose to use either the combination of `TrySuspend` and `Resume` or the combination
117+
/// of setting MemoryUsageTargetLevel to low and normal. It is advices to not mix of them.
118+
/// Suspend and resume opertaion is a superset of setting MemoryUsageTargetLevel.
119+
/// TrySuspend will automatically set MemoryUsageTargetLevel to low while Resume on suspended WebView
120+
/// will automatically set MemoryUsageTargetLevel to normal.
121+
/// Calling `Resume` when the WebView is not suspended would not change MemoryUsageTargetLevel.
122+
/// Setting MemoryUsageTargetLevel to normal on suspended WebView will auto resume WebView.
123+
HRESULT put_MemoryUsageTargetLevel([in] COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL level);
95124
}
96125
97126
```
@@ -100,10 +129,17 @@ interface ICoreWebView2_6 : ICoreWebView2 {
100129
```c#
101130
namespace Microsoft.Web.WebView2.Core
102131
{
132+
public enum CoreWebView2MemoryUsageTargetLevel
133+
{
134+
///
135+
Normal = 0,
136+
///
137+
Low = 1,
138+
}
103139
public partial class CoreWebView2
104140
{
105141
// There are other API in this interface that we are not showing
106-
public void SetMemoryUsageLevel(CoreWebView2MemoryUsageLevel level);
142+
public CoreWebView2MemoryUsageTargetLevel MemoryUsageTargetLevel { get; set; };
107143
}
108144
}
109145
```

0 commit comments

Comments
 (0)