当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET OutOfMemoryException类代码示例

本文整理汇总了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
开发者ID:VB.NET开发者,项目名称:System,代码行数:23,代码来源:OutOfMemoryException

输出:

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
开发者ID:VB.NET开发者,项目名称:System,代码行数:14,代码来源:OutOfMemoryException

输出:

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
开发者ID:VB.NET开发者,项目名称:System,代码行数:32,代码来源:OutOfMemoryException

输出:

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
开发者ID:VB.NET开发者,项目名称:System,代码行数:33,代码来源:OutOfMemoryException

输出:

Sample mean: 0.500022771458399, N = 200,000,000


注:本文中的System.OutOfMemoryException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。