3 ms·
Examples of a situation where you don't want a resource associated with a variable/storage location? Global variables exist and can be used to hold resources.
by rian 13y ago
Examples of a situation where you don't want a resource associated with a variable/storage location?
Global variables exist and can be used to hold resources. If you have a resource that's not associated with any single function context you can move it into the global variable.
- danbruc 13y agoSee mikeash's comment for a very simple situation. I mostly develop business applications with customers, products, orders, contracts, addresses and hundreds of other things connected and entangled in sometimes insane ways as dictated by the business. And at any point the user may just open another form viewing the orders of a different customer linked to the same products as in five other forms. Managing such complex object graphs by hand would be a nightmare. And this is just the model of the business, we did not even start to think about database connections, client connections, UI components and what not.
- rian 13y agoYes I think you would use a ref counted resource in that case. For your tangled mess of database connections I can't imagine ref counting not being adequate in those scenarios as well.
- danbruc 13y agoAnd now we are back at garbage collection - the two main options are reference counting GCs and collecting GCs. I know it for the .NET GC, they tried a reference counting GC and it performed worse than a collecting GC and not to forget the trouble with cycles when implementing a reference counting GC. And reference counting also renders the point of determinism moot - now you never know if releasing a reference while trigger freeing a resource. You could of course delay offload it to a separate thread, but this makes you lose any guarantee about the time when the resource is actually freed, too.
- rian 13y agoAh, I don't think ref counting is generally considered GC. It's more like RAII and used in C++ elsewhere via shared_ptr.
- danbruc 13y agoIt is a mechanism to figure out if a resource is no longer in use and can be released - that sounds like the very definition of garbage collection.
- letzjuc 13y ago> And reference counting also renders the point of determinism moot - now you never know if releasing a reference while trigger freeing a resource. You know that releasing the last reference will. That is why the important concept is single ownership of resources and not reference counting by itself.
- danbruc 13y agoBut you don't know which reference is the last until you actually get there - that is why you used reference counting in the first place, because you were unable to statically determine which reference will be the last one. So every time you abandon a reference it might be the last one and you might have to spend extra cycles to free the resource. This is no more deterministic than a garbage collector.
- letzjuc 13y agoIn a non-deterministic garbage collector memory _might not_ be released when the last reference goes out of scope (and this is often the case).
- gnaritas 13y ago> Global variables exist And are generally considered bad practice.