• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Grain

Large array allocation & out of memory error

5 posts in this topic

I am having trouble with this prime number sieve running out of memory.
 
It fails at Values.Capacity = n; Previously Values was just an array defined like this : bool[] Values = new bool[n]; but had the same result. I am passing primesieve an int.MaxValue.
        static int[] Primesieve(int n)
        {
            List<bool> Values = new List<bool>();
            Values.Capacity = n;
            int[] primes = new int[(int)Math.Sqrt(n)];
            int primecount = 0;
            Values[0] = true;
            Values[1] = true;
            
            for (int i = 0; i < Math.Sqrt(n); i++)
            {
                if (Values[i] == false)
                {
                    for (int j = i * i; j < Values.Count; j += i)
                        Values[j] = true;
                    primes[primecount] = i;
                    primecount++;
                }
            }
            return primes;
        } 
I believed my problem to be that since they are bools, a value type they are being created on the stack so I did this to force heap allocation. but it still fails at the same place.
    

public class Reference<T>
{
    public T Ref;
}
static int[] Primesieve(int n) 
{ 
    List<Reference<bool>> Values = new List<Reference<bool>>();
    Values.Capacity = n; 
    int[] primes = new int[(int)Math.Sqrt(n)]; 
    int primecount = 0; 
    Values[0].Ref = true; 
    Values[1].Ref = true;
    for (int i = 0; i < Math.Sqrt(n); i++)
    {
        if (Values[i].Ref == false) 
        { 
            for (int j = i * i; j < Values.Count; j += i)
                Values[j].Ref = true;
            primes[primecount] = i;
            primecount++;
        }
    }
    return primes;
}
My machine has 16gb of ram running win7 64-bit and i even targeted the build specifically to x64. So I know it cat actually be running out of memory as int.MaxValue Booleans should only take around 2gb assuming .Net is not packing 8 of them into 1 byte,  and if it is then it should only really take up 265mb.
 
Or am I some how still failing to use the heap? Edited by Grain
0

Share this post


Link to post
Share on other sites
Yes, there is something you are missing.

First, when you request bools it still allocates in individual bytes a full byte for each bool. A few languages (notably c++) tried to go the route of making bit arrays into compact containers, and it fails horribly. Each bool takes a full byte for many very good reasons. You can search online for custom containers for bit arrays if you want a compact representation.

You are passing 2^31 for your array length. That's about 2.1 billion entires, or 2GB of space, not 256MB. That won't work. The design of .net limits the maximum size of objects.

On .net 4.5, the maximum size of an array is 56 bytes less than 2^21 if you are on a 64-bit build.
On .net before 4.5, the actual value is system dependent and varies between runs, but the limit is about 1.4 billion single-byte entries, even on 64-bit builds.
0

Share this post


Link to post
Share on other sites

There is a maximum object size in the CLR, even on 64-bit systems. There are a couple of possible solutions:

 

- Use a BitVector to avoid wasting 7/8 of the array:

static IEnumerable<int> Primesieve(int n)
{
    BitArray A = new BitArray(n);
    A.SetAll(true);
    A[0] = false;
    A[1] = false;

    for (int i = 2; i < Math.Sqrt(n); i++)
    {
        if (A[i])
        {
            for (long j = i * i; j < n; j += i)
            {
                A[(int)j] = false;
            }
        }
    }

    for (int i = 0; i < A.Length; i++)
    {
        if (A[i])
            yield return i;
    }
}

- Create a class that stores a large logical array as multiple chunks

- Use P/Invoke to allocate unmanaged memory (should be your last option)

Edited by lwm
1

Share this post


Link to post
Share on other sites

Or a memory mapped file, perhaps ?

 

Oh; and I believe a bool takes 4 bytes, sometimes, based on padding and word alignment imposed by the 32/64bit OS. I haven't checked this myself, but in general in .net there is no advantage in storing anything less than a full Int32 (shorts, bools etc may easily take a full 32bits), other than it being a more declarative coding style; again, please dont take this as a definitive answer.

 

Phillip

0

Share this post


Link to post
Share on other sites


I haven't checked this myself, but in general in .net there is no advantage in storing anything less than a full Int32 (shorts, bools etc may easily take a full 32bits), other than it being a more declarative coding style

 

For local variables this may be the case. But that's an implementation detail.

In an array of bools, every element is exactly one byte though.

For bool fields in classes/structs the bool will probably be padded to 32 bits for performance reasons but you can tell the CLR to only use one byte if you really want to.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0