Results 1 to 3 of 3
  1. #1
    sagngh8 is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default Assigning null to Complex Type object in the context of memory in java

    What is the meaning of assigning null to Complex Type object in the context of memory in java language.
    Eg

    while(true) //Approx 9000 times
    {

    Employee objEmployee = new Employee ();

    objEmployee = //Put some value

    objEmployee = null;
    }

    Does assigning null to the complex type make the object ready for garbage collection in java.Also
    is it the way to efficient memory management in java.

    Regards,
    Sagar.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    If that is the only reference to the object, then yes, it makes it GC "ready". Is it any better than simply letting the Object go out of scope? In the above example, no, in the case of a long-lived instance variable or something, maybe, as long as you are not simply immediately reassigning a new value to the instance variable (in which case you can skip the null assignment, of course) and are not doing it shortly before the "end" of the instance (in which case you can simply let it go out of scope, again).

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    What masijade says, but I'll add that this is (outside of some very specific cases) usually a sign you've written something incorrectly.
    You should (outside of etc etc) never need to do this, and I use it (in code reviews) as a flag that the author has gone wrong somewhere.

Similar Threads

  1. Replies: 8
    Last Post: 03-04-2011, 01:50 PM
  2. FAIL - Invalid context path null was specified
    By humanbeing in forum Java Servlet
    Replies: 0
    Last Post: 08-13-2010, 11:14 AM
  3. Assigning values to an object
    By camper2 in forum New To Java
    Replies: 4
    Last Post: 04-05-2009, 03:13 AM
  4. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 06:24 PM
  5. Replies: 1
    Last Post: 08-06-2007, 11:10 PM

Posting Permissions

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