What are some examples of non-critical resources?

A quote from Effective Java states that:

The second legitimate use of finalizers relates to objects with native peers. native peer is a native object to which a normal object is delegated through its own methods. Since native peer is not an ordinary object, the garbage collector does not find out about this and will not be able to return it when its Java peer is restored. The finalizer is a suitable vehicle for this task , assuming that the native peer does not have critical resources .

I have not done C ++ yet, although I dimly know that file handlers and database connections are critical resources. But what exactly means that the resource is not critical?

Or rather, what are some examples of non-critical resources?

+4
source share
2 answers

I do not think that this is really the resource that is critical, despite the phrase used. I think that it restores a resource, which may or may not be critical, and the quote may be rephrased, "assuming that the resource is freed."

If it is critical that the resource is freed at a certain point during program execution, after the object is unavailable, but before the resource is needed for any other purpose, the finalizer is inadequate. Instead, you need some kind of programming logic to make sure this is happening.

So file descriptors or db connections are crucial, if you are worried that you might end up, they are not critical otherwise. If you have reached some limit of open database connections, because the finalizers that close your old ones are not running yet, and you are trying to open another database connection, it will most likely fail. The memory situation is much better, because if you run out of memory due to inaccessible objects and try to create a new object, the GC will at least try to find something that can be completed and freed.

Thus, file descriptors and db connections must have a close() function that the user can call to free all resources in cases where program logic can determine that the object will no longer be used. The expectation that the GC to close the connection through the finalizer is not reliable enough. It also does a poor job of flashing a flash or crashing, although this is a separate issue.

+1
source

Non-critical resources do not exist. The quote does not speak of non-critical resources, but simply indicates the absence of critical resources.

In a sense, you could say that memory is a non-critical resource in garbage collection. However, I am not convinced that this will be correct (in fact: managed resources can still be critical if they run out), and Ive never heard of this.

+2
source

Source: https://habr.com/ru/post/1379887/


All Articles