0

Most of the times students gets console based management system assignments i.e. Library or Point of Sale System etc. They have to store their data using traditional file system.

And 80-90 percent times they make mistake of forgetting to close their file after opening or creating it.

someFile.close();

(They have to debug all the code to find any error, as to why their data is not being stored in a file. Being a mentor I have faced this problem myself many times too.)

So the real thing is, the way these advanced languages especially Java enforces Exception Handling why there isn't anything to enforce file closure?

6
  • 1
    have a look at stackoverflow.com/questions/28813637/…
    – Astra Bear
    Mar 20, 2015 at 22:22
  • 6
    To play devil's advocate: please imagine the preferable way you would like them to enforce closing. Mar 20, 2015 at 22:25
  • That is why I don't like managed languages: they separate memory into "special" kind of resource that is automatically managed (more or less successfully), but then give programmer a set of crutches to manage everything else. C++ FTW!
    – n0rd
    Mar 20, 2015 at 22:28
  • @n0rd All resources are handled the same in all these languages, if you think about it. Memory has the special characteristic that we often tolerate a non-deterministic release, while for other resources we usually have to be more strict. The difference is in our real-world requirements for those resources - the languages simply reflect that, by giving us an additional choice for the latter. Mar 20, 2015 at 22:39
  • Generally, memory can be non-deterministically released if and only if the hardware, your program runs on have orders of magnitude more memory than your application consumes. Once your memory consumption start to get close to memory limits you'd "suddenly" want to manually manage it.
    – n0rd
    Mar 20, 2015 at 22:46

3 Answers 3

7

Resources are released whenever they are deallocated (by being garbage collected, which is not expected to be deterministic). If you want to dispose them in a deterministic manner (and not at the moment of their deallocation), in Java there is the try-with-resources block and in C# there is the using block.

Why don't they enforce this to be the case for all types that encapsulate autoclosable/disposable resources? Well, simply because this pattern is good only in some use cases. If they enforced it, it would be troublesome in all the other cases.

2
  • 3
    Its always better to assume the programmer knows what they are doing. +1. Mar 20, 2015 at 22:27
  • I am beginner in C# and didn't knew about #using block. It is pretty much helping in traditional file handling. @Theodoros Mar 22, 2015 at 10:39
1

You kind of answered your own question. Programmers did traditionally forget to close open files. The developers of Java and C# sat down and tried to come up with a way to prevent traditional programming errors. In doing this, they came up with the idea of automatic garbage collection.

The idea behind Java and C# is "Why rely on the programmer to delete objects and close files when the runtime environment can be programmed to do that automatically?" Thus was born automatic garbage collection.

Speaking from a C# stand point, when the runtime senses that an object has gone out of scope (e.g., not needed anymore) or a stream needs to be disposed of, it is put in a garbage collection queue. After a while, the object is disposed of, RAM is freed and any files that need closing are closed.

1
  • Actually, in Java, things like that can only be done in finalizers. Finalization is not done very quickly and is relegated to a finalizing thread, and this means you can run out of resources before old, unreferenced ones are freed. So it's bad practice to leave this to the JVM. Garbage collection is the solution to memory management, not resource management. Mar 20, 2015 at 22:32
0

Another aspect is this: in Java unused variables etc are cleaned up by the Garbage Collector (GC)- but only if you wait long enough.

With memory used by variables one piece of memory is as good as another so if there is enough memory available the GC doesn't need to be run. With stream resources this is not the case. If another part of the program (or another program) needs that resource it needs that resource and cannot just use another resources interchangeably, so it should be manually closed asap.

In a nutshell: memory is interchangeable - resources are not.

Not the answer you're looking for? Browse other questions tagged or ask your own question.