C++ allocating large array on heap gives "out of memory exception"

10100111001

I am currently having a problem with declaring or filling a large array with data because I get a dialog box saying "Out of memory", originating from CMemoryException.

I am trying to create an array or vector (tried both) with around 50000 elements of an object, where sizeof(MyObjectClass) returns around 37000 bytes.

If I try to just fill up a vector or a CArray element by element, then I get around to filling with somewhere near 16000 elements before getting the Out Of Memory exception. That should be close to 600MBs?

I have 8GB RAM on the machine and only 4GB are being used according to Windows Task Manager. So the amount of physical RAM should not impose a problem. I am running C++ MFC in Visual Studio 2010, 32-bit.

Also if I try to write

MyObjectClass* heaparray = new MyObjectClass[50000];

then I immediately get that very same Out of memory error, on that very row.

Any ideas? Thank You in advance!

UPDATE: I have also tried to simply create a TestStruct with the fields:

struct TestStruct
{
  long long field1;
  GUID field2;
  GUID field3;
  GUID field4;
  TCHAR field5[256];
  TCHAR field6[4];
  TCHAR field7[258];
  TCHAR field8[1026];
  TCHAR field9[258];
  TCHAR field10[16386];
  TCHAR field11[258];
};

TestStruct* heapArr = new TestStruct[50000];

Still the same...I get a "Out of Memory" exception when executing the last line of code. Isn't one of the great things with the heap supposed to be possibility to be limited only by RAM (more or less) when handling big data. And yet...since it crashes already at 600MB of allocated space I cannot agree that that is very big data either...or should I? :/

Cory-G

This is a fun one. Both Vectors and arrays are stored contiguously in memory as stated here.

You are not only looking for 1850000000 bytes (1.72295 gigabytes) in memory, but one unbroken chunk of memory that big. That will be hard to find. If you switch to a different data structure that does not do contiguous storage (say a linked list) then you may be able to store that much.

Note: that will also make each object just a bit bigger.

What would be best would be to see if there is any way to just buffer the objects; load only the ones you will update and load the others on the fly when you need them. I have my doubts that you are doing cpu operations on more than one at a time. If you do it right (with threading most likely) you won't even suffer any slows from reading/writing them.

More information about what you are working on would be helpful. There may even be a way to just have an array filled with a type identifier, if your object has less than 2,147,483,647 (size of int) variations. You could store an array of integers that the class could be generated from (a toHash and fromHash that would be 50000 * 4 bytes = 195.312 kilobytes), that may work for you too. Again, it depends on what you are working on.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Out of Memory Exception when handling large files in C#

From Dev

C# Large JSON to string causes out of memory exception

From Dev

ADO Large DataSet throws out of memory exception

From Dev

Writing Large File To Disk Out Of Memory Exception

From Dev

C# out of memory exception in GetThumbnailImage on a server

From Dev

C++ Out of Memory Exception Test

From Dev

Preventing a heap out of memory exception presumably due to threads

From Dev

Preventing a heap out of memory exception presumably due to threads

From Java

Out of memory exception when decrypt large file using Cipher

From Dev

Compress large file using SharpZipLib causing Out Of Memory Exception

From Dev

Hibernate out of memory exception while processing large collection of elements

From Dev

Entity framework large data set, out of memory exception

From Dev

Hibernate out of memory exception while processing large collection of elements

From Dev

EF - Query Large Data Sets Causes Out Of Memory Exception

From Dev

Out of memory exception when using xlsx module with large files

From Dev

Allocating an 2D array in C in heap instead of stack

From Dev

Calling c++ delegate from c# out of memory exception

From Dev

Calling c++ delegate from c# out of memory exception

From Dev

C# WinForms Out of Memory Exception on Bitmap Clone

From Dev

c# Out of Memory Exception with System.Drawing.Image

From Dev

Out of memory exception while updating zip in c#.net

From Dev

C# out of memory Exception in System.Drawing.dll

From Dev

Why is C++ allocating such a large space in memory for my dynamic array?

From Dev

PS Old Gen memory in Heap Memory Usage: GC settings for Java Out Of Memory Exception

From Dev

Filling an array of structs and allocating memory on the heap

From Dev

C++ allocating class in stack, not in heap

From Dev

C# - DataTable Out of Memory exception in application to catch SQL Server "INSERT" events

From Dev

Randomly getting Argument Exception and out of memory exception when receiving images via tcp c#

From Dev

C# List<T> out of memory exception but far away from the 2Gb limit

Related Related

  1. 1

    Out of Memory Exception when handling large files in C#

  2. 2

    C# Large JSON to string causes out of memory exception

  3. 3

    ADO Large DataSet throws out of memory exception

  4. 4

    Writing Large File To Disk Out Of Memory Exception

  5. 5

    C# out of memory exception in GetThumbnailImage on a server

  6. 6

    C++ Out of Memory Exception Test

  7. 7

    Preventing a heap out of memory exception presumably due to threads

  8. 8

    Preventing a heap out of memory exception presumably due to threads

  9. 9

    Out of memory exception when decrypt large file using Cipher

  10. 10

    Compress large file using SharpZipLib causing Out Of Memory Exception

  11. 11

    Hibernate out of memory exception while processing large collection of elements

  12. 12

    Entity framework large data set, out of memory exception

  13. 13

    Hibernate out of memory exception while processing large collection of elements

  14. 14

    EF - Query Large Data Sets Causes Out Of Memory Exception

  15. 15

    Out of memory exception when using xlsx module with large files

  16. 16

    Allocating an 2D array in C in heap instead of stack

  17. 17

    Calling c++ delegate from c# out of memory exception

  18. 18

    Calling c++ delegate from c# out of memory exception

  19. 19

    C# WinForms Out of Memory Exception on Bitmap Clone

  20. 20

    c# Out of Memory Exception with System.Drawing.Image

  21. 21

    Out of memory exception while updating zip in c#.net

  22. 22

    C# out of memory Exception in System.Drawing.dll

  23. 23

    Why is C++ allocating such a large space in memory for my dynamic array?

  24. 24

    PS Old Gen memory in Heap Memory Usage: GC settings for Java Out Of Memory Exception

  25. 25

    Filling an array of structs and allocating memory on the heap

  26. 26

    C++ allocating class in stack, not in heap

  27. 27

    C# - DataTable Out of Memory exception in application to catch SQL Server "INSERT" events

  28. 28

    Randomly getting Argument Exception and out of memory exception when receiving images via tcp c#

  29. 29

    C# List<T> out of memory exception but far away from the 2Gb limit

HotTag

Archive