Skip to content

Commit a498b9a

Browse files
authored
Create SetMemoryUsageLevel.md
1 parent 7f3fb50 commit a498b9a

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

specs/SetMemoryUsageLevel.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 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,
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+
webView.CoreWebView2.SetMemoryUsageLevel(CoreWebView2MemoryUsageLevel.Idle);
15+
}
16+
async protected void OnBecomingActive()
17+
{
18+
webView.CoreWebView2.SetMemoryUsageLevel(CoreWebView2MemoryUsageLevel.Normal);
19+
}
20+
```
21+
## Win32 C++
22+
```cpp
23+
bool ViewComponent::HandleWindowMessage(
24+
HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT* result)
25+
{
26+
if (message == WM_SYSCOMMAND)
27+
{
28+
if (wParam == SC_MINIMIZE)
29+
{
30+
OnBecomingInactive();
31+
}
32+
else if (wParam == SC_RESTORE)
33+
{
34+
OnBecomingActive();
35+
}
36+
}
37+
}
38+
39+
void ViewComponent::OnBecomingInactive()
40+
{
41+
HRESULT hr = webView->SetMemoryUsageLevel(COREWEBVIEW2_MEMORY_USAGE_LEVEL_IDLE);
42+
if (FAILED(hr))
43+
ShowFailure(hr, L"Failed to set SetMemoryUsageLevel to idle");
44+
}
45+
46+
void ViewComponent::OnBecomingActive()
47+
{
48+
HRESULT hr = webView->SetMemoryUsageLevel(COREWEBVIEW2_MEMORY_USAGE_LEVEL_NORMAL);
49+
if (FAILED(hr))
50+
ShowFailure(hr, L"Failed to set SetMemoryUsageLevel to normal");
51+
}
52+
53+
```
54+
55+
# Remarks
56+
See API details.
57+
58+
# API Notes
59+
See [API Details](#api-details) section below for API reference.
60+
61+
# API Details
62+
63+
## Win32 C++
64+
```IDL
65+
/// Specifies memory usage level of WebView.
66+
[v1_enum]
67+
typedef enum COREWEBVIEW2_MEMORY_USAGE_LEVEL {
68+
/// Specifies normal memory usage level.
69+
COREWEBVIEW2_MEMORY_USAGE_LEVEL_NORMAL,
70+
71+
/// Specifies idle memory usage level.
72+
/// Used for inactivate WebView for reduced memory consumption.
73+
COREWEBVIEW2_MEMORY_USAGE_LEVEL_IDLE,
74+
75+
} COREWEBVIEW2_MEMORY_USAGE_LEVEL;
76+
77+
interface ICoreWebView2_6 : ICoreWebView2 {
78+
79+
/// An app may call the `SetMemoryUsageLevel` API to indicate desired memory
80+
/// comsumption level of WebView. Scripts will not be impacted and continue to run.
81+
/// This is useful for inactive apps that still want to run scripts and/or keep
82+
/// network connections alive and therefore could not call `TrySuspend` and `Resume`
83+
/// 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
86+
/// the app becomes active.
87+
/// It is not neccesary to set CoreWebView2Controller's IsVisible property to false when calling the API.
88+
/// 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
90+
/// 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`
92+
/// when the app becomes active again to have smoothy user experience.
93+
/// Setting memory usage level back to normal will not happen automatically.
94+
HRESULT SetMemoryUsageLevel([in] COREWEBVIEW2_MEMORY_USAGE_LEVEL level);
95+
}
96+
97+
```
98+
99+
## .NET WinRT
100+
```c#
101+
namespace Microsoft.Web.WebView2.Core
102+
{
103+
public partial class CoreWebView2
104+
{
105+
// There are other API in this interface that we are not showing
106+
public void SetMemoryUsageLevel(CoreWebView2MemoryUsageLevel level);
107+
}
108+
}
109+
```

0 commit comments

Comments
 (0)