user1195623
user1195623

Reputation: 11

What's the difference between memory allocation and garbage collection, please?

I understand that 'Garbage Collection' is a form of memory management and that it's a way to automatically reclaim unused memory.

But what is 'memory allocation' and the conceptual difference from 'Garbage Collection'?

Upvotes: 1

Views: 4400

Answers (5)

Marlon
Marlon

Reputation: 20332

You want a book. You go to the library and request the book you want. The library checks to see if they have the book (in which case they do) and you gladly take it and know you must return it later.

You go home, sit down, read the book and finish it. You return the book back to the library the next day because you are finished with it.

That is a simple analogy for memory allocation and garbage collection. Computers have limited memory, just like libraries have limited copies of books. When you want to allocate memory you need to make a request and if the computer has sufficient memory (the library has enough copies for you) then what you receive is a chunk of memory. Computers need memory for storing data.

Since computers have limited memory, you need to return the memory otherwise you will run out (just like if no one returned the books to the library then the library would have nothing, the computer will explode and burn furiously before your very eyes if it runs out of memory... not really). Garbage collection is the term for checking whether memory that has been previously allocated is no longer in use so it can be returned and reused for other purposes.

Upvotes: 1

hcarver
hcarver

Reputation: 7234

Memory allocation asks the computer for some memory, in order to store data. For example, in C++:

int* myInts = new int[howManyIntsIWant];

tells the computer to allocate me enough memory to store some number of integers.

Another way of doing the same thing would be:

int myInts[6];

The difference here is that in the second example, we know when the code is written and compiled exactly how much space we need - it's 6 * the size of one int. This lets us do static memory allocation (which uses memory on what's called the "stack").

In the first example we don't know how much space is needed when the code is compiled, we only know it when the program is running and we have the value of howManyIntsIWant. This is dynamic memory allocation, which gets memory on the "heap".

Now, with static allocation we don't need to tell the computer when we're finished with the memory. This relates to how the stack works; the short version is that once we've left the function where we created that static array, the memory is swallowed up straight away.

With dynamic allocation, this doesn't happen so the memory has to be cleaned up some other way. In some languages, you have to write the code to deallocate this memory, in other it's done automatically. This is garbage collection - some automatic process built into the language that will sweep through all of the dynamically allocated memory on the heap, work out which bits aren't being used and deallocate them (i.e. free them up for other processes and programs).

So: memory allocation = asking for memory for your program. Garbage collection = where the programming language itself works out what memory isn't being used any more and deallocates it for you.

Upvotes: 0

jondavidjohn
jondavidjohn

Reputation: 62412

They are Polar opposites. So yeah, pretty big difference.

Allocating memory is the process of claiming a memory space to store things.

Garbage Collection (or freeing of memory) is the process of releasing that memory back to the pool of available memory.

Many newer languages perform both of these steps in the background for you when variables are declared/initialized, and fall out of scope.

Upvotes: 6

user418748
user418748

Reputation:

A simple pseudo-code example:

void myFoo()
{    
    LinkedList<int> myList = new LinkedList<int>();
    return;
}

This will request enough new space on the heap to store the LinkedList object. However, when the function body is over, myList dissapears and you do not have anymore anyway of knowing where this LinkedList is stored (the memory address). Hence, there is absolutely no way to tell to the system to free that memory, and make it available to you again later.

The Java Garbage Collector will do that for you automatically, in the cost of some performance, and with also introducing a little non-determinism (you cannot really tell when the GC will be called).

In C++ there is no native garbage collector (yet?). But the correct way of managing memory is by the use of smart_pointers (eg. std::auto_ptr (deprecated in C++11), std::shared_ptr) etc etc.

Upvotes: 1

Matteo Italia
Matteo Italia

Reputation: 126967

Memory allocation is the act of asking for some memory to the system to use it for something.

Garbage collection is a process to check if some memory that was previously allocated is no longer really in use (i.e. is no longer accessible from the program) to free it automatically.

A subtle point is that the objective of garbage collection is not actually "freeing objects that are no longer used", but to emulate a machine with infinite memory, allowing you to continue to allocate memory and not caring about deallocating it; for this reason, it's not a substitute for the management of other kind resources (e.g. file handles, database connections, ...).

Upvotes: 3

Related Questions