Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.WeakReference F# snippets #8077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions snippets/fsharp/System/WeakReference/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="program.fs" />
</ItemGroup>
</Project>
86 changes: 86 additions & 0 deletions snippets/fsharp/System/WeakReference/Overview/program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// <Snippet1>
open System
open System.Collections.Generic

// This class creates byte arrays to simulate data.
type Data(size) =
let _data = Array.zeroCreate<byte> (size * 1024)

// Simple property.
member _.Name =
string size

type Cache(count) =
// Dictionary to contain the cache.
static let mutable _cache = Dictionary<int, WeakReference>()

// Track the number of times an object is regenerated.
let mutable regenCount = 0

do
_cache <- Dictionary<int, WeakReference>()
// <Snippet2>
// Add objects with a short weak reference to the cache.
for i = 0 to count - 1 do
_cache.Add(i, WeakReference(Data i, false))
// </Snippet2>

// 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) =
// <Snippet3>
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
// </Snippet3>

// 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%
// </Snippet1>
3 changes: 3 additions & 0 deletions xml/System/WeakReference.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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":::

]]></format>
Expand Down Expand Up @@ -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 <xref:System.WeakReference> 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":::

]]></format>
Expand Down Expand Up @@ -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 <xref:System.WeakReference> 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":::

]]></format>
Expand Down