Thread: How much heap memory can I allocate under windows?

  1. #1
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181

    How much heap memory can I allocate under windows?

    My C++ program allocates memory in the heap to the order of 1.64 GB. Under task manager performance, it quotes free physical memory at 2.43 GB. If I allocate anything over 1.64 GB, I receive a runtime error of "bad alloc". What is using the 0.79 GB and how do I squeeze extra memory for my programme? I get a feeling the rabit hole goes alot deeper than merely looking at the two memory figures quoted above.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When allocating memory on the heap, there must be a contiguous free range of memory matching the size you requested. If there isn't, the operation fails.
    In a 32-bit program in Windows, you have 2 GB of virtual memory to play with. Due to how Windows works, there are bits and pieces of stuff mapped into this virtual memory at different places, ie fragmentation.
    This could easily explain why it fails.

    But first, why do you need so much memory? Could you get by with less? If so, that would be ideal.
    If not, then consider testing these things:
    - Can you allocate several chunks of lesser size so that their total size exceeds 1.64 GB? Remember that you can only allocate 2 GB minus overhead for your application if it's 32 bits.
    - Try compiling it as a 64-bit executable. That should give you more memory.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2012, 04:28 AM
  2. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  3. Replies: 3
    Last Post: 10-19-2008, 03:35 PM
  4. allocate memory function
    By sunoflight77 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 10:38 AM
  5. when to allocate memory
    By SAMSAM in forum Windows Programming
    Replies: 3
    Last Post: 01-22-2003, 11:40 PM