本文整理匯總了VB.NET中System.OutOfMemoryException類的典型用法代碼示例。如果您正苦於以下問題:VB.NET OutOfMemoryException類的具體用法?VB.NET OutOfMemoryException怎麽用?VB.NET OutOfMemoryException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了OutOfMemoryException類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
Module Example
Public Sub Main()
Try
' Outer block to handle any unexpected exceptions.
Try
Dim s As String = "This"
s = s.Insert(2, "is ")
' Throw an OutOfMemoryException exception.
Throw New OutOfMemoryException()
Catch e As ArgumentException
Console.WriteLine("ArgumentException in String.Insert")
End Try
' Execute program logic.
Catch e As OutOfMemoryException
Console.WriteLine("Terminating application unexpectedly...")
Environment.FailFast(String.Format("Out of Memory: {0}",
e.Message))
End Try
End Sub
End Module
輸出:
Terminating application unexpectedly...
示例2: Example
' 導入命名空間
Imports System.Text
Module Example
Public Sub Main()
Dim sb As New StringBuilder(15, 15)
sb.Append("Substring #1 ")
Try
sb.Insert(0, "Substring #2 ", 1)
Catch e As OutOfMemoryException
Console.WriteLine("Out of Memory: {0}", e.Message)
End Try
End Sub
End Module
輸出:
Out of Memory: Insufficient memory to continue the execution of the program.
示例3: Main
' 導入命名空間
Imports System.Collections.Generic
Module Example
Public Sub Main()
Dim values() As Double = GetData()
' Compute mean.
Console.WriteLine("Sample mean: {0}, N = {1}",
GetMean(values), values.Length)
End Sub
Private Function GetData() As Double()
Dim rnd As New Random()
Dim values As New List(Of Double)()
For ctr As Integer = 1 To 200000000
values.Add(rnd.NextDouble)
If ctr Mod 10000000 = 0 Then
Console.WriteLine("Retrieved {0:N0} items of data.",
ctr)
End If
Next
Return values.ToArray()
End Function
Private Function GetMean(values() As Double) As Double
Dim sum As Double = 0
For Each value In values
sum += value
Next
Return sum / values.Length
End Function
End Module
輸出:
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.
示例4: Main
' 導入命名空間
Imports System.IO
Module Example
Public Sub Main()
Dim result As Tuple(Of Double, Long) = GetResult()
Console.WriteLine("Sample mean: {0}, N = {1:N0}",
result.Item1, result.Item2)
End Sub
Private Function GetResult As Tuple(Of Double, Long)
Dim chunkSize As Integer = 50000000
Dim nToGet As Integer = 200000000
Dim rnd As New Random()
' Dim fs As New FileStream(".\data.bin", FileMode.Create)
' Dim bin As New BinaryWriter(fs)
' bin.Write(CInt(0))
Dim n As Integer
Dim sum As Double
For outer As Integer = 0 To CInt(Math.Ceiling(nToGet/chunkSize) - 1)
For inner = 0 To Math.Min(nToGet - n - 1, chunkSize - 1)
Dim value As Double = rnd.NextDouble()
sum += value
n += 1
' bin.Write(value)
Next
Next
' bin.Seek(0, SeekOrigin.Begin)
' bin.Write(n)
' bin.Close()
Return New Tuple(Of Double, Long)(sum/n, n)
End Function
End Module
輸出:
Sample mean: 0.500022771458399, N = 200,000,000