Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default 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 ?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default 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.

  3. #3
    Chivanigunt is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Re: avoid memory leak

    Hi,

    Can one help me how to integrated the cache mechanism in Ibatis with Spring.

    thanks,
    Chivanigunt.

  4. #4
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: avoid memory leak

    Quote Originally Posted by Tolls View Post
    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.
    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 03:24 PM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: avoid memory leak

    Quote Originally Posted by 2by4 View Post
    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.
    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.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default 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.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: avoid memory leak

    Quote Originally Posted by 2by4 View Post
    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.
    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.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: avoid memory leak

    Quote Originally Posted by JosAH View Post
    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.

    Jos
    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!

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: avoid memory leak

    Quote Originally Posted by 2by4 View Post
    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!
    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.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default 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?

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: avoid memory leak

    Quote Originally Posted by 2by4 View Post
    Do you mean the object g references?

    What if a member object of g is being referenced by something that persists?
    The object g references, since that is all that gc is concerned about.

    Quote Originally Posted by 2by4 View Post
    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.
    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
    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
    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.

    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 05:13 PM.

Similar Threads

  1. Memory leak in swing
    By smeshram in forum AWT / Swing
    Replies: 7
    Last Post: 12-08-2011, 04:52 AM
  2. Replies: 1
    Last Post: 07-18-2011, 05:24 PM
  3. Memory Leak questions, code review
    By mensaFool in forum Advanced Java
    Replies: 9
    Last Post: 03-08-2010, 05:27 PM
  4. Help I have a memory leak...
    By cdman52 in forum Java Applets
    Replies: 10
    Last Post: 09-28-2009, 11:37 PM
  5. Memory Leak using a Swing Application Project
    By iimasd in forum AWT / Swing
    Replies: 0
    Last Post: 11-27-2007, 11:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •