It seems like people got tired of manual memory management, so they invented garbage collection, and life was reasonably good. But what about every other resource types? File descriptors, sockets, or even user created data like database connections?
This feels like a naive question but I cannot find any place where anyone has asked it. Let's consider file descriptors. Say a program knows that it will only be allowed to have 4000 fds available when it starts. Whenever it performs an operation that will open a file descriptor, what if it would
- Check to make sure that it isn't about to run out.
- If it is, trigger the garbage collector, which will free a bunch of memory.
- If some of the memory freed held references to file descriptors, close them immediately. It knows the memory belonged to a resource because the memory tied to that resource was registered into a 'file descriptor registry', for lack of a better term, when it was first opened.
- Open a new file descriptor, copy it into new memory, register that memory location into the 'file descriptor registry' and return it to the user.
So the resource would not be freed promptly, but it would be freed whenever the gc ran which includes at the very least, right before the resource was about to run out, assuming it isn't being entirely utilized.
And it seems like that would be sufficient for many user defined resource cleanup issues. I managed to find a single comment here that references doing cleanup similar to this in C++ with a thread that contains a reference to a resource and cleans it up when only it has a single reference remaining (from the cleanup thread), but I can't find any evidence of this being a library or part of any existing language.
Asked By : mindreader
Answered By : devundef
GC deals with a predictable and reserved resource. The VM has total control over it and has total control over what is created and when. The key words here are "reserved" and "total control". On the other hand, handles and are resources allocated by the OS, and pointers are... well pointers to resources allocated outside the managed space. Because of that, handles and pointers are not restricted to be used inside managed code. Handles and pointers can be used - and ofter are - by managed and unmanaged code running in the same process. A managed code can invoke native methods to pass or receive handles and pointers. A "Resource Collector" can not be sure that a handle is not being used just by checking if a managed resource holds a reference to a handle or pointer.
A practical example is the .NET CLR. One can use flavored C++ to write code which works with both managed and unmanaged memory space; handles, pointers and references, all in the same scope, the perfect storm.
Thinking about it, it should be very easy to write a Resource Collector that only acts in the last moment, when there is no more room to the program to do its work. But it's clearly a very bad design; it's not even close to be efficient, and it probably would lead to unpredictable behavior caused by resource starvation. Too many downsides...
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/52735
0 comments:
Post a Comment
Let us know your responses and feedback