Results 1 to 11 of 11
Thread: avoid memory leak
- 12-08-2011, 10:02 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
avoid memory leak
I've used struts, spring, ibatis in applications .
I've used bean parameter to get data from model in action class .
Should I use bean=null in action to avoid the memory leak ?
Goods g = new Goods() ;
g.setEmployee(lid) ;
List<Goods> goods = goodsModel.getGoods(g) ;
request.setAttribute( ... ) ;
g == null ?
- 12-08-2011, 01:14 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,447
- Rep Power
- 16
Re: avoid memory leak
No.
That is almost always a sign of someone who doesn't understand how garbage collection works.
Since, presumably, you exit whatever scope you are in soon after that request.setAttribute call then g will be eliginle for garbage collection anyway.
- 12-20-2011, 02:14 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
Re: avoid memory leak
Hi,
Can one help me how to integrated the cache mechanism in Ibatis with Spring.
thanks,
Chivanigunt.
- 12-20-2011, 02:21 PM #4
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: avoid memory leak
Do you mean the object g references?
What if a member object of g is being referenced by something that persists?
As I understand it, g is just a reference on the stack. Not something for the garbage collector to worry about. g passes out of scope when the stack unwinds. The object that g points at seems to persist in the list.Last edited by 2by4; 12-20-2011 at 02:24 PM.
- 12-20-2011, 02:33 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Re: avoid memory leak
If g was the only reference to the object, it will be garbage collected when the memory, taken by the object, is needed again; if there are still other references to the object after g went out of scope the object won't be garbage collected; weak references don't count; don't try to obfuscate yet another thread.
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-20-2011, 02:41 PM #6
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: avoid memory leak
The point I make is that g is on the stack and goes out of scope. setting it to null is unnecessary. I agree with Tolls, there.
As for when the object, created within the method call, is ready for garbage collection, I think it is a separate and complex question. This is the advanced forum, you know. I can put the question out there.
- 12-20-2011, 02:51 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Re: avoid memory leak
I know that: no reference to an object: it can be garbage collected somewhere in the future; if there are still references to the object, the object can't be gargage collected unless those references can't be reached themselves; and I know that this is the advanced forum; I moderate it and now and then I have to keep an eye on trouble makers, problem seekers and other vague types that claim to be Mr-Know-It-All.
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-20-2011, 03:02 PM #8
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: avoid memory leak
I said you were thinking low level on another thread, and since then you've sulked. That is your problem. You need to get over it and understand "low level" in its proper context.
If you read my post above, all I have done is ASK a question and state MY UNDERSTANDING.
Now please stop this childishness.
bye bye!
- 12-20-2011, 03:21 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Re: avoid memory leak
I don't know what 'sulked' means, I guess it means 'insulted' (English is not my native language) I can only be insulted by someone I appreciate and have high esteem for; you're not one of them; 'low level' has only one context in my understanding and you don't even have your low level facts straightened out.
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-20-2011, 03:34 PM #10
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: avoid memory leak
Moving on..
Tolls answered it. g goes out of scope. Setting it to null gains you nothing.
I would like to use this opportunity to further examine the OP.
Now, if you are talking about a bean in something like a Spring Container, would references persist once you exit the method?
- 12-20-2011, 04:10 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,447
- Rep Power
- 16
Re: avoid memory leak
The object g references, since that is all that gc is concerned about.
The object g is referencing is on the heap.
All objectes are on the heap.
The references to them are on the stack.
Hence the object g references becomes eligible for gc once it goes out of scope.
eg
Which is why setting it to null is pointless and usually a sign of someone who doesn't quite know how this thing works, or has written something in which objects have too large a scope.Java Code:void someMethod() { Object o = new Object(); // An object is created on the heap // blah blah blah ... } // That object is now eligible for garbage collection so long as nothing in "blah blah blah" has resulted in it // being referred to elsewhere
ETA: Just to clarify, in the given code it will make even less sense since, as you say, g is in a List that (presumably) is stored in the request, so there are still references to it.
My main point being that setting stuff to null as in the OP is a waste of time.Last edited by Tolls; 12-20-2011 at 04:13 PM.
Similar Threads
-
Memory leak in swing
By smeshram in forum AWT / SwingReplies: 7Last Post: 12-08-2011, 03:52 AM -
what are the memory profiling tools available that can help me detect memory leak?
By guest_user in forum New To JavaReplies: 1Last Post: 07-18-2011, 04:24 PM -
Memory Leak questions, code review
By mensaFool in forum Advanced JavaReplies: 9Last Post: 03-08-2010, 04:27 PM -
Help I have a memory leak...
By cdman52 in forum Java AppletsReplies: 10Last Post: 09-28-2009, 10:37 PM -
Memory Leak using a Swing Application Project
By iimasd in forum AWT / SwingReplies: 0Last Post: 11-27-2007, 10:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks