- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Why would this have any effect on memory whatsoever? It literally does nothing.
Admin
Ah... now I get it. Thanks for pointing that out.
Admin
Admin
Admin
Yaay optimisation!
Admin
Obviously the problem is that he forgot to call System.gc().
Admin
Uses CPU time, too. Definitely not "nothing"!
Admin
public class Destruction { public static void delete(Object object){ object = null; } }
I got it. He forgot to object ? null , right?
CAPTCHA: erat - to remove a difficult programmer by force.
Admin
And if the programmer relies on this function, it's probable that the original object reference will never be set to null (and so, the object never deleted).
Admin
You really haven't read about JIBE? That's Java Intention Based Execution. The compiler sees the intention of the programmer was to destruct the object, and will generate code to do so. Afterwards, all variables and fields pointing to this object will be null. I think it's time for y'all to upgrade and keep up with the literature.
Admin
it allow the object to be garbage collected.... it is still a WTF because a one liner should be done by the caller
Admin
Any Java compiler from 1.4 will optimize it away the first time this code is executed. So it would't consume any CPU nor additional memory
Admin
I bet this application used to be written in C++.
Only problem is that it's missing a vector delete method!
Admin
Honestly, this is WTF-worthy?
Admin
I almost want to call this article a troll to get someone to say, "Java's the real WTF," and start a holy war...
Admin
I'm guessing that he read somewhere that setting an object reference to null causes it to be reclaimed.
Luckily, I've never had to worry about garbage collection with my applications. I let the VM do that for me.
Admin
Yeah, not really.
Admin
The main reason it's a memory hog is that it's in Java. Bazing.
Admin
Admin
You know nulling objects to "help" the garbage collector is usually considered bad practice, right?
http://www.ibm.com/developerworks/library/j-jtp01274.html
Admin
Admin
No, it doesn't.
Admin
It shows a complete lack of understanding of how the language works. It's as bad a smell as there is. It'd not surprise me if this came from the kind of place that reboots their application server once or twice a day to keep the memory footprint at bay.
Admin
Maybe the coder was used to VB, where parameters are sent ByRef by default?
Admin
I'm not a Java guru (despite having used it extensively) so I'll say what I think is going wrong here.
First of all, he's obviously instantiated a class whose sole purpose is to delete other objects. If this isn't a singleton then it will be taking up a lot of memory if he's instantiating a new instance every time.
Secondly, setting it to null doesn't help. Best let the garbage collector do things on its own. I can't remember Java's semantics for parameters but I think it's a local copy of the object which is being set to null and not the original memory reference.
Admin
This one actually made me laugh. I've not seen a WTF that made me laugh in a while :-)
Seriously though, this highlights just how poorly these concepts are dealt with on CS courses. I've met many developers who have a major lack of understanding about the "black magic" of garbage collection. Arbitrary null setting really drives me nuts.
<rant> Seriously, if you have a memory problem in your application, run the damn thing through a profiler. Don't just start scatter-gunning null allocations throughout your code. </rant>Captcha: populus - Awesome game, 'nuff said!
Admin
Quite so. That code displays a rather big lack of knowledge how java handles parameters, and a blatant disregard for a bunch best practices regarding the language and OOP in general. It is rather hard to manage to get more things wrong in a single static method class in java tbh.
Admin
This function has an upside: If it is consistently used, then there will be a distinct lack of heap fragmentation.
Admin
Admin
Admin
It's a static method. No instance needs to be created to call that method.
It's beside the point. It's not about whether it's better to let the GC do things on its own. The point is that it doesn't even do anything. Even if a forced removal of the reference were the way to go, this does not achieve that. The only reference to the object that is removed is the one on the stack that is used to pass the location of the object into the function. The original reference is left unaffected. This is not Java, it's just basic computer science.
Admin
You tell me over and over, we're on the eve of
.Admin
TRWTF is that you commenters don't know how references work. No, it doesn't make a copy of the object. No, the variable reassignment doesn't bubble back to the caller. No, the garbage collector doesn't care.
Admin
One can hope, but I have seen people make a class with all methods static and then create instance of this class and call the static methods from the instance variable.
Admin
No, the method is static.
Objects aren't "set to null", references (or pointers) to those objects are set to null. I think this is the fundamental misunderstanding that caused this code to be created in the first place.
Admin
I think that is the whole purpose of the code. Create more garbage so that the garbage collection kicks in sooner.
Admin
Admin
obviously on an embedded system......
Admin
I grow tired of this conversation. Summon the dancing girls!
Admin
Admin
WRONG! Thank you for playing.
It only internally to the routine sets the object to null. The routine that calls this would still have the original and untouched object.
Admin
Admin
agreed.
you guys call yourselves coders? rule 1. stfu if you don't know and don't try to pull it out of your ass. rule 2. it's not pass by value. go to rule 1.
Admin
Admin
As always, the WTF are in the comments, Java doesn't make a copy of the passed object, the reference to the object is passed by value, the reference is nulled, and the original reference of the caller is not modified.
There was also a comment at the beginning which talked about the reference counter incremented and decremented, wrong!!! The function does nothing, it writes a 0 on the call stack, that's all.
Admin
This is why this is a valid WTF! More comments from java experts please!
Admin
Let's not forget that Java doesn't GC with reference counting; it uses mark and sweep.
Admin
Admin
Depends entirely on the situation. That page lists a perfectly legitimate case -- objects still technically reachable (part of an array, for instance) but which the program is done with.
Of course, this only works if you're actually talking directly to the array. I suppose the person who wrote this wanted
Destruction.delete(arr[i]);
But of course, that does less than nothing. The method would only really work like this:
Destruction.delete(arr, i);
...and that's specific to arrays, and also fairly useless.
Admin
My Favourite Java Destruction till now was a program that set all instance variables null ... in the finallizer ... "To help garbage collection"
argh