Skip to content

Commit e0d92c2

Browse files
authored
WeakReference F# snippets (dotnet#8077)
1 parent 8f58082 commit e0d92c2

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="program.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// <Snippet1>
2+
open System
3+
open System.Collections.Generic
4+
5+
// This class creates byte arrays to simulate data.
6+
type Data(size) =
7+
let _data = Array.zeroCreate<byte> (size * 1024)
8+
9+
// Simple property.
10+
member _.Name =
11+
string size
12+
13+
type Cache(count) =
14+
// Dictionary to contain the cache.
15+
static let mutable _cache = Dictionary<int, WeakReference>()
16+
17+
// Track the number of times an object is regenerated.
18+
let mutable regenCount = 0
19+
20+
do
21+
_cache <- Dictionary<int, WeakReference>()
22+
// <Snippet2>
23+
// Add objects with a short weak reference to the cache.
24+
for i = 0 to count - 1 do
25+
_cache.Add(i, WeakReference(Data i, false))
26+
// </Snippet2>
27+
28+
// Number of items in the cache.
29+
member _.Count =
30+
_cache.Count
31+
32+
// Number of times an object needs to be regenerated.
33+
member _.RegenerationCount =
34+
regenCount
35+
36+
// Retrieve a data object from the cache.
37+
member _.Item
38+
with get (index) =
39+
// <Snippet3>
40+
match _cache[index].Target with
41+
| :? Data as d->
42+
// Object was obtained with the weak reference.
43+
printfn $"Regenerate object at {index}: No"
44+
d
45+
| _ ->
46+
// If the object was reclaimed, generate a new one.
47+
printfn $"Regenerate object at {index}: Yes"
48+
let d = Data index
49+
_cache[index].Target <- d
50+
regenCount <- regenCount + 1
51+
d
52+
// </Snippet3>
53+
54+
// Create the cache.
55+
let cacheSize = 50
56+
let r = Random()
57+
let c = Cache cacheSize
58+
59+
let mutable dataName = ""
60+
GC.Collect 0
61+
62+
// Randomly access objects in the cache.
63+
for _ = 0 to c.Count - 1 do
64+
let index = r.Next c.Count
65+
66+
// Access the object by getting a property value.
67+
dataName <- c[index].Name
68+
69+
// Show results.
70+
let regenPercent = double c.RegenerationCount / double c.Count
71+
printfn $"Cache size: {c.Count}, Regenerated: {regenPercent:P2}%%"
72+
73+
// Example of the last lines of the output:
74+
//
75+
// ...
76+
// Regenerate object at 36: Yes
77+
// Regenerate object at 8: Yes
78+
// Regenerate object at 21: Yes
79+
// Regenerate object at 4: Yes
80+
// Regenerate object at 38: No
81+
// Regenerate object at 7: Yes
82+
// Regenerate object at 2: Yes
83+
// Regenerate object at 43: Yes
84+
// Regenerate object at 38: No
85+
// Cache size: 50, Regenerated: 94%
86+
// </Snippet1>

xml/System/WeakReference.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
The example randomly accesses objects in the cache. If an object is reclaimed for garbage collection, a new data object is regenerated; otherwise, the object is available to access because of the weak reference.
7777
7878
:::code language="csharp" source="~/snippets/csharp/System/WeakReference/Overview/program.cs" id="Snippet1":::
79+
:::code language="fsharp" source="~/snippets/fsharp/System/WeakReference/Overview/program.fs" id="Snippet1":::
7980
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/WeakReference/vb/Module1.vb" id="Snippet1":::
8081
8182
]]></format>
@@ -227,6 +228,7 @@
227228
The following example creates a cache of data objects with short weak references. This example is part of a larger example provided for the <xref:System.WeakReference> class.
228229
229230
:::code language="csharp" source="~/snippets/csharp/System/WeakReference/Overview/program.cs" id="Snippet2":::
231+
:::code language="fsharp" source="~/snippets/fsharp/System/WeakReference/Overview/program.fs" id="Snippet2":::
230232
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/WeakReference/vb/Module1.vb" id="Snippet2":::
231233
232234
]]></format>
@@ -529,6 +531,7 @@
529531
The following example tries to obtain an object from a cache of objects with weak references. If the object was reclaimed for garbage collection, a new object is generated. This example is part of a larger example provided for the <xref:System.WeakReference> class.
530532
531533
:::code language="csharp" source="~/snippets/csharp/System/WeakReference/Overview/program.cs" id="Snippet3":::
534+
:::code language="fsharp" source="~/snippets/fsharp/System/WeakReference/Overview/program.fs" id="Snippet3":::
532535
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/WeakReference/vb/Module1.vb" id="Snippet3":::
533536
534537
]]></format>

0 commit comments

Comments
 (0)