DEV Community

Discussion on: How does your language handle memory?

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

It has automatic collection, but it also uses a lot of manual techniques. For example it has using clauses, and it also follows a Dispose pattern. The need for Dispose in frameworks is due to not being able to "automatically" handle object disposal.

C++ uses a different pattern where objects can be disposed. It's got it's own problems of course, I just want to contradict the "best of both worlds" statement. :P

Collapse
 
galdin profile image
Galdin Raphael

The need for Dispose in frameworks is due to not being able to "automatically" handle object disposal.

I'm not sure if that's right. Garbage Collection is automatic. Disposing is usually done manually. Because the whole point of disposables is to control when the object resources are disposed.

Closing files for example. You'd want to do that manually. Why would one wait for a garbage collector to do it?

Thread Thread
 
mortoray profile image
edA‑qa mort‑ora‑y

It's considered one of the strong points of C++ that you don't need to manually cleanup resources of any kind, it doesn't make memory a special case.

The Dispose pattern is a contrast to this. Like C, all resources, except memory, must be manually released. Automatic "memory" management doesn't solve any of the problems of other resources, something that C++ does actually solve.

All resources are part of this discussion, and if we just ignore them it isn't a fair comparison between how languages handle it.

Thread Thread
 
galdin profile image
Galdin Raphael

aaah you're contrasting it with C++. Makes sense.

C# doesn't the C++ concept of automatic storage. In C# everything that's a reference-type is garbage collected. Garbage collection means that you don't have control over when the object is disposed. It doesn't generally matter, but sometimes it does. Like in files for example. This is where the disposable pattern comes in.

C++ doesn't have a runtime garbage collector. So things are different there. One can tell when a resource is going to be disposed (i.e. when an object's destructor will be called) unlike in C#. So there's no need for a disposable pattern here.

Collapse
 
nickpolyder profile image
Nick Polyderopoulos

Well the Dispose pattern is commonly used with unmanaged code.
The using clauses use the Dispose pattern.

See Dispose pattern docs on microsoft.docs

and wiki page

For me is the "best of both worlds". I didnt claim that this is what others think about the management of the memory in C#

Collapse
 
rhymes profile image
rhymes • Edited

The .NET dispose pattern and the using remind me of Python's context managers and the with statement.

with in Python automatically calls an __enter__ and __exit__ method which means that the writer of the class can initialize and dispose resources.

As mostly everything in Python it does not uses interfaces but duck typing, so to adhere to the "context manager pattern" you just need to implement those two methods.

In practice it is used for file handling, connection management (mostly databases) and resource disposal.