Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can garbage collection coexist with explicit memory management?

For example, say one was to include a 'delete' keyword in C# 4. Would it be possible to guarantee that you'd never have wild pointers, but still be able to rely on the garbage collecter, due to the reference-based system?

The only way I could see it possibly happening is if instead of references to memory locations, a reference would be an index to a table of pointers to actual objects. However, I'm sure that there'd be some condition where that would break, and it'd be possible to break type safety/have dangling pointers.

EDIT: I'm not talking about just .net. I was just using C# as an example.

like image 319
TraumaPony Avatar asked Oct 25 '08 12:10

TraumaPony


People also ask

Which part of memory is involved in garbage collection?

The managed heap After the CLR initializes the garbage collector, it allocates a segment of memory to store and manage objects. This memory is called the managed heap, as opposed to a native heap in the operating system.

Which garbage collection a program can never run out of memory?

No, garbage collection cannot guarantee that your application will not run out of memory.

What is memory management and garbage collection?

In computer science, garbage collection (GC) is a form of automatic memory management. The garbage collector attempts to reclaim memory which was allocated by the program, but is no longer referenced; such memory is called garbage.

What are the limitations of garbage collection?

Drawbacks of garbage collection in Java Garbage collectors bring some runtime overhead that is out of the programmer's control. This could lead to performance problems for large applications that scale large numbers of threads or processors, or sockets that consume a large amount of memory.


2 Answers

You can - kind of: make your object disposable, and then dispose it yourself.

A manual delete is unlikely to improve memory performance in a managed environment. It might help with unmanaged ressources, what dispose is all about.

I'd rather have implementing and consuming Disposable objects made easier. I have no consistent, complete idea how this should look like, but managing unmanaged ressources is a verbose pain under .NET.


An idea for implementing delete: delete tags an object for manual deletion. At the next garbage collection cycle, the object is removed and all references to it are set to null.

It sounds cool at first (at least to me), but I doubt it would be useful. This isn't particulary safe, either - e.g. another thread might be busy executing a member method of that object, such an methods needs to throw e.g. when accessing object data.

like image 58
peterchen Avatar answered Nov 10 '22 05:11

peterchen


With garbage collection, as long as you have a referenced reference to the object, it stays alive. With manual delete you can't guarantee that.

Example (pseudocode):

obj1 = new instance;
obj2 = obj1;

// 

delete obj2;
// obj1 now references the twilightzone.

Just to be short, combining manual memory management with garbage collection defeats the purpose of GC. Besides, why bother? And if you really want to have control, use C++ and not C#. ;-).

like image 44
Toon Krijthe Avatar answered Nov 10 '22 06:11

Toon Krijthe