Difference in allocating memory when static or local

I have a static array of struct that I use in a lot of code. Recently I have found need to try and create this variable with an array size over 27M. I am able to do this, as a test, if I create a local variable, but my code requires that it is static and when I try to create it - I get an OutOfMemory Exception.

My machine has 4G ram and can easily hold the array.

How might I get around this problem

Moon



Answer this question

Difference in allocating memory when static or local

  • KimberlyL

    Hi

    what is the value of recsize

    it could just be that you are running out of memory,

    so could you not write the values to disk reclaiming them in memory (and disposing of them) as needed



  • samaya78

    That's 10GB of 40 byte records = 400GB. Clearly, that's not going to work.

  • hye_heena

    Sure Frederikm,

    Here it is cut down:

    public struct MyStruct

    {

    public double DateTime DT;

    public string Name;

    public bool Flag;

    public int Code;

    public double Price;

    }

    public static MyStruct[] Data;

    MyStruct Dummy = new MyStruct();

    int RecSize = Marshal.SizeOf(Dummy);

    long FileLength = 10000000000;

    Data = new MyStruct[FileLength/RecSize]; // This is line weher I get memory exception


  • deadman_dexter

    Guys,

    You are all correct. The code I gave is just a mock up.

    There is no string in my real code, and the file size is not so big. The file size of my data on disc is 1G, as a binary file - I read that straight in. For smaller files the memory required is approx the same once loaded into memory.

    I am tryning to get away without restructuring my code. I just would like to allocate the mem better - which may be something that should be done in bios or the XP OS rather than c# at all...


  • Zeldacat

    > But you don't have 4GB unless you're running 64bit Windows

    Even if you have a 64bit version of windows at the moment most of the current chipsets cannot handle 4GB of address range so you still won't be able to see it all so you have to be careful or you might be spending money on RAM you can't use. I know one or two people who got hit by this limitation.

    Mark.



  • Kevin Rodgers

    If I count the zero's correct your reserving space for 10 GB. Also remember that when you start filling the structs, the value for the string field is stored as a reference and the string itself will need extra space from the heap.

  • ravi_9793

    recsize it 40 buytes.

    No I need it in memory for speed.

    I have 4G ram - the total amount of memory I am trying to use is only 1G.


  • DTHMTLGOD

    Hi

    could you post the code of the struct and the array



  • mobigital

    But you don't have 4GB unless you're running 64bit Windows, you only have 2GB, since Windows takes the upper 2GB of the address space. In addition, .NET consumes a large portion of the memory too, so your actual memory available to your app is between 800MB and 1.2GB depending on .NET's usage, which means you might very well be running out of memory.



  • MotteKarotte

    It seems that it has occupied much more than the available heap memory according to your code...

  • Difference in allocating memory when static or local