Skip to content

Commit 9ec50eb

Browse files
authored
OutOfMemoryException F# snippets (dotnet#7818)
1 parent 64a185c commit 9ec50eb

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module data1
2+
3+
// <Snippet3>
4+
open System
5+
6+
let getData () =
7+
let rnd = Random()
8+
[| for i = 1 to 200000000 do
9+
rnd.NextDouble()
10+
if i % 10000000 = 0 then
11+
printfn $"Retrieved {i:N0} items of data." |]
12+
13+
let getMean values =
14+
let sum = Array.sum values
15+
16+
sum / float values.Length
17+
18+
let values = getData ()
19+
// Compute mean.
20+
printfn $"Sample mean: {getMean values}, N = {values.Length}"
21+
22+
// The example displays output like the following:
23+
// Retrieved 10,000,000 items of data.
24+
// Retrieved 20,000,000 items of data.
25+
// Retrieved 30,000,000 items of data.
26+
// Retrieved 40,000,000 items of data.
27+
// Retrieved 50,000,000 items of data.
28+
// Retrieved 60,000,000 items of data.
29+
// Retrieved 70,000,000 items of data.
30+
// Retrieved 80,000,000 items of data.
31+
// Retrieved 90,000,000 items of data.
32+
// Retrieved 100,000,000 items of data.
33+
// Retrieved 110,000,000 items of data.
34+
// Retrieved 120,000,000 items of data.
35+
// Retrieved 130,000,000 items of data.
36+
//
37+
// Unhandled Exception: OutOfMemoryException.
38+
// </Snippet3>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module data2
2+
3+
// <Snippet4>
4+
open System
5+
// open System.IO
6+
7+
let getResult () =
8+
let chunkSize = 50000000
9+
let nToGet = 200000000
10+
let rnd = Random()
11+
// use fs = new FileStream(@".\data.bin", FileMode.Create)
12+
// use bin = new BinaryWriter(fs)
13+
// bin.Write 0
14+
let mutable n = 0
15+
let mutable sum = 0.
16+
for _ = 0 to int (ceil (nToGet / chunkSize |> float) - 1.) do
17+
for _ = 0 to min (nToGet - n - 1) (chunkSize - 1) do
18+
let value = rnd.NextDouble()
19+
sum <- sum + value
20+
n <- n + 1
21+
// bin.Write(value)
22+
// bin.Seek(0, SeekOrigin.Begin) |> ignore
23+
// bin.Write n
24+
sum / float n, n
25+
26+
let mean, n = getResult ()
27+
printfn $"Sample mean: {mean}, N = {n:N0}"
28+
29+
// The example displays output like the following:
30+
// Sample mean: 0.500022771458399, N = 200,000,000
31+
// </Snippet4>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module failfast1
2+
3+
// <Snippet2>
4+
open System
5+
6+
try
7+
// Outer block to handle any unexpected exceptions.
8+
try
9+
let s = "This"
10+
let s = s.Insert(2, "is ")
11+
12+
// Throw an OutOfMemoryException exception.
13+
raise (OutOfMemoryException())
14+
with
15+
| :? ArgumentException ->
16+
printfn "ArgumentException in String.Insert"
17+
18+
// Execute program logic.
19+
with :? OutOfMemoryException as e ->
20+
printfn "Terminating application unexpectedly..."
21+
Environment.FailFast $"Out of Memory: {e.Message}"
22+
// The example displays the following output:
23+
// Terminating application unexpectedly...
24+
// </Snippet2>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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="failfast1.fs" />
9+
<Compile Include="sb_example1.fs" />
10+
<Compile Include="data1.fs" />
11+
<Compile Include="data2.fs" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module sb_example1
2+
3+
// <Snippet1>
4+
open System
5+
open System.Text
6+
7+
let sb = StringBuilder(15, 15)
8+
sb.Append "Substring #1 "
9+
|> ignore
10+
try
11+
sb.Insert(0, "Substring #2 ", 1)
12+
|> ignore
13+
with :? OutOfMemoryException as e ->
14+
printfn $"Out of Memory: {e.Message}"
15+
// The example displays the following output:
16+
// Out of Memory: Insufficient memory to continue the execution of the program.
17+
// </Snippet1>

