What is Garbage Collection And Memory Management

Hey guys

I keep on hearing these two concepts above as special features to C++ I haven't reached te chapter in my book for memory management


What is memory management

And Garbage Collection

I just want to know if

Is both of these hard
What is it
Why should I use it

Thank you for reading means alot to me
The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and free it for reuse when no longer needed.

... and free it for reuse when no longer needed
Garbage collection it trying to automate this part.
Last edited on
Why does the word allocate mean

Sorry I am a kid
If your program want's some memory, it asks the memory manager for some. The memory manager goes off and gets some, or tells you it can't. This act is called allocation.

Why do things work this way? That's explained here:
Amazing explanation

Where can I learn memory management
And garbage collection

What is garbage collection

I don't understand the word automate -_-
Lots of good resources on the web to learn memory management.
Here's one:

Garbage collection is used in languages like Java, but not C++.
In C++, the user is responsible for managing (deleting) any memory he allocates.

Automate = automatically.
Thanks a lot

I will learn memory management

So basically no garbage collection in C++
Not in the sense that Java does.

The C++ heap does coalesce adjacent free blocks, but that's not really garbage collection.

|Free block | used block | free block |
When the block in the middle is released, the three blocks are combined to form a single block.
|------------Free block---------------|
Last edited on
Check out RAII.
RAII is smart pointers right

and is it important to learn low level concepts


So bassically memory management is basically asking for some memmory in your programmes this is called allocation and well thats it is that right
Last edited on
This might sound like nonsense to you, but RAII is the idea of using an OOP language's encapsulation features to manage your memory and resources so that you don't have to.

C++ has these things called "constructors" and "destructors". They are special functions for classes which are automatically called when the object is created and destroyed. This lets you do any cleanup necessary for the object.

For example:

// without RAII, if we want to dynamically allocate 10 ints, we'd do
//   something like this:

int* ar = new int[10];  // allocate the array

ar[0] = 0;  // use it

delete[] ar;  // clean up when done. 

If you leave out that last delete[] line, you will leak memory -- as the memory you allocated will not get released.

So if you forget to delete, or if something happens that interrupts your code before you get a chance to delete (like an exception, an early return/break command, or a goto), you will leak memory.

RAII says.. let the destructor do the cleanup so that it is done automatically. This will make it impossible to forget. Also, destructors are called even when code is interrupted, so it will be guaranteed to cleanup, making a leak impossible:

// An overly simplistic RAII example.  DO NOT USE THIS CLASS -- use std::vector
//  instead:
class Array  // our array class, which will be an RAII container for allocating ints
    int* data;  // our data -- this will point to the actual array

    Array( int size )  // our constructor, which takes the number of ints we want to allocate
        data = new int[size];

    ~Array()  // our destructor, which will clean up.
        delete[] data;

    int& operator [] (int index)  // overloading the [] operator so we can access the array
    {                  // from outside the class
        return data[index];

// Now that we have this RAII class, we can use it:

Array ar(10);  // create an array of 10 ints.  This calls the constructor

ar[0] = 0;  // use it

// no need to clean up... the destuctor will do it automatically. 

RAII is smart pointers right

Smart pointers use RAII.

and is it important to learn low level concepts

Not immediately, no. Though you'll want to learn them eventually. If you stick to using smart pointers and container classes, you are very unlikely to leak memory.

So bassically memory management is basically asking for some memmory in your programmes this is called allocation and well thats it is that right


Allocation is asking for some memory.

Memory management is being responsible with the memory you have already allocated. IE if you allocate memory and never free it, then that is poor memory management. But if you use RAII to ensure you never have any leaks, then that is good memory management.
Last edited on
So bassically memory management is basically asking for some memmory in your programmes this is called allocation and well thats it is that right

Asking for memory is not memory management. That's memory usage.

There are a couple of levels to memory management. First, at the application level there is making sure that you return any memory that you request so that you don't create memory leaks.

There is also the issue of managing the lifetime of memory that you request. You should return memory as soon as it is no longer needed. In single threaded programs, this is usually trivial. When getting into multi-threaded programs, this is not so trivial. You have to be sure the memory you're returning is no longer referenced any any other objects.

Second aspect of memory management is at the run-time library level. The run-time library must allocate memory is such a way as to minimize fragmentation and coalesce blocks of memory when objects are deleted as I described earlier.

Last edited on

will the tutorials on this website help me learn all these stuff from beggining to advance

and do you know any tutorials for c++

please could you recomend me tutorials so i can reach a advance level of C++ like you
i want to learn all this stuff from basic to the subjects metioned her

anyone can you guys suggest a tutorial
do you know any tutorials for c++

This site has an excellent tutorial for C++, but it does not cover memory management beyond allocating and deleting objects.
Last edited on
Thanks after the tut on this page where else should i look to further my skills
Any other tuts on memory management and basic stuff and oop and raii
Topic archived. No new replies allowed.