Does delete in C++ work the same way as garbage collection in Java? I mean, the memory management part internally (what really happens in heap in both the cases?).
-
1I'm sure if you looked hard enough you could have found this answer your self. – Wyatt Lowery Aug 25 '15 at 03:52
-
1I guess you already know that one is triggered by you via `delete` and will actually instantly destroy the object, whereas the java GC runs whenever it feels like it – WorldSEnder Aug 25 '15 at 03:54
-
1Yes. I do know that. I am looking for internal details and if they actually work the same way. – San Aug 25 '15 at 03:58
-
Java GCs often move allocated memory around during a GC, but with C++'s memory management that can't be done efficiently. (Allocated memory stays in place until deallocated) – user253751 Aug 25 '15 at 04:02
-
@immibis i think you're the victim of a few large misconceptions : neither can memory be "moved" at all (it can only be reallocated or copied and deleted) nor are operations on allocated fields "inefficient" in C++ to any extent. In fact, you will have a hard time finding a language construct which is as efficient as C or even C++ - this has been proven many, many times, even long before you were born. Just saying. The only languages which can actually beat C/C++ are ASM and ASM-like dialects. There are a few COMPARABLE languages - but AFAIR exactly zero which surpass it. – specializt Aug 25 '15 at 05:00
-
@specializt `neither can memory be "moved" at all` - you know perfectly well what I mean: copying GCs move *objects*. `nor are operations on allocated fields "inefficient"` - moving an object is, because you'd have to find all pointers to it. – user253751 Aug 26 '15 at 01:59
-
@immibis i recommend doing a bit of research about languages like C and C++, apparently you dont know much about them. There is no GC in C/C++. At all. If you're using the CLR with C++ you may create objects which are managed by the .NET VM BUT these are also highly efficient - no pointers have to be "searched" because literally all of them are in ordered, optimized maps, lists and sets. I think its time to stop assuming things now - C/C++ never has been, isnt and probably never will be inefficient in any way. These languages were MADE to be efficient wherever possible. – specializt Aug 26 '15 at 04:38
-
@specializt Now I have no clue what you're on about. I said that Java may use a copying GC which moves objects ("memory"), and that a copying GC isn't feasible for C++. That is one way in which memory management in Java and C++ works differently internally, beyond the obvious difference that Java is garbage-collected. – user253751 Aug 26 '15 at 04:41
-
so you really think a copying GC isnt "feasible". In C++. A language which allows for pretty much anything - and even efficiently if your design is accordingly. Thats a facepalm. – specializt Aug 26 '15 at 04:45
-
@immibis the "unfeasible" GC is about 10 years old now : https://msdn.microsoft.com/en-us/library/ee787088(v=vs.110).aspx#generations. Do not make assumptions about topics you have no knowledge of - that will never end well. – specializt Aug 26 '15 at 05:02
-
1@specializt ... so is that for C++? I see that's about the .NET garbage collector, which runs various languages including C++/CLI (which is not C++). – user253751 Aug 26 '15 at 05:24
-
@immibis you're contradicting yourself in your own words. C++/CLR (CLI is something different) is C++ and C++/CLR is able to run as managed code - which is being garbage collected. Please stop spreading your assumptions now - its getting really ridiculous. – specializt Aug 26 '15 at 06:18
-
@immibis plus : C++/CLI (managed only) is also standard C++ - its simply a restricted subset of C++/CLR whereas C++/CLR can mix managed and unmanaged types although mixing is not recommended ... i usually tend to seperate my managed and unmanaged parts very strictly, in fact compiler options like "verifiable" and "strict" are advisable in order to completely avoid memory leaks and other problems. – specializt Aug 26 '15 at 06:34
5 Answers
delete call in C++ releases memory on Free Store (heap) occupied by a variable using new operator. and we do all this manually (until we using smart pointers, C++ 11 and so on), for each new we must do corresponding delete. Garbage Collector is present only in C++ 11 and above. Garbage Collection in other languages (Java, C#) frees memory occupied by variable automatically. Behind the scene Garbage collector keeps reference count of variable and when reference count reaches 0 (or variable goes out of scope) it frees memory occupied by that variable.

- 1,460
- 9
- 22
-
-
reference count is maintained for every object on free store, according to that count GC works – Ali Kazmi Aug 25 '15 at 04:00
-
What is your reason for claiming that Java garbage collection is reference count based? My experience is that it does not act that way. – Patricia Shanahan Aug 25 '15 at 06:06
In C++, the functionality of the delete
, delete[]
, and new
operators can be defined through operator overloading, so you can make it work however you want.
In Java's heap behavior is defined by the JVM, and in general, as long as no references to an object exist in memory, it will be cleared out by the garbage collector eventually. See this for more details on Java's garbage collection.

- 626
- 1
- 6
- 17
-
-
Note that, for C++, you can find a lot of different libraries to do dynamic memory allocation, allowing you to choose one that suits your application. [Here](http://g.oswego.edu/dl/html/malloc.html) is one that's popular. – Charles Spencer Aug 25 '15 at 05:21
Actucally Garbage collection is automated . VM is automatically doing that. You can use System.gc() to force VM to do garbage collection.

- 411
- 3
- 15
-
4Actually, it's not guaranteed to actually perform garbage collection, although it usually does. See [this question](http://stackoverflow.com/questions/66540/when-does-system-gc-do-anything). – Charles Spencer Aug 25 '15 at 04:00
-
Allocation and deallocation occurs on the heap. Let's take an example here. In C++ STL containers use an object called an allocator
that does memory management. It allocates memory on the heap (lets assume it goes any random place that is not occupied) and then deallocates it. Now the allocator can have a map, or an array or some sort of data structure with which it can tell if there is some memory that is free, i.e. some memory that it has allocated and deallocated before. It can keep track of this itself, how it does this is implementation defined. All the programmer and the standard specify is that it just does this (and some restrictions therein) so as a programmer you should usually not worry about how heap allocation is done. The compiler can optimize code and make it behave differently in different situations.
Now going back to your question about delete in C++ and garbage collection in Java. C++ compiles down to raw machine executable code, i.e. code that is run natively by your machine (assembly code) while Java compiles down to "bytecode", "bytecode" is executed by the Java Virtual Machine (JVM), which itself is a program. delete
in C++ makes the machine deallocates memory on the heap. In java delete happens automatically when an object doesn't have any references left to it and when garbage collection happens. The JVM here is like the allocator
it "deallocates" memory.
So memory is marked deallocated. It can be reused. How that happens at machine level can vary extensively. You can even make the operators do what you want by overloading them! Hope that helps :)
In C++: new and delete are used to create and delete a "space". This space is mainly for storing your data. usually use stack to store some return value, local variables, function parameters and so on, heap is another space that usually created by programmers. However, there are others space like static which is used to store global variables and static value or constant. In C, we usually use malloc/free, In C++ we use new/delete, you should know malloc() just is a function but new() is a constructor.
Time *t;
t = new Time(0,0,0,/"t");//this allowed you create a space in heap by using constructor
now there is a space in heap and p is point at this address.
delete t;// this delete is used to delete the pointer, which means this space won't be used anymore, after a short time, C++ will recycle this space.
JAVA: In java you should know JVM and GC. JVM has 5 parts:
- program counter(used by thread)
- VM stack(store primary variables and so on)
- native method are(OS method)
- Method are(contain runtime constant pool)
- HEAP !!!(GC mainly works here)
In JVM, there are mainly three parts in JVM's heap:
- Eden or young generation(used to store the latest created object)
- Tenuerd (old) generation(used to store some objects which is always used)
- permanent generation(after Jdk 8 version it's merged into old generation) Anyway, hope you have some idea about java heap, if not, you should now most objects created and store in there.
How GC work: first user create a object
Object o = new Object();
o has a address and is stored in heap(usually object has 8 bytes for identify such as class information), at the very beginning, it store in young generation, there are two register call "from" and "to" in young generation which are identical. we have o now, there is a counter of this object, we create it the counter is 1, next time it used, the counter goes to 2, when counter change young generation will wipe the others objects(not activated objects), o will be stored in "from", next time objects in register "from" will be copied to "to",until the counter increased to 8, it will be copied to on generation.
- old generation use mark and wipe algorithm. The first step in the process is called marking. This is where the garbage collector identifies which pieces of memory are in use and which are not. Second is Normal delete,normal deletion removes unreferenced objects leaving referenced objects and pointers to free space.To further improve performance, in addition to deleting unreferenced objects, you can also compact the remaining referenced objects. By moving referenced object together, this makes new memory allocation much easier and faster. So far, the heap use this strategy, it can clean it memory and keep objects which is always been used.
Any suggestion is welcomed and appreciate, hope it can help you understand.

- 580
- 2
- 13