How to allocate 1MB in VB.NET

3 Answers

0 votes
Imports System

Public Class Program
    Public Shared Sub Main(ByVal args As String())
        Dim buffer As Byte() = New Byte(1024 * 1024) {} ' 1MB = 1,048,576 bytes
		
        Console.WriteLine("Allocated 1MB of memory.")
    End Sub
End Class


' run:
'
' Allocated 1MB of memory.
'

 



answered May 19, 2025 by avibootz
edited May 19, 2025 by avibootz
0 votes
Imports System
Imports System.IO

Class Program
	Public Shared Sub Main()
        Using ms As MemoryStream = New MemoryStream(New Byte(1048575) {}) ' 1MB = 1,048,576 bytes
        End Using

        Console.WriteLine("Allocated 1MB using MemoryStream.")
    End Sub
End Class



' run:
'
' Allocated 1MB using MemoryStream.
'

 



answered May 19, 2025 by avibootz
0 votes
Imports System
Imports System.Runtime.InteropServices

Class Program
	Public Shared Sub Main()
		' Allocate 1MB = 1,048,576 bytes in unmanaged memory
        Dim ptr As IntPtr = Marshal.AllocHGlobal(1024 * 1024)
		
        Console.WriteLine("Allocated 1MB of unmanaged memory.")
        Marshal.FreeHGlobal(ptr)
    End Sub
End Class



' run:
'
' Allocated 1MB of unmanaged memory.
'

 



answered May 19, 2025 by avibootz

Related questions

3 answers 229 views
2 answers 198 views
3 answers 213 views
1 answer 146 views
146 views asked May 20, 2025 by avibootz
3 answers 224 views
3 answers 233 views
233 views asked May 20, 2025 by avibootz
1 answer 125 views
125 views asked May 20, 2025 by avibootz
...