1

I have 3 questions concerning memory allocation that I thought better to put into one question than 3.

  1. When memory is allocated as I understand, it is allocated on the heap, which is just 16mb. How hen do programs such as video games or modern browsers manage to use over 1GB?

  2. Since it is obviously possible for this much memory to be used, why can it not be allocated at the start? I have found the most I can allocate in High Level Assembly language is around 100MB. This is a lot more than 16MB, and far less than I have 3, so where does this limitation come from?

  3. Why allocate memory in the first place, rather than allocating variables and letting the compiler/system handle it?

1 Answer 1

7

When memory is allocated as I understand, it is allocated on the heap, which is just 16mb. How hen do programs such as video games or modern browsers manage to use over 1GB?

The heap can grow. It isn't limited to any value and certainly not 16MB. You can easily allocate 1GB of heap, just make a program test and you'll see.

Since it is obviously possible for this much memory to be used, why can it not be allocated at the start? I have found the most I can allocate in High Level Assembly language is around 100MB. This is a lot more than 16MB, and far less than I have 3, so where does this limitation come from?

I'm not sure why your OS isn't filling larger allocation requests. Perhaps due to memory fragmentation? It's going to be a problem specific to your setup, which you didn't share. I can allocation much more memory than that without an issue.

You can try to use the mmap system call if malloc (which uses the brk system call) is having some sort of issue. Note that for GNU libc, malloc actually uses mmap instead of brk when the allocation is large enough (over 128k I think).

Why allocate memory in the first place, rather than allocating variables and letting the compiler/system handle it?

Variable must live in memory somewhere. What you are saying is "why manually manage memory? Why can't some algorithm do that for me?". It is actually very common for the compiler and a runtime component to handle allocation/freeing - it's called garbage collection.

4
  • 1
    But it's not just garbage collection. There is a limited way to allocate memory automatically – it's called local variables.
    – svick
    Sep 7, 2011 at 19:37
  • For many languages, local variables are either optimised away, live very briefly in a register, or lives on the stack. Keep in mind this is all part of the language and/or language implementation - just because a variable is local doesn't mean the language doesn't place that value in the heap. Sep 7, 2011 at 20:54
  • @Thomas thankyou for your answer. I guess what I mean is why note just declare a variable, local, global or static containing the datta, e.g. textures for a video game, rather than allocating and using it later on?
    – Jason Sill
    Sep 8, 2011 at 17:10
  • @Jason You often don't know how many values you need. Take a spread sheet for example - how many cells should you allocate? 1024x1024 cells? What if the user wants to enter in a value at location 1025? This problem grows with the amount of uncertainty and users are very creative in making the problem explode. For example, in some video games you can get unlimited objects, such as mines. To record the location the user places an unbounded number of objects you need to dynamically allocate an unbounded amount of memory (or stop the game at some arbitrary bound). Sep 8, 2011 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.