clearing or set null to objects in java - java

I was recently looking into freeing up memory occupied by Java objects. While doing that I got confused about how objects are copied (shallow/deep) in Java and how to avoid accidently clearing/nullifying objects while they are still in use.
Consider following scenarios:
passing a ArrayList<Object> as an argument to a method.
passing a ArrayList<Object> to a runnable class to be processed by a thread.
putting a ArrayList<Object> into a HashMap.
Now in these case, if I call list = null; or list.clear();, what happens to the objects? In which case the objects are lost and in which cases only the reference is set to null?
I guess it has to do with shallow and deep copying of objects, but in which cases does shallow copying happens and in which case does deep copy happens in Java?

Firstly, you never set an object to null. That concept has no meaning. You can assign a value of null to a variable, but you need to distinguish between the concepts of "variable" and "object" very carefully. Once you do, your question will sort of answer itself :)
Now in terms of "shallow copy" vs "deep copy" - it's probably worth avoiding the term "shallow copy" here, as usually a shallow copy involves creating a new object, but just copying the fields of an existing object directly. A deep copy would take a copy of the objects referred to by those fields as well (for reference type fields). A simple assignment like this:
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = list1;
... doesn't do either a shallow copy or a deep copy in that sense. It just copies the reference. After the code above, list1 and list2 are independent variables - they just happen to have the same values (references) at the moment. We could change the value of one of them, and it wouldn't affect the other:
list1 = null;
System.out.println(list2.size()); // Just prints 0
Now if instead of changing the variables, we make a change to the object that the variables' values refer to, that change will be visible via the other variable too:
list2.add("Foo");
System.out.println(list1.get(0)); // Prints Foo
So back to your original question - you never store actual objects in a map, list, array etc. You only ever store references. An object can only be garbage collected when there are no ways of "live" code reaching that object any more. So in this case:
List<String> list = new ArrayList<String>();
Map<String, List<String>> map = new HashMap<String, List<String>>();
map.put("Foo", list);
list = null;
... the ArrayList object still can't be garbage collected, because the Map has an entry which refers to it.

To clear the variable
According to my knowledge,
If you are going to reuse the variable, then use
Object.clear();
If you are not going to reuse, then define
Object=null;
Note:
Compare to removeAll(), clear() is faster.
Please correct me, If I am wrong....

It depends on how many variables are referenciating to each of your objects, to explain this it would be better some code:
Object myAwesomeObject = new Object();
List<Object> myList = new ArrayList<Object>();
myList.add(myAwesomeObject);
myList = null; // Your object hasn't been claimed by the GC just yet, your variable "myAwesomeObject" is still refering to it
myAwesomeObject = null; // done, now your object is eligible for garbage collection.
So it doesn't depend whether you pass your ArrayList as an argument to a method or the like, it depends on how many variables are still refering to your objects.

If you passed an ArrayList to a method then list = null will have no effect if there is a live reference to the list somewhere eg in the calling code. If you call list.clear() anywhere in the code the references to the objects from this list will be nulled. Passing a reference to a method is not shallow copying it is passing reference by-value

Java GC automatically claims the objects when they are not referenced anywhere. So in most cases you will have to set the reference as null explicitly
As soon as the scope of the variable ends the object becomes eligible for GC and gets freed up if no other reference points to the object.
Java is pass by value so if you set the list as null in the method then it will not affect the original reference that was passed to you in the method.
public class A{
private List<Integer> list = new ArrayList<Integer>();
public static void main(String[] args) {
A a = new A();
B b = new B();
b.method(a.list);
System.out.println(a.list.size()); //Will print 0 and not throw NullPointerException
}
}
class B{
public void method(List<Integer> list){
list = null;
//just this reference is set to null and not the original one
//so list of A will not be GCed
}
}

If you put the list into a hash map, the hash map now holds a reference to the list.
If you pass the list as an argument to a method, the method will have a reference to it for the duration of the method.
If you pass it to a thread to be manipulated, the thread will have a reference to the object until it terminates.
In all of these cases, if you set list = null, the references will still be maintained, but they will disappear after these references disappear.
If you simply clear the list, the references will still be valid, but will now point to a list that has suddenly been emptied, by means that may be unknown to the programmer and may be considered a bug, especially if you use the thread.

