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

I'm trying to find the limit, if there is one, of the number of objects you can create.

Logically there is a limit and this limit would be that of the capacity of the RAM but to test concretely I established this piece of code and I let it run

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

I let this code run for a while and it didn't crash, so I thought of a possible limitation like :

  • RAM Amount
  • OS Architecture (32/64bits)
  • Memory Swap on/off

But these are just guesses, my teacher wants the limit and proof of it, can someone help me or direct me please?



Solution 1:[1]

Try keeping all the objects you create. Replace your code with this

    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()

I suspect you'll crash with out of memory.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1