• Post Reply Bookmark Topic Watch Topic
  • New Topic

"delete" in java  RSS feed

 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I wanted to know what was the reason of not providing "delete" functionality in Java? I agree that Java wanted the programmer to be free from all the memory-related dirty work, but it was possible to do both : provide "delete" as well as have an in-built GC. The typical requirement as I see could be in a real-time-application : where I DONOT want the garbage collector to run during a specific period. I would rather prefer to free the ununsed memory myself at some non-critical stage in the code. The "delete" functionality could be useful under such circumstances.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because from hard experience with languages where programmers manage memory, sometimes programmers free "unused" memory that is not actually unused. This causes hard-to-trace bugs and crashes and even potential security problems.
 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you depend on such features (e.g. highly optimized apps) you should choose another programming language like memory-unmanaged C/C++.

java did well to abandon this memory tweaking/writing thing. it really helps writing better maintainable code and avoids lots of errors.

always remember: the right tool for the right job
 
Brian Mozhdehi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul's reasoning is spot on, however I will also add that the garbage collector does not run during peak times unless it has to to free memory, i.e. in the event none is available. It runs cleanup when the system has resources available for it to do so, i.e. like a low priority thread. So, this really isnt a concern.....you dont need explicit control over this in Java to optimize performance. You're not mssing anything. C++ is old school. Java is the world of now.
 
Dana Bothner-By
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should google the "Real-Time Specification for Java". I'm writing from complete ignorance but I know that Real-Time Java is a reality. I believe James Gosling was one of the peoplw who worked on its spec. Of course, dealing with the garbage collector was a major issue, but I think the way such programming is done is that you allocate all the memory you need ahead of time, and then instruct the R-T JVM that certain threads are not to be interrupted.
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the reply.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

you can invoke GC from your code, to freeup the memory,

in following ways,

1) System.gc();

2) Runtime.getRuntime().gc();
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posted by Srinivas Kalvala
you can invoke GC from your code
Not quite. You can suggest to the JVM that it run the garbage collector, but it carries out that suggestion at its own discretion.
Somebody else was asking a very similar question today here.
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
You can suggest to the JVM that it run the garbage collector, but it carries out that suggestion at its own discretion.
Somebody else was asking a very similar question today here.


I respectfully disagree (since I was the OP of that thread).

Deterministic semantics transcends memory management. A class could represent a resource (and memory is an example of one).

'delete' continues to put the responsibility on the programmer.

Determinstic destruction IMHO allows for simpler code. If I opened a file, and (ab)used it as I please, I do not have to explicitly remember to close it -- if only the language supported deterministic destruction.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!