From 338e5089ae9acd66a0cb6594a0c8e016ea071ea0 Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Sat, 12 Mar 2022 17:34:47 -0800
Subject: [PATCH] OutOfMemoryException F# snippets
---
.../OutOfMemoryException/Overview/data1.fs | 38 +++++++++++++++++++
.../OutOfMemoryException/Overview/data2.fs | 31 +++++++++++++++
.../Overview/failfast1.fs | 24 ++++++++++++
.../OutOfMemoryException/Overview/fs.fsproj | 13 +++++++
.../Overview/sb_example1.fs | 17 +++++++++
xml/System/OutOfMemoryException.xml | 4 ++
6 files changed, 127 insertions(+)
create mode 100644 snippets/fsharp/System/OutOfMemoryException/Overview/data1.fs
create mode 100644 snippets/fsharp/System/OutOfMemoryException/Overview/data2.fs
create mode 100644 snippets/fsharp/System/OutOfMemoryException/Overview/failfast1.fs
create mode 100644 snippets/fsharp/System/OutOfMemoryException/Overview/fs.fsproj
create mode 100644 snippets/fsharp/System/OutOfMemoryException/Overview/sb_example1.fs
diff --git a/snippets/fsharp/System/OutOfMemoryException/Overview/data1.fs b/snippets/fsharp/System/OutOfMemoryException/Overview/data1.fs
new file mode 100644
index 00000000000..c01091ee565
--- /dev/null
+++ b/snippets/fsharp/System/OutOfMemoryException/Overview/data1.fs
@@ -0,0 +1,38 @@
+module data1
+
+//
+open System
+
+let getData () =
+ let rnd = Random()
+ [| for i = 1 to 200000000 do
+ rnd.NextDouble()
+ if i % 10000000 = 0 then
+ printfn $"Retrieved {i:N0} items of data." |]
+
+let getMean values =
+ let sum = Array.sum values
+
+ sum / float values.Length
+
+let values = getData ()
+// Compute mean.
+printfn $"Sample mean: {getMean values}, N = {values.Length}"
+
+// The example displays output like the following:
+// Retrieved 10,000,000 items of data.
+// Retrieved 20,000,000 items of data.
+// Retrieved 30,000,000 items of data.
+// Retrieved 40,000,000 items of data.
+// Retrieved 50,000,000 items of data.
+// Retrieved 60,000,000 items of data.
+// Retrieved 70,000,000 items of data.
+// Retrieved 80,000,000 items of data.
+// Retrieved 90,000,000 items of data.
+// Retrieved 100,000,000 items of data.
+// Retrieved 110,000,000 items of data.
+// Retrieved 120,000,000 items of data.
+// Retrieved 130,000,000 items of data.
+//
+// Unhandled Exception: OutOfMemoryException.
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/OutOfMemoryException/Overview/data2.fs b/snippets/fsharp/System/OutOfMemoryException/Overview/data2.fs
new file mode 100644
index 00000000000..afc389d4949
--- /dev/null
+++ b/snippets/fsharp/System/OutOfMemoryException/Overview/data2.fs
@@ -0,0 +1,31 @@
+module data2
+
+//
+open System
+// open System.IO
+
+let getResult () =
+ let chunkSize = 50000000
+ let nToGet = 200000000
+ let rnd = Random()
+ // use fs = new FileStream(@".\data.bin", FileMode.Create)
+ // use bin = new BinaryWriter(fs)
+ // bin.Write 0
+ let mutable n = 0
+ let mutable sum = 0.
+ for _ = 0 to int (ceil (nToGet / chunkSize |> float) - 1.) do
+ for _ = 0 to min (nToGet - n - 1) (chunkSize - 1) do
+ let value = rnd.NextDouble()
+ sum <- sum + value
+ n <- n + 1
+ // bin.Write(value)
+ // bin.Seek(0, SeekOrigin.Begin) |> ignore
+ // bin.Write n
+ sum / float n, n
+
+let mean, n = getResult ()
+printfn $"Sample mean: {mean}, N = {n:N0}"
+
+// The example displays output like the following:
+// Sample mean: 0.500022771458399, N = 200,000,000
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/OutOfMemoryException/Overview/failfast1.fs b/snippets/fsharp/System/OutOfMemoryException/Overview/failfast1.fs
new file mode 100644
index 00000000000..bea50fb6472
--- /dev/null
+++ b/snippets/fsharp/System/OutOfMemoryException/Overview/failfast1.fs
@@ -0,0 +1,24 @@
+module failfast1
+
+//
+open System
+
+try
+ // Outer block to handle any unexpected exceptions.
+ try
+ let s = "This"
+ let s = s.Insert(2, "is ")
+
+ // Throw an OutOfMemoryException exception.
+ raise (OutOfMemoryException())
+ with
+ | :? ArgumentException ->
+ printfn "ArgumentException in String.Insert"
+
+ // Execute program logic.
+with :? OutOfMemoryException as e ->
+ printfn "Terminating application unexpectedly..."
+ Environment.FailFast $"Out of Memory: {e.Message}"
+// The example displays the following output:
+// Terminating application unexpectedly...
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/OutOfMemoryException/Overview/fs.fsproj b/snippets/fsharp/System/OutOfMemoryException/Overview/fs.fsproj
new file mode 100644
index 00000000000..642c97cfbc8
--- /dev/null
+++ b/snippets/fsharp/System/OutOfMemoryException/Overview/fs.fsproj
@@ -0,0 +1,13 @@
+
+
+ Exe
+ net6.0
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/snippets/fsharp/System/OutOfMemoryException/Overview/sb_example1.fs b/snippets/fsharp/System/OutOfMemoryException/Overview/sb_example1.fs
new file mode 100644
index 00000000000..cfb05877bfb
--- /dev/null
+++ b/snippets/fsharp/System/OutOfMemoryException/Overview/sb_example1.fs
@@ -0,0 +1,17 @@
+module sb_example1
+
+//
+open System
+open System.Text
+
+let sb = StringBuilder(15, 15)
+sb.Append "Substring #1 "
+|> ignore
+try
+ sb.Insert(0, "Substring #2 ", 1)
+ |> ignore
+with :? OutOfMemoryException as e ->
+ printfn $"Out of Memory: {e.Message}"
+// The example displays the following output:
+// Out of Memory: Insufficient memory to continue the execution of the program.
+//
\ No newline at end of file
diff --git a/xml/System/OutOfMemoryException.xml b/xml/System/OutOfMemoryException.xml
index 015e2e469eb..f0544cb0a2c 100644
--- a/xml/System/OutOfMemoryException.xml
+++ b/xml/System/OutOfMemoryException.xml
@@ -80,6 +80,7 @@ An exception has two major causes:
This type of exception represents a catastrophic failure. If you choose to handle the exception, you should include a `catch` block that calls the method to terminate your app and add an entry to the system event log, as the following example does.
:::code language="csharp" source="~/snippets/csharp/System/OutOfMemoryException/Overview/failfast1.cs" id="Snippet2":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/OutOfMemoryException/Overview/failfast1.fs" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.outofmemoryexception/vb/failfast1.vb" id="Snippet2":::
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 exception has two major causes:
You are attempting to increase the length of a object beyond the size specified by its property. The following example illustrates the exception thrown by a call to the method when the example tries to insert a string that would cause the object's property to exceed its maximum capacity.
:::code language="csharp" source="~/snippets/csharp/System/OutOfMemoryException/Overview/sb_example1.cs" interactive="try-dotnet" id="Snippet1":::
+:::code language="fsharp" source="~/snippets/fsharp/System/OutOfMemoryException/Overview/sb_example1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.outofmemoryexception/vb/sb_example1.vb" id="Snippet1":::
You can do either of the following to address the error:
@@ -128,11 +130,13 @@ To prevent the exceptions, you must modify yo
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 is thrown.
:::code language="csharp" source="~/snippets/csharp/System/OutOfMemoryException/Overview/data1.cs" id="Snippet3":::
+:::code language="fsharp" source="~/snippets/fsharp/System/OutOfMemoryException/Overview/data1.fs" id="Snippet3":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.outofmemoryexception/vb/data1.vb" id="Snippet3":::
The following example eliminates the 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.
:::code language="csharp" source="~/snippets/csharp/System/OutOfMemoryException/Overview/data2.cs" id="Snippet4":::
+:::code language="fsharp" source="~/snippets/fsharp/System/OutOfMemoryException/Overview/data2.fs" id="Snippet4":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.outofmemoryexception/vb/data2.vb" id="Snippet4":::
**You are repeatedly concatenating large strings.**