Skip to content
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
38 changes: 38 additions & 0 deletions snippets/fsharp/System/OutOfMemoryException/Overview/data1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module data1

// <Snippet3>
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.
// </Snippet3>
31 changes: 31 additions & 0 deletions snippets/fsharp/System/OutOfMemoryException/Overview/data2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module data2

// <Snippet4>
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
// </Snippet4>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module failfast1

// <Snippet2>
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...
// </Snippet2>
13 changes: 13 additions & 0 deletions snippets/fsharp/System/OutOfMemoryException/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="failfast1.fs" />
<Compile Include="sb_example1.fs" />
<Compile Include="data1.fs" />
<Compile Include="data2.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module sb_example1

// <Snippet1>
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.
// </Snippet1>
4 changes: 4 additions & 0 deletions xml/System/OutOfMemoryException.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ An <xref:System.OutOfMemoryException> exception has two major causes:
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.

:::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:
Expand All @@ -89,6 +90,7 @@ An <xref:System.OutOfMemoryException> exception has two major causes:
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.

:::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:
Expand Down Expand Up @@ -128,11 +130,13 @@ To prevent the <xref:System.OutOfMemoryException> 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 <xref:System.OutOfMemoryException> 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 <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.

:::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.**
Expand Down