dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Out of memory problem

  1. #1
    Join Date
    Dec 2010
    Posts
    907

    Out of memory problem

    Code:
    void TimeAStar::SetupPathTileTypeNodes() {
    	for (int i = 0; i < MAXTILE_XLEN; i++) {
    		for (int j = 0; j < MAXTILE_YLEN; j++) {			
    			for (int k = 0; k < 8; k++) {
    				for (int l = 0; l < 128; l++) {
    					PathTileNodeTypeEx* newNode = new PathTileNodeTypeEx(i, j, k, 0);
    					mPathTileNode[i][j][k][l] = newNode;
    				}
    			}
    		}
    	}
    }
    If every 128 chunks of the last iteration is the same, how can I mimimize the memory usage in
    this case, I got 16G ram, still run out of storage.
    192x192x128x8x sizeof(PathTileNodeTypeEx)

    Maybe I'll try hash maps, unordered maps
    Thanks
    Jack
    Last edited by lucky6969b; February 29th, 2016 at 03:28 AM.

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Out of memory problem

    Quote Originally Posted by lucky6969b View Post
    192x192x128x8x sizeof(PathTileNodeTypeEx)
    even assuming no fragmentation and very optimistic free space, this means at most ~1k bytes for each PathTileNodeTypeEx instance, are you sure 16G is enough ?

    anyway, if PathTileNodeTypeEx is very small, you can avoid fragmentation by using a memory pool, or by using an array, or by replacing PathTileNodeTypeEx with a class exposing a view of PathTileNodeTypeEx-like objects ( and managing a flat memory layout internally )

    if avoiding fragmentation is not enough, you may exploit redundancies/symmetries in your data to reduce memory usage, see memoization techniques, sparse data structures, or the flyweight pattern for an object oriented approach

  3. #3
    Join Date
    Dec 2010
    Posts
    907

    Re: Out of memory problem

    Hello,
    I decided to allocate stuff at runtime with unordered maps, which is still good enough,
    If I preallocate with unordered map, I still don't get the benefits of preserving memory...
    So I just do it on the fly...
    Thanks
    Jack

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Out of memory problem

    Quote Originally Posted by lucky6969b View Post
    Hello,
    I decided to allocate stuff at runtime with unordered maps, which is still good enough,
    If I preallocate with unordered map, I still don't get the benefits of preserving memory...
    So I just do it on the fly...
    Thanks
    Jack
    Wait, you were declaring your table on the stack !? That would never work in a million year. Your table alone would be about 300MB, and a stack is usually no bigger than 1-4 MB. You might have more luck declaring it globally, but it would still be a very bad idea.

    You mention "pre-allocating with unordered map". What exactly does that mean?

    If you are going to prealocate anyways, just allocate a "new PathTileNodeTypeEx[192*192*128*8]" array, and then wrap it up to look like a 4D array. Even if the array is sparse, it's not THAT much memory.

    NB. You'll usually want to wrap such things in a class, where you can later access things with the syntax "mPathTileNode.get(x, y, z, t)". This is usually a better approach, for multiple reasons.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)