I was recently looking into freeing up memory occupied by java objects.
A piece of advice.
It is usually a bad idea to think about this. And it is usually a worse idea to try to "help". In 99.8% of cases, the Java garbage collector is able to do a better job of collecting the garbage if you actually just let it get on with it ... and don't waste your effort by assigning null to things. Indeed, the chances are that the fields you are nulling are in objects that are about to become unreachable anyway. And in that case, the GC is not even going to look at the fields that you've nulled.
If you take this (pragmatic) view, all your thinking about shallow versus deep copies and when it is safe to null things is moot.
There is a tiny percentage of cases where it is advisable to assign null ... to avoid medium or long term storage leaks. And if you are in one of those rare situations where it is "recycling" objects is actually a good idea, then nulling is also advisable.

Related

Is modyfying the referenced object affecting the original object from which the new object was created?

I have a problem with understanding one thing.
I have:
List<Map> resultList = new ArrayList<Map>();
Then this resultList is filled with some data
resultList.addAll(somemethod(something, something, else));
Later in the method I have this kind of code:
Map timeSpan = someMethod(resultList, date);
timeSpan.put(KEY_ART, VALUE_ART);
timeSpan.put(KEY_TIMESPAN, true);
So I have a question now. If Map timeSpan is a map referenced to an element of List<Map> resultList, is using the put() method on the timeSpan map affecting the element in resultList?
I am asking this question cause a collegue told me that this is working this way - modifying an element in timeSpan is also modyfying this element in resultList. She is far more experienced, and I don't just want to believe her but I want to understand why it is working this way.
You have to remember that resultList is a reference to an object. You can copy this reference around and use it in many way, in many places but there is only one object. This means when you alter the object, there is only one view of this object.
Java objects are always instantiated as a reference to a memory space. If you create a second object from the first object, both will point to the same memory space:
Map a = new HashMap();
Map B=b = a;
Here, we first create an instance A which points to a HashMap which is created somewhere in memory. Next, we create an instance of Map b and have it reference to the same memory space as Map a. Now, when we change map b, these changes will also be made to Map a, since they point to the same memory construct.
In you case, you have a List this in itself is a memory construct. Each item in the list references a seperate Map. These are each also created somewhere in memory. The moment you retreive a Map from the list, you retreive the reference to the memory space where the actual map is located. After that, it works exactly as the example.
addAll() will copy all the elements to the current list from the Collection/List you passed as an argument to this method. In your case, as each element is a reference to Map object, after copying, you have 2 references pointing to same Map object, so changes done using any one reference are visible through the other one.
The short answer is yes, put() for timeSpan is affecting resultList, because when a method returns a Map, which is got from the List, it returns a reference to the heap where map elements are located.

final reference object get modified

