Garbage collector functioning


photo

Recommended Posts

I would like to know details about how the garbage collector works. 

 

I have this function

 

void foo(){
    int dummy[];
    for(int i=0; i<10000; i++){
        dummy.append(new XmlMessage(engine.getDataPath() + XML_FILENAME));
    }
}

According to the definition of the garbage collector, the objects inside dummy should be deleted by the GC, but instead, the program is running bigger and bigger in memory. Is strictly necessary to call delete on those objects in order to get the GC disposing them? What would happen if I create several objects in a thread and that thread dies later on? Is that memory garbage collected?

 

Thanks!

Link to post

Hi Daniel,

 

You should use delete() function after you called new() operator (in your case yo can use dummy.delete() in the end of foo() function). Please, check Memory Management article to get more details about this behavior. 

 

Also, you can use Vectors instead of Map to decrease memory footprint. All you need to do is to replace int dummy[] with XmlMessage dummy[0].

 

What would happen if I create several objects in a thread and that thread dies later on? Is that memory garbage collected?

 

If there will be no pointers to this values, then memory will be freed. Please, check this two small articles about garbage collector and destructors / delete operator usage.

 

Thanks!

How to submit a good bug report
---
FTP server for test scenes and user uploads:

Link to post