6

I have to assume that the following method doesn't leak memory:

public final void setData(final Integer p_iData)
{
    data = p_iData;
}

Where data is a property of some class.

Every time the method gets called, a new Integer is replacing the currently existing data reference. So what's happening with the current/old data?

Java has to be doing something under the hood; otherwise we'd have to null-out any objects every time an object is assigned.

2

7 Answers 7

7

Simplistic explanation:

Periodically the garbage collector looks at all the objects in the system, and sees which aren't reachable any more from live references. It frees any objects which are no longer reachable.

Note that your method does not create a new Integer object at all. A reference to the same Integer object could be passed in time and time again, for example.

The reality of garbage collection is a lot more complicated than this:

  • Modern GCs tend to be generational, assuming that most objects are short-lived, so it doesn't need to check the whole (possibly large) heap as often; it can just check "recent" objects for liveness frequently
  • Objects can have finalizers - code to be run before they're garbage collected. This delays garbage collection of such objects by a cycle, and the object could even "resurrect" itself by making itself reachable
  • Modern GCs can collect in parallel, and have numerous tweaking options
Sign up to request clarification or add additional context in comments.

Comments

3

Java is a garbage-collected language.

Once there are no more live references to an object, it becomes eligible for garbage collection. The collector runs from time to time and will reclaim the object's memory.

In a nutshell, your code is 100% correct and is not leaking memory.

Comments

0

It gets garbage collected eventually.

Comments

0

if there is no ther reference to data, the garbage collector of java will clean the old data up and free the memory

Comments

0

Actually, since Integer is an object not a primitive type, the line:

data = p_iData;

is updating a reference.

Now, the old object that this.data used to point to will be examined by the GC to determine if there are no more references to that object. If not, that object is destroyed and the memory is freed (at some later time)

Comments

0

If the object previously referenced by data is no longer referenced by any object structure that is referenced from any running thread it is eligible for garbage collecion. GC is performed by Java in the background to free the memory of unused objects.

Comments

0

i want to show one example to you in some code :

int x;
x=10;
x=20;

initially i assigned x to 10 again x to 20 first reference memory will be handled by Java GC. Java GC is a thread tht run continuously and checked unreferenced memory and clean it .

1 Comment

Not quite because int is a primitive type and even if you take Integer x; instead and leaving the following lines to use autoboxing to Integer nothing would be cleaned because Integer.valueOf(int i) uses cached Integer instances for all values between -128 and 127 that are never GCed. At least in the standard Java implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.