I'm turning crazy on this one.
What I want to do is to get an object from an ArrayList, and add it in 3 others after modifying it.
The problem is that the object I get from my ArrayList is modified aswell when I modify the others... Can't figure out why, is this normal?
Code below:
final Product tmpRef = productsRef.get(i);
Product tmp = tmpRef;
tmp.setPos(products1.size());
Log.d("test2","tmpRef:"+tmpRef.getPos()+";tmp:"+tmp.getPos());
Product tmp2 = tmpRef;
tmp2.setPos(products2.size());
Log.d("test2","tmpRef:"+tmpRef.getPos()+";tmp:"+tmp.getPos()+";tmp2:"+tmp2.getPos());
Product tmp3 = tmpRef;
tmp3.setPos(products3.size());
Log.d("test2","tmpRef:"+tmpRef.getPos()+";tmp:"+tmp.getPos()+";tmp2:"+tmp2.getPos()+";tmp3:"+tmp3.getPos());
tmp.setPos(products1.size());
"pos" is just a simple int, with a getter/setter.
LogCat output:
03-21 09:56:14.926: D/test2(6200): tmpRef:9;tmp:9
03-21 09:56:14.926: D/test2(6200): tmpRef:7;tmp:7;tmp2:7
03-21 09:56:14.926: D/test2(6200): tmpRef:0;tmp:0;tmp2:0;tmp3:0
With #FD_, #blackbelt and #Guidobaldo da Montefelt's explainations, I ended creating a simple new Porduct's constructor to be able to copy the object, and not only the reference. Thanks guys.
Essentially, you just store references in your ArrayList, so it does not matter whether you use something like Product tmp = tmpRef;. tmp still points to the same object, thus changes are applied to both.
In this case, final just means that a pointer cannot be changed once it has been set.
Just search for deep copy java for possible solutions.
final Product tmpRef
means that the Object's reference of tmpRef can not be change. Meaning that you can not do
tmpRef = null;
or
tmpRef = anotherProdocut;
after
final Product tmpRef = productsRef.get(i);,
but the content of the object could change if its member are not final.
Adding to what #blackbelt said, you are getting confused between
"final" and "immutable" concepts. If you don't want the "object's
value" to change, then you have to make it immutable (java doesn't
provide any built-in mechanism for this). final merely prevents you
from reassigning the reference to another object.
EDIT:
1. final - if a "reference" is made final, then you cannot reassign it to point to something else. if a primitive is made final, then it's value cannot change.
eg:
final int i=5;
i=10; // error
final List<String> l = new ArrayList<String>();
l.add("one"); // works
l =new ArrayList<String>() // fails
2. Immutable - you cannot change the values of attributes of the object.
if list l were immutable, then
List<String> l = new ArrayList<String>();
l.add("one"); // fails, depending on your definition/implementation of immutablility
l =new ArrayList<String>() // l is not final, so it works fine
Before doing such a thing, you should really try to understand the difference between references and objects, and more deeply, the difference between copying a reference vs copying an object.
Product is an object stored in a memory zone called Heap. "tmpRef" is a reference that contains the address that points to that object.
This code:
Product tmp = tmpRef;
does not copy any "Product" objects but it simply assign the tmpReference in a new reference variable called tmp. So, you´ll have 1 Object pointed by 2 references. Passing a reference (4 bytes in a 32bit system) is very cheap. That´s why you can pass objects in your methods as parameters. It´s what it is called "copy by reference".
The final keyword means that the reference is immutable, not the Product object.
Probably you have confusion because these logic is not applied to primitive objects, where reference copy does not exist, but primitives are always copied by value, and never by reference.
So if you want to do not change the original object, you do need to create new Product objects in the heap.
Hope this helps.

Understanding Java References in Java collections

I am having trouble understanding some simple code relating with Lists and maps. Take the code below as an example:
public class test {
private Map<Integer, List<String>> myMap ;
public test(){
myMap = new HashMap<Integer, List<String>>();
}
public void addToMap(String ss){
List<String> myTemp = myMap.get(ss);
Random r = new Random();
if(myTemp == null){
myTemp = new ArrayList<String>();
myMap.put(r.nextInt(100), myTemp);
}
myTemp.add(ss);
}
public Map<Integer, List<String>> getMap1(){
return myMap;
}
public static void main(String args[]){
test myTest = new test();
myTest.addToMap("abdc");
myTest.addToMap("eeer");
System.out.println(myTest.getMap1());
}
}
How exactly does the addToMap() add a new element to the mylist Map. More specifically, how does myTemp.add(ss) add a new element to myMap when myTemp is a local variable and gets deleted once its done executing. Moreover, removing myTemp.add(ss) from the addToMap() method prints out an empty HashMap in the main method, why is this? How does the put method insert the element into the map when it is executed before the add method? Thanks.
Edit: I edited the code to make a little more sense.
Unlike C++, in Java all variables are references to the real objects. So, when you do
myTemp = new Object();
you create an object in the heap whose reference is copied to the stack variable myTemp. If the reference is added to the list (or other variable), a copy of the reference is added there.
Once myTemp is destroyed, the object continues alive. The exception is when all the references to the object are gone; then the object can not be reached by the code and Garbage Collection can (but it is not forced to) delete it from memory.
myTemp isn't the thing - the list - it is instead a reference to the thing. But it isn't that specific reference which you are putting into the map; it is the thing to which it refers. The map has its own reference to the thing, so deleting the myTemp reference doesn't affect the map.
myTemp is a reference to the list. The list is allocated in heap memory and myTemp is actually is a pointer stored on the stack. Whenever you exit the function it gets deleted but the memory allocated in heap memory won't get deleted. Memories allocated in heap memory release when there is no reference to them by Garbage Collector.
You are right. When you leave the addToMap() method, the myTemp will be released(Actually it will be poped from the stack) but the list which is allocated in heap memory space will remain. Everything which is created using new keyword, will be placed inside heap memory space.
Read this link Java Reference Objects
From a best practice point of view, calling a HashMap "mylist" is problematic in a number of ways. It isn't orthogonal and "mylist" doesn't tell you a lot about its usage.
In answer to your question, it's a simple case of, if the item exists in the map, it is retrieved and the string added to it, if not the list is created and added to the map. As long as the map is in scope, the references to any lists added to it (even if they were created in the scope of a function) remain in scope themselves.
However, this isn't a helpful example at all as it mixes its naming and provides no real world use-case.

ArrayList objects

Need some inputs:
Lets say i have N ArrayList and in each i am adding foo() object.
Foo foo = new Foo()
A.add(foo);
B.add(foo);
N.add(foo);
Now modification done on any one foo() object will reflect in all the other arraylist?
If YES WHY? and
whether this behaviour can also be achieved using any other collection like Vector etc...?
IF i make foo as null will it reflect in all arraylist?
Yes, because all lists only contain a reference to the same objects
Yes, all collections work like that
No, because you can only set a reference to null, and each list has a copy of the reference.
Any implementation of Collection API such as ArrayList or Vector hold reference to an object in heap memory so when you would get an index of a List by get(index) method, you achive reference to object so:
Yes, if get an index of list by get(index) method and then change the stat of the achieved object, changes stay in memory.
Yes,All Collection API have this behavior.
No,When achieve to a index of list, act is: "You achieve a copy of reference to object" and when set it to null, list instance don't any change.

How objects are added to data structures - is just their reference added?

When adding an object to a data structure the reference to the object is always added, not the object itself. So below code snippets are fundamentally the same ?
Snippet 1 -
Object objectRef = new Object();
Vector vector = new Vector();
vector.add(objectRef);
Object object = (Object)vector.get(0);
Snippet 2 -
Vector vector = new Vector();
vector.add(new Object());
Object object = (Object)vector.get(0);
Java always passes references by value. So whenever you use a reference as an argument to a function call, the reference value is copied. The object itself is not passed, a copy of its reference is. Its an important distinction to understand. It also is important to understand that with the reference you (or the library) can still operate on the object passed via its reference.
Your snippets are not entirely the same. In (1) you end up with two references to the same object, objectRef and object. In (2) you end up with only 1, object. In both cases the vector has a different reference to the object.
Both snippets are the same, the only difference is that you have a location variable named objectRef which can be modified after it was added to the vector, if it is modified, it will be reflected in the object returned from the vector as well since it IS the same object. Though the "Object" class doesn't exactly have any methods which can modify the object.
That's correct. In the java programming language there is only heap. Every object exists out there in magic heap land. Every non-primitive value is just a pointer whose only operation is to dereference. A List can be thought of as a list of pointers to strings, the actual data content of the strings is out on the heap, not inside the collection structure.
(This is the semantics of the language of course. Modern runtimes are under no obligation to actually do it that way, the JIT uses method stacks, it just has to behave exactly the same as if it worked that way.)
They result in the same state except that at the end of snippet 2, you don't have a symbol called objectRef.
Both snippets give the same results. The difference lies that in the first snippet you have three references to the same object (objectRef, object and a reference in vector), and in the second snippet you only have two references to the same object (object and a reference in vector).

Categories