- 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
yes, yes you do. So does java. Many ways.
It doesn;t take away from the fact that a lot of the commenters don't know what they're on about. The java memorey allocation and manipulation doesn't really translate neatly on to other languages.
Admin
Because of all the commenters who don't know what they're talking about, here's a C version:
void delete(void *object) { object = NULL; }
There, happy?
Admin
I involuntarily winced when I read this! Good example of why the original post was such a WTF!
Admin
The real WTF is actually the comments by some who does not understand pass by value/ref.
Admin
I don't knew it...
But in J2ME is IMPERATIVE to do it or you'll run out of memory quickly.... So I'll have to read more before believe it (Im going to google it now)
Admin
He used a static method, so the Destruction class doesn't need to be instantiated.
Parameters are passed by value. In the case of objects this means the value of the pointer. All the delete method does is set the method local copy of the pointer to null, doing exactly nothing with the original object and marking nothing to be garbage collected.
Admin
It's an interesting optical illusion whereby if you lie on your back and look up her skirt a) she appears to change from clockwise to counterclockwise or vice-versa b) you can see if she's "commando", and c) you get kicked in the nuts and thrown out by security.
Admin
FTFY
Admin
It's obviously implemented wrong... It should be:
public class Destructor<T> { public T delete(T object){ return null; }
}
Admin
you are not alone in this universe brother
Admin
As happens a lot on this site the WTF is in the ignorant beholder. A little lesson: java is garbage collected, but sometimes people have to set a reference to null, but this is not a normal case. This method would be handy to mark those exceptional cases in the code.
“I’d like to say that this explains why the application is a completely memory hog… but sadly, this is but one of many, many, many reasons.”
TRWTF is that so called software engineers don't even know the basics of their platform. Setting a object reference to null does not cause a memory leak in Java like in C/C++.
Admin
Admin
http://msdn.microsoft.com/en-us/library/system.object.finalize.aspx
Admin
You don't get it. If the programmer designs his program thinking that he is removing references to an object this way, then objects that he thinks are ready to be garbage collected are probably still being referenced, because the quoted code has no effect.
There's a good chance the JVM will optimize calls to it away during runtime, because it has no side effects, and does no work other than assigning an unused value to a method LOCAL reference variable.
Admin
Admin
Someone else pointed that else, too. We all know by now that embedded devices have their own rules. Did you notice I said "usually". I didn't mean it in the passive-agressive-I-really-mean-always kind of way. I purposely quoted an article that shows an exception.
Admin
RTM. Object.clone() creates a new copy of the Object (not just the reference). Your way might work just fine, I don't have a Java compiler at work to test it out. I do mostly C++ and C# for work. I only use Java for school projects.
The guy asked for a swap function. He didn't say how.
Admin
Wtf? that's not safe enough. Everybody knows that's just assigning null doesn't help.
Here is proper code:
public class Destruction { public static void delete(Object object){ object = null;
}
Admin
TRWTF is that this little piece of code has created 3 pages of comments.
Admin
^.^
Admin
Oh, I meant VB, not Visual Fred or VB.NET or whatever newfangled stuff they're using now at other places. Definitely one of the poorer defaults I've seen in a programming language, but now I see it as just one of VB's quaint quirks that you get used to.
Admin
TRWTF is not calling 'finalize()'...
Admin
Under certain conditions (read "poor shitty coding") doing just that will actually lead to a memory leak. Memory leaks in Java are of a different nature, and I'm pretty sure the original poster of this WTF is actually referring to that (as opposed to blaming a C++ like memory leak to his Java specific memory problems.)
See, setting an object reference parameter to null inside a method body that
is outside the scope that created the object being referenced to by that reference parameter, and
with the expectation that such method call in the de-allocation/garbage collection of that object (which we can safely assume that is the case given the name of the static class and method
will most certainly cause a memory leak by virtue of not garbage collecting that object in a timely fashion (which is critical in high volume systems) or never at all if it happens to have one reference inside a data structure with a long lifespan (.ie. as an attribute in a http servlet session or application context.)
Ever seen some people rolling their own poorly written caching layer on JEE apps that end up failing to clear their expired entries? Chances are they do something like what is described in this articles WTF.
Admin
1 - clone is protected. 2 - after the function call a[0] != b[0] 3 - depending on the implementation of the object a[0].equals(b[0]) might be true if the two objects are the same type.
So much for the swap function in java, huh
Admin
Admin
The current version of the language is called "VB", see http://msdn.microsoft.com/en-us/vbasic/default.aspx. So be more specific next time.
Admin
The Destruction-Delete Pattern works in most languages.
For example, Here's DDP in Python:
DDP ensures that my basket of eggs remains untouched.
Admin
Clearly he wanted:
import org.apache.commons.lang.mutable.MutableObject;
public static void delete(MutableObject object) { object.setValue(null); }
with a call like:
Destruction.delete(new MutableObject(thingToDelete));
:D
Admin
Whoops, forgot mandatory logging and exception handling, as all enterprise applications require. And what if two threads tried to delete it at once?
They should hire me. I'd soon have their shoddy code whipped into shape!
Admin
Well played. This is the funniest code I've ever seen.
Admin
Did you quote the wrong post? I said nothing about age of objects. And the age of an object should not really matter. Except that it might influence the garbage collection policy for that object.
Admin
I was just wondering why the clone calls were there. There should be no reason to clone the objects, since we're basically just switching a couple of pointers.
Admin
Admin
"newer" as written by someone with German as their primary language would be pronounced as "never" in English.
"Volkswagen" spoken -> "Folks vagen" means -> Folk wagon same as -> people cart.
I used to work with a guy who must have learned English in a font that could not distinguish lower case g from lower case q. It made for lots of interesting variable names, but if you knew the algorithm you could read it.
P.S. I love using open source from all over the world.
Admin
Actually it does. Java passes function arguements by value. What this means is that when you pass Object object to the funtion, it actually creates a copy of that object, which takes up more memory. After that you set it to null, but being java it will not immediatly deallocate the memory, it will wait for the garbage collector to do this.
Not only are you increasing memory usage (at least until the GC cleans up), you are also stressing your cpu more, because your garbage collector has way more garbage to collect. Besides the obvious fact that calling a function and copying objects all take up CPU time as well.
Admin
Actually, I just realised that I might have made a mistake here. I believe that what java does is actually pass the reference to the object by value. So that would mean that the only additional memory allocated is that of the reference.
Admin
Please be quiet now, you've just contributing ill-informed noise at this point.
Admin
With the exception that the remove method you present contains not a memory leak, rather a logic error. One that would likely be caught given get calls subsequent the supposed remove shall continue to find the key, presumably in error.
Admin
Post your complete Java code for a swap function (non array) so we can have another WTF!
Admin
I was going to call you an idiot. But then I looked it up. http://javadude.com/articles/passbyvalue.htm
So... I'm stfu.
Admin
TRWTF is that I can't work out who is trolling and who isn't.
Admin
Your right, the call to clone is completely uneccessary and possibly not compilable. That what happens when you don't actually try the code you post online. I should have just said you can use arrays as a pass-by-reference hack.
Admin
Object references are passed by value. You can set the reference to null all you want, its not going to affect the callers reference.
Admin
I think this must've been written by one of my ex-colleagues; jeez!!!
Admin
Not much of a hack, considering that the encapsulated variables -- themselves -- still won't be swapped, simply the containers holding them have been switched.
Admin
Yes.
It's not really "allocated" in the same sense that objects are allocated. It's just shoved on the stack or into a register.
Admin
Wierdest reference leak ever.
Admin
Hi! Is it weird that I am 23 and still a virgin? I would like to invite you to join my circle of friends on _ Black White Cupid c/o-m **—- My username is “looking4sexxx” Give me your comments on my photos. I’m waiting 4 u.
Admin
Admin