Java allocated memory leak
I must assume that the following methods do not leak memory:
public final void setData(final Integer p_iData) { data = p_iData; }
Data is an attribute of a class
Each time the method is called, a new integer replaces the existing data reference So what happened to the current / old data?
Java must do something behind the scenes; Otherwise, we must set any object to zero every time we allocate an object
Solution
Simple explanation:
The garbage collector will periodically view all objects in the system and view objects that are no longer accessible in real-time references It releases any objects that are no longer reachable
Note that your method does not create new integer objects at all For example, you can pass a reference to the same integer object over and over again
The reality of garbage collection is much more complex than this:
>Modern GC is often generational, assuming that most objects are short-lived, so it does not need to check the whole (possibly large) heap often; It can often check the activity of "recent" Objects > objects can have finalizers - the code runs before garbage collection This delays garbage collection of these objects by looping, and the object can even "revive" itself by making itself reachable > modern GC can collect in parallel and has many tuning options