Why doesn't Java have a destructor like C ++?

Java has its own garbage collection, so it does not require a destructor such as C ++. This makes the Java developer lazy in implementing memory management.

However, we can have a destructor together with the garbage collector, where the developer can free up resources and which can save the garbage collector. This can improve application performance. Why doesn't Java provide any kind of destructor mechanism?

The developer does not have control over the GC, but he / she can manage or create the object . Then why not give them the opportunity to destroy objects?

+7
java destructor
source share
10 answers

You claim that "garbage collection is very expensive" - ​​could you confirm this with evidence? Garbage collection, of course, is not free, but modern garbage collectors are very good.

Note that one of the ways an effective GC is is that it knows that this is the only thing that does memory allocation and deallocation (for managed objects). Allowing the developer to explicitly release the object can impede this efficiency. You will also need to worry about what happens if the developer tries to “use” the freed object:

Foo f = new Foo(); Foo g = f; free(f); // Or whatever System.out.println(g.toString()); // What should this do? 

Do you suggest that each object has an additional flag for "it is explicitly released", which must be checked with every dereferencing? Honestly, this sounds like a recipe for disaster.

You are correct, although this allows Java developers to be lazy in this area. It's good. IDEs also allow developers to be lazy - like high-level languages, etc. Laziness in memory allocation allows developers in managed environments to spend their energy worrying about business issues rather than managing memory.

+54
source share

Garbage collection is very expensive.

In fact, for complex applications, garbage collection performance is competitive with manual malloc / free storage management. There is a classic article by Benjamin Zorn that clearly demonstrates this. In this article, Zorn describes how he modified some large large-load applications to use a conservative garbage collector instead of malloc and free . He then compared the original and modified versions of the applications. The result was comparable performance.

This document was published in "Practice and Experience in Using Software" in 1993. If you have not read it, you do not have the right to make statements about the "inefficiency" of garbage collection.

Please note that this study was conducted with a 1993 conservative garbage collector. Conservative collector - marking without any seal; that is, objects without garbage do not move. The latter means that allocating space for new objects is as slow and complicated as malloc . In contrast, modern garbage collectors (like Java 6/7) are collectible copy collectors that are much more efficient. And since copying compresses the remaining objects without garbage, distribution is much faster. This makes the GC even more competitive ... if you can find a way to make a comparison.


The developer does not have control over the GC, but he / she can manage or create the object. Then why not give them the opportunity to destroy objects?

It depends on what exactly you mean by "destruct".

  • In Java, you have the option to assign null . In some cases, this can accelerate the destruction of the object.

  • In Java, you can use finalizers and Reference types to notice that the object is about to be destroyed ... and something like that.

  • In Java, you can define a close() method (or equivalent) for any object and do something suitable. Then call it explicitly.

  • In Java 7, you have a “try with resources” construct to automatically call close() for resources when you exit the scope.

However, you cannot force a Java object to be deleted NOW. The reason this is not allowed is because it will allow the program to create dangling links, which can lead to heap failure and accidental JVM crashes.

This is NOT a Java way. The philosophy is that writing reliable programs is more important than efficiency. Although some aspects of Java do not follow this (e.g. streaming), no one wants the possibility of accidental JVM crashes.

+21
source share

The C ++ destructor is not a way to destroy objects - it is a set of operations that must be performed when the object is destroyed. In Java, you do not control the time when objects are destroyed (they are never even destroyed), so the inclusion of any important code that will be executed when the object is destroyed is strongly discouraged (although it is possible to finalize ). If you ask for not a destructor, but a way to explicitly destroy this object, you invite dangling links to your code. They are not welcome.

+7
source share

This makes the Java developer lazy in implementing memory management.

No, he frees them to do useful work.

And garbage collection is very expensive.

Compared to what? Facts? The numbers? You are already 20 years old with this remark. Adopting Java only effectively refutes this claim.

This can improve application performance.

Or not. Did you have any facts to cite?

Then why not give them the opportunity to destroy objects?

Because it is not required?

+5
source share

Destructors are called when an object is destroyed in C ++, not to destroy the object. If you want to guarantee cleanup, ask the user to call the Destroy method or the like.

+4
source share

If you know that you do not have large objects, just set them to zero. Perhaps this will speed up garbage collection of these objects.

+1
source share

The C ++ destructor is not a way to destroy objects - it is a set of operations that must be performed when the object is destroyed.

I think you are misleading the terminology. Here is how I see it:

create an object = first allocate memory, and then build through the constructor

destroy object = first destroy through destructor, then free memory

How memory is allocated and freed. If you use new and delete , memory management is done using void* operator new(size_t) and void operator delete(void*) .

+1
source share

The C ++ destructor is useful for freeing up any resources belonging to an object, not just memory. It can be files, sockets, mutexes, semaphores or any other resource descriptors. Using a destructor is a smart way to prevent resource leaks. Wrap resource handling in a C ++ class and create a destructor that frees up any allocated resources. I do not see such a method in Java. You must explicitly free the resource, and it can be difficult if there are many possible ways out.

0
source share

No, java does not support destructors. All memory task deallocation is performed by GARBAGE COLLECTOR.

Java has its own memory management function using the garbage collector. When you use finalize (), the object becomes available for garbage collection, and you do not need to explicitly access the destructor. C # and Java do not want you to worry about the destructor as they have a garbage collection function.

Java is a bytecode language, it has very strong garbage detection. If you would allow people to define their own destructors, it is likely that they might make some mistakes. By automating the process, Java intends to prevent these errors.

0
source share

You have the ability to control the destruction of objects in Java. It just uses a different idiom:

 Connection conn = null; try { conn = ... // do stuff } finally { try { conn.close(); } catch (Exception e) { } } 

At this point, you could indicate that this is not destroying the object, and you could, for example, pass this object to something else and still have a link to it. You are right on both points. It is just as close as Java (and most managed platforms).

But no Java, as you say, has a destructor mechanism, as in C ++. Some people are mistaken for this. They are not destructors, and it is dangerous to use them as such.

Memory management is difficult for the programmer. You can easily leak memory, especially with multi-threaded programming (also difficult). Experience shows that the cost of GC, while real, and sometimes substantial, is well and truly justified by the increase in productivity and errors in the vast majority of cases, so the vast majority of platforms are now “managed” (which means that they use garbage collection )

-2
source share

All Articles