|
|
|
| Hurry! Enter The Daily WTF's OMGWTF2 Contest by June 28th! - Prizes! Fame! Trophies! Do your worst: http://omg2.thedailywtf.com/ |
| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Next » |
|
Why would this have any effect on memory whatsoever? It literally does nothing.
|
Ah... now I get it. Thanks for pointing that out. |
Of course it has an effect. It's code - it takes up memory. |
It both increments *and* decrements the underlying object's reference count! You can hardly call that "nothing"! |
Re: Java Destruction
2010-04-07 11:18
•
by
Anon
(unregistered)
|
public class Destruction {
Yaay optimisation! |
|
Obviously the problem is that he forgot to call System.gc().
|
Uses CPU time, too. Definitely not "nothing"! |
|
<i>public class Destruction {
public static void delete(Object object){ object = null; } }</i> I got it. He forgot to object ? null , right? CAPTCHA: erat - to remove a difficult programmer by force. |
Re: Java Destruction
2010-04-07 11:24
•
by
Medinoc
(unregistered)
|
|
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).
|
|
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.
|
Re: Java Destruction
2010-04-07 11:26
•
by
javacoder
(unregistered)
|
|
it allow the object to be garbage collected.... it is still a WTF because a one liner should be done by the caller
|
|
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
|
|
I bet this application used to be written in C++.
Only problem is that it's missing a vector delete method! |
|
Honestly, this is WTF-worthy?
|
|
I almost want to call this article a troll to get someone to say, "Java's the real WTF," and start a holy war...
|
|
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. |
Re: Java Destruction
2010-04-07 11:30
•
by
SkittlesAreYum
(unregistered)
|
Yeah, not really. |
|
The main reason it's a memory hog is that it's in Java. Bazing.
|
But the object won't be garbage-collected unless ALL outstanding references are set to null. And this only sets the (newly-created) reference to null. Even the caller's reference will still be non-null. |
You know nulling objects to "help" the garbage collector is usually considered bad practice, right? http://www.ibm.com/developerworks/library/j-jtp01274.html |
The act before you with the anatomically-accurate balloon animals was better. In a depressing way. |
No, it doesn't. |
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. |
|
Maybe the coder was used to VB, where parameters are sent ByRef by default?
|
|
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. |
|
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! |
Re: Java Destruction
2010-04-07 11:49
•
by
Swedish tard
(unregistered)
|
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. |
|
This function has an upside: If it is consistently used, then there will be a distinct lack of heap fragmentation.
|
It's unlikely they are instantiating a new class since the method itself is static. |
Re: Java Destruction
2010-04-07 11:51
•
by
Patrick
(unregistered)
|
Simple. It makes a copy of the object passed to it (as it is passed by value), then doesn't use it. The allocated memory remains until the garbage collector kicks in, and by then more copies of other objects would have also appeared. |
Re: Java Destruction
2010-04-07 11:55
•
by
ShatteredArm
(unregistered)
|
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. |
|
You tell me over and over, we're on the eve of
Destruction. |
|
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.
|
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. |
Re: Java Destruction
2010-04-07 12:00
•
by
Anon
(unregistered)
|
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. |
I think that is the whole purpose of the code. Create more garbage so that the garbage collection kicks in sooner. |
Re: Java Destruction
2010-04-07 12:02
•
by
Buffled
(unregistered)
|
Depending on the JVM. The Blackberry docs encourage it, for example. That being said, the way it was implemented here is literally worse than useless. |
|
obviously on an embedded system......
|
|
I grow tired of this conversation. Summon the dancing girls!
|
Re: Java Destruction
2010-04-07 12:15
•
by
Anonymous
(unregistered)
|
Alright! Let's dance!
|
Re: Java Destruction
2010-04-07 12:20
•
by
Russ
(unregistered)
|
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. |
INFIDEL!!!! |
Re: Java Destruction
2010-04-07 12:35
•
by
nobody
(unregistered)
|
|
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. |
Re: Java Destruction
2010-04-07 12:36
•
by
Lunkwill
(unregistered)
|
Indeed, seems about a third of those who feel qualified to comment on Java stuff don't have a farkin clue how the language works. I hope that's not representative of people actually using it. "Hope dies last", as they say... s/dancing/Irish/ |
Re: Java Destruction
2010-04-07 12:36
•
by
gallier2
(unregistered)
|
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. |
This is why this is a valid WTF! More comments from java experts please! |
|
Let's not forget that Java doesn't GC with reference counting; it uses mark and sweep.
|
Re: Java Destruction
2010-04-07 12:40
•
by
Lunkwill
(unregistered)
|
2. yes it is, think of primitive types. it's just that object types *are* references. go to rule 1. |
Re: Java Destruction
2010-04-07 12:40
•
by
Sanity
(unregistered)
|
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. |
|
My Favourite Java Destruction till now was a program that set all instance variables null ... in the finallizer ...
"To help garbage collection" argh |
| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Next » |