Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8796131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:33:06+00:00 2026-06-13T23:33:06+00:00

I am experimenting with Marshal.AllocHGlobal and found puzzling that this code would not succeed,

  • 0

I am experimenting with Marshal.AllocHGlobal and found puzzling that this code would not succeed, instead it throws an OutOfMemory exception:

namespace HAlloc
{
    using System;
    using System.IO;
    using System.Runtime.InteropServices;

    class Program
    {
        static void Main(string[] args)
        {
            // large file ~ 800MB
            string fileName = @"largefile.bin";
            FileInfo fileInfo = new FileInfo(fileName);

            // allocation succeeds
            IntPtr p = Marshal.AllocHGlobal((int)fileInfo.Length);

            // OutOfMemory exception thrown here:
            Marshal.Copy(File.ReadAllBytes(fileName), 0, p, (int)fileInfo.Length);
            Marshal.FreeHGlobal(p);
        }
    }
}

Why would it get an OutOfMemory when the AllocHGlobal call succeeded?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:33:07+00:00Added an answer on June 13, 2026 at 11:33 pm

    Cause File.ReadAllBytes(fileName) also has to read the file which causes extra ~800 MB

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Was experimenting with interface inheritance and found this: interface String{} interface Object{} interface Exception{}
In experimenting with this question I created an example that I utterly do not
after experimenting with js's prototypal inheritance i've found that i'm not wild about the
While experimenting a bit with C++ templates I managed to produce this simple code,
im experimenting locally with jQuery and its load() function. I have this JS code:
While experimenting with this question on collections in Spring.NET , I discovered that Spring
After experimenting with an iterator block I noticed the generated IL code is not
I experimenting with jQuery. As I was trying I found out that I can't
When experimenting with (embedded) Apache Derby DB, I noticed that a fresh database, with
From experimenting with the r.js optimizer, it seems that there is no way for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.