xml/System/OutOfMemoryException.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ An <xref:System.OutOfMemoryException> exception has two major causes:
8080
This type of <xref:System.OutOfMemoryException> exception represents a catastrophic failure. If you choose to handle the exception, you should include a `catch` block that calls the <xref:System.Environment.FailFast%2A?displayProperty=nameWithType> method to terminate your app and add an entry to the system event log, as the following example does.
8181
8282
:::code language="csharp" source="~/snippets/csharp/System/OutOfMemoryException/Overview/failfast1.cs" id="Snippet2":::
83+
:::code language="fsharp" source="~/snippets/fsharp/System/OutOfMemoryException/Overview/failfast1.fs" id="Snippet2":::
8384
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.outofmemoryexception/vb/failfast1.vb" id="Snippet2":::
8485
8586
Some of the conditions under which the exception is thrown and the actions you can take to eliminate it include the following:
@@ -89,6 +90,7 @@ An <xref:System.OutOfMemoryException> exception has two major causes:
8990
You are attempting to increase the length of a <xref:System.Text.StringBuilder> object beyond the size specified by its <xref:System.Text.StringBuilder.MaxCapacity%2A?displayProperty=nameWithType> property. The following example illustrates the <xref:System.OutOfMemoryException> exception thrown by a call to the <xref:System.Text.StringBuilder.Insert%28System.Int32%2CSystem.String%2CSystem.Int32%29?displayProperty=nameWithType> method when the example tries to insert a string that would cause the object's <xref:System.Text.StringBuilder.Length%2A> property to exceed its maximum capacity.
9091
9192
:::code language="csharp" source="~/snippets/csharp/System/OutOfMemoryException/Overview/sb_example1.cs" interactive="try-dotnet" id="Snippet1":::
93+
:::code language="fsharp" source="~/snippets/fsharp/System/OutOfMemoryException/Overview/sb_example1.fs" id="Snippet1":::
9294
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.outofmemoryexception/vb/sb_example1.vb" id="Snippet1":::
9395
9496
You can do either of the following to address the error:
@@ -128,11 +130,13 @@ To prevent the <xref:System.OutOfMemoryException> exceptions, you must modify yo
128130
The following example gets a array that consists of 200 million floating-point values and then calculates their mean. The output from the example shows that, because the example stores the entire array in memory before it calculates the mean, an <xref:System.OutOfMemoryException> is thrown.
129131
130132
:::code language="csharp" source="~/snippets/csharp/System/OutOfMemoryException/Overview/data1.cs" id="Snippet3":::
133+
:::code language="fsharp" source="~/snippets/fsharp/System/OutOfMemoryException/Overview/data1.fs" id="Snippet3":::
131134
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.outofmemoryexception/vb/data1.vb" id="Snippet3":::
132135
133136
The following example eliminates the <xref:System.OutOfMemoryException> exception by processing the incoming data without storing the entire data set in memory, serializing the data to a file if necessary to permit further processing (these lines are commented out in the example, since in this case they produce a file whose size is greater than 1GB), and returning the calculated mean and the number of cases to the calling routine.
134137
135138
:::code language="csharp" source="~/snippets/csharp/System/OutOfMemoryException/Overview/data2.cs" id="Snippet4":::
139+
:::code language="fsharp" source="~/snippets/fsharp/System/OutOfMemoryException/Overview/data2.fs" id="Snippet4":::
136140
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.outofmemoryexception/vb/data2.vb" id="Snippet4":::
137141
138142
**You are repeatedly concatenating large strings.**

0 commit comments

Comments
 (0)