vb.net 您可以创建的对象数量有限制吗?

vb.net Is there a limit to the number of objects you can create?

我正在尝试找出您可以创建的对象数量的限制(如果有的话)。

逻辑上有一个限制,这个限制是 RAM 的容量,但为了具体测试,我建立了这段代码,我让它 运行

Module Module1
    Sub Main()
        Dim newObj As New classTest.classTest
        Dim i As Int64
        Dim MAX As Int64
        MAX = 9223372036854775807 ' max 64 bits int
        For i = 1 To MAX
            newObj = New classTest.classTest
            Console.WriteLine(i)
        Next
        Console.ReadLine()
    End Sub
End Module

我让这个代码 运行 了一段时间,它没有崩溃,所以我想到了一个可能的限制,比如:

但这些只是猜测,我的老师想要它的极限和证明,请有人帮助我或指导我吗?

尝试保留您创建的所有对象。用这个替换你的代码

    Dim newObj As classTest.classTest
    Dim newObjCollection As New List(Of classTest.classTest)
    Dim i As Int64
    Dim MAX As Int64 = Int64.MaxValue
    For i = 1L To MAX
        newObj = New classTest.classTest
        newObjCollection.Add(newObj)
        Console.WriteLine(i)
    Next
    Console.ReadLine()

我怀疑你会因为内存不足而崩溃。