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

But

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.
http://en.wikipedia.org/wiki/Memory_management

... 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:
http://computer.howstuffworks.com/c21.htm
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:
http://en.wikipedia.org/wiki/Memory_management

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

EDIT

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:

1
2
3
4
5
6
7
8
// 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 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
{
private:
    int* data;  // our data -- this will point to the actual array

public:
    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


Kinda.

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
@Dich

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.
http://www.cplusplus.com/doc/tutorial/
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.