Comment On Java Destruction

“It can be difficult to develop complicated, J2EE software without a fundamental understanding of how Java works,” Vladimir writes, “difficult… but not impossible.” [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Re: Java Destruction

2010-04-07 11:10 • by OutlawProgrammer (unregistered)
Why would this have any effect on memory whatsoever? It literally does nothing.

Re: Java Destruction

2010-04-07 11:12 • by Maurits
304854 in reply to 304853
OutlawProgrammer:
Why would this have any effect on memory whatsoever? It literally does nothing.


Ah... now I get it. Thanks for pointing that out.

Re: Java Destruction

2010-04-07 11:16 • by tim (unregistered)
304855 in reply to 304853
OutlawProgrammer:
Why would this have any effect on memory whatsoever? It literally does nothing.

Of course it has an effect. It's code - it takes up memory.

Re: Java Destruction

2010-04-07 11:16 • by DaveK
304856 in reply to 304853
OutlawProgrammer:
Why would this have any effect on memory whatsoever? It literally does nothing.
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)
304857 in reply to 304855
public class Destruction {

public static void delete(Object object){}
}


Yaay optimisation!

Re: Java Destruction

2010-04-07 11:18 • by frits
Obviously the problem is that he forgot to call System.gc().

Re: Java Destruction

2010-04-07 11:20 • by Coyne
304859 in reply to 304856
DaveK:
OutlawProgrammer:
Why would this have any effect on memory whatsoever? It literally does nothing.
It both increments *and* decrements the underlying object's reference count! You can hardly call that "nothing"!


Uses CPU time, too. Definitely not "nothing"!

Re: Java Destruction

2010-04-07 11:24 • by Bryan The K (unregistered)
<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)
304861 in reply to 304859
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).

Re: Java Destruction

2010-04-07 11:25 • by TGV
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)
304863 in reply to 304856
it allow the object to be garbage collected.... it is still a WTF because a one liner should be done by the caller

Re: Java Destruction

2010-04-07 11:27 • by wil (unregistered)
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

Re: Java Destruction

2010-04-07 11:28 • by Mike Caron (unregistered)
I bet this application used to be written in C++.

Only problem is that it's missing a vector delete method!

Re: Java Destruction

2010-04-07 11:29 • by gm (unregistered)
Honestly, this is WTF-worthy?

Re: Java Destruction

2010-04-07 11:29 • by Anonymously Yours (unregistered)
I almost want to call this article a troll to get someone to say, "Java's the real WTF," and start a holy war...

Re: Java Destruction

2010-04-07 11:29 • by Randy (unregistered)
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)
304869 in reply to 304863
javacoder:
it allow the object to be garbage collected.... it is still a WTF because a one liner should be done by the caller


Yeah, not really.

Re: Java Destruction

2010-04-07 11:30 • by epv (unregistered)
The main reason it's a memory hog is that it's in Java. Bazing.

Re: Java Destruction

2010-04-07 11:31 • by JamesQMurphy
304872 in reply to 304863
javacoder:
it allow the object to be garbage collected.... it is still a WTF because a one liner should be done by the caller

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.

Re: Java Destruction

2010-04-07 11:34 • by frits
304873 in reply to 304863
javacoder:
it allow the object to be garbage collected.... it is still a WTF because a one liner should be done by the caller


You know nulling objects to "help" the garbage collector is usually considered bad practice, right?

http://www.ibm.com/developerworks/library/j-jtp01274.html

Re: Java Destruction

2010-04-07 11:37 • by dkf
304874 in reply to 304870
epv:
The main reason it's a memory hog is that it's in Java. Bazing.
The act before you with the anatomically-accurate balloon animals was better. In a depressing way.

Re: Java Destruction

2010-04-07 11:38 • by friedo
304875 in reply to 304863
javacoder:
it allow the object to be garbage collected....


No, it doesn't.

Re: Java Destruction

2010-04-07 11:44 • by Goo (unregistered)
304877 in reply to 304866
gm:
Honestly, this is WTF-worthy?


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.

Re: Java Destruction

2010-04-07 11:45 • by pcooper (unregistered)
Maybe the coder was used to VB, where parameters are sent ByRef by default?

Re: Java Destruction

2010-04-07 11:45 • by Steve W (unregistered)
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.

Re: Java Destruction

2010-04-07 11:45 • by Docta Jonez (unregistered)
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)
304881 in reply to 304866
gm:
Honestly, this is WTF-worthy?


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.

Re: Java Destruction

2010-04-07 11:49 • by Mike D. (unregistered)
This function has an upside: If it is consistently used, then there will be a distinct lack of heap fragmentation.

Re: Java Destruction

2010-04-07 11:50 • by Jim (unregistered)
304883 in reply to 304879
Steve W:
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.

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)
304884 in reply to 304853
OutlawProgrammer:
Why would this have any effect on memory whatsoever? It literally does nothing.

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)
304887 in reply to 304879
Steve W:
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.


It's a static method. No instance needs to be created to call that method.


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.


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.

Re: Java Destruction

2010-04-07 11:59 • by Raedwald
You tell me over and over, we're on the eve of
Destruction
.

Re: Java Destruction

2010-04-07 11:59 • by Xyro
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.

Re: Java Destruction

2010-04-07 11:59 • by tiller
304890 in reply to 304883
Jim:
Steve W:
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.

It's unlikely they are instantiating a new class since the method itself is static.


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)
304891 in reply to 304879
Steve W:
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.


No, the method is static.


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.


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.

Re: Java Destruction

2010-04-07 12:01 • by avflinsch
304892 in reply to 304884
Patrick:
OutlawProgrammer:
Why would this have any effect on memory whatsoever? It literally does nothing.

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.


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)
304893 in reply to 304873
frits:
javacoder:
it allow the object to be garbage collected.... it is still a WTF because a one liner should be done by the caller


You know nulling objects to "help" the garbage collector is usually considered bad practice, right?

http://www.ibm.com/developerworks/library/j-jtp01274.html

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.

Re: Java Destruction

2010-04-07 12:03 • by rwbthatisme (unregistered)
obviously on an embedded system......

Re: Java Destruction

2010-04-07 12:04 • by anonymous (unregistered)
I grow tired of this conversation. Summon the dancing girls!

Re: Java Destruction

2010-04-07 12:15 • by Anonymous (unregistered)
304896 in reply to 304895
anonymous:
I grow tired of this conversation. Summon the dancing girls!

Alright! Let's dance!

Re: Java Destruction

2010-04-07 12:20 • by Russ (unregistered)
304898 in reply to 304863
javacoder:
it allow the object to be garbage collected.... it is still a WTF because a one liner should be done by the caller


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.

Re: Java Destruction

2010-04-07 12:28 • by OBL (unregistered)
304900 in reply to 304867
Anonymously Yours:
I almost want to call this article a troll to get someone to say, "Java's the real WTF," and start a holy war...
INFIDEL!!!!

Re: Java Destruction

2010-04-07 12:35 • by nobody (unregistered)
304901 in reply to 304889
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)
304902 in reply to 304889
Xyro:
TRWTF is that you commenters don't know how references work.
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...

anonymous:
I grow tired of this conversation. Summon the dancing girls!
s/dancing/Irish/

Re: Java Destruction

2010-04-07 12:36 • by gallier2 (unregistered)
304903 in reply to 304884
Patrick:
OutlawProgrammer:
Why would this have any effect on memory whatsoever? It literally does nothing.

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.


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.

Re: Java Destruction

2010-04-07 12:37 • by Bob (unregistered)
304904 in reply to 304863
javacoder:
it allow the object to be garbage collected.... it is still a WTF because a one liner should be done by the caller


This is why this is a valid WTF! More comments from java experts please!

Re: Java Destruction

2010-04-07 12:37 • by Dan (unregistered)
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)
304906 in reply to 304901
nobody:
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.

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)
304907 in reply to 304873
frits:
javacoder:
it allow the object to be garbage collected.... it is still a WTF because a one liner should be done by the caller


You know nulling objects to "help" the garbage collector is usually considered bad practice, right?

http://www.ibm.com/developerworks/library/j-jtp01274.html


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.

Re: Java Destruction

2010-04-07 12:58 • by Quicksilver
My Favourite Java Destruction till now was a program that set all instance variables null ... in the finallizer ...
"To help garbage collection"

argh
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Add Comment