diff --git a/snippets/fsharp/System/WeakReference/Overview/fs.fsproj b/snippets/fsharp/System/WeakReference/Overview/fs.fsproj new file mode 100644 index 00000000000..36cf252c6c2 --- /dev/null +++ b/snippets/fsharp/System/WeakReference/Overview/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/WeakReference/Overview/program.fs b/snippets/fsharp/System/WeakReference/Overview/program.fs new file mode 100644 index 00000000000..782f2b41f94 --- /dev/null +++ b/snippets/fsharp/System/WeakReference/Overview/program.fs @@ -0,0 +1,86 @@ +// +open System +open System.Collections.Generic + +// This class creates byte arrays to simulate data. +type Data(size) = + let _data = Array.zeroCreate (size * 1024) + + // Simple property. + member _.Name = + string size + +type Cache(count) = + // Dictionary to contain the cache. + static let mutable _cache = Dictionary() + + // Track the number of times an object is regenerated. + let mutable regenCount = 0 + + do + _cache <- Dictionary() + // + // Add objects with a short weak reference to the cache. + for i = 0 to count - 1 do + _cache.Add(i, WeakReference(Data i, false)) + // + + // Number of items in the cache. + member _.Count = + _cache.Count + + // Number of times an object needs to be regenerated. + member _.RegenerationCount = + regenCount + + // Retrieve a data object from the cache. + member _.Item + with get (index) = + // + match _cache[index].Target with + | :? Data as d-> + // Object was obtained with the weak reference. + printfn $"Regenerate object at {index}: No" + d + | _ -> + // If the object was reclaimed, generate a new one. + printfn $"Regenerate object at {index}: Yes" + let d = Data index + _cache[index].Target <- d + regenCount <- regenCount + 1 + d + // + +// Create the cache. +let cacheSize = 50 +let r = Random() +let c = Cache cacheSize + +let mutable dataName = "" +GC.Collect 0 + +// Randomly access objects in the cache. +for _ = 0 to c.Count - 1 do + let index = r.Next c.Count + + // Access the object by getting a property value. + dataName <- c[index].Name + +// Show results. +let regenPercent = double c.RegenerationCount / double c.Count +printfn $"Cache size: {c.Count}, Regenerated: {regenPercent:P2}%%" + +// Example of the last lines of the output: +// +// ... +// Regenerate object at 36: Yes +// Regenerate object at 8: Yes +// Regenerate object at 21: Yes +// Regenerate object at 4: Yes +// Regenerate object at 38: No +// Regenerate object at 7: Yes +// Regenerate object at 2: Yes +// Regenerate object at 43: Yes +// Regenerate object at 38: No +// Cache size: 50, Regenerated: 94% +// \ No newline at end of file diff --git a/xml/System/WeakReference.xml b/xml/System/WeakReference.xml index d6b91c4e002..96f47067943 100644 --- a/xml/System/WeakReference.xml +++ b/xml/System/WeakReference.xml @@ -76,6 +76,7 @@ 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. :::code language="csharp" source="~/snippets/csharp/System/WeakReference/Overview/program.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/WeakReference/Overview/program.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/WeakReference/vb/Module1.vb" id="Snippet1"::: ]]> @@ -227,6 +228,7 @@ The following example creates a cache of data objects with short weak references. This example is part of a larger example provided for the class. :::code language="csharp" source="~/snippets/csharp/System/WeakReference/Overview/program.cs" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/WeakReference/Overview/program.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/WeakReference/vb/Module1.vb" id="Snippet2"::: ]]> @@ -529,6 +531,7 @@ 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 class. :::code language="csharp" source="~/snippets/csharp/System/WeakReference/Overview/program.cs" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/WeakReference/Overview/program.fs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/WeakReference/vb/Module1.vb" id="Snippet3"::: ]]>