LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   what does garbage collected mean (https://www.linuxquestions.org/questions/programming-9/what-does-garbage-collected-mean-356705/)

Garda 08-24-2005 10:11 PM

what does garbage collected mean
 
i've heard of languages that are garbage collected. i think java is an
example. what does this mean

nadroj 08-24-2005 10:15 PM

its a method to reclaim memory, from unused objects.. more or less.
read this
java has automatic collection, c++ might be manual... im not positive, i dont use c++, only java.

IBall 08-25-2005 12:38 AM

C / C++ certainly do not have Garbage Collection.

Ass far as I know, it is to free up memory on the heap from unused objects. I think it also has something to do with pointers - If you dynamically allocate an array using malloc or calloc, you must remember to use free to release the memory.

Java does have automatic garbage collection - when there are no references to any objects, Java knows that it can safely remove these from memory, and will do so when it feels like it.

I hope this helps
--Ian

lowpro2k3 08-25-2005 01:54 AM

In C++ this function would cause a memory leak:

Code:

void memoryleak()
{
    string * s = new string;
    s = "Hello World";
    cout << s << endl;
}

It generates a memory leak because the string "Hello World" is allocated on the heap. Pointer 's' is created on the stack when the function is called. s points to "Hello World", but s is lost when the function returns. Basically s was the last reference to this heap memory, and now it wont be found and a memory leak occurs. Java would notice this lack of references and perform the cleanup itself, probably by calling the string destructor when all references are destroyed.


All times are GMT -5. The time now is 04:56 AM.