Why java use references not pointers?????
To get the answer of this we have to go the low level that is the memory allocation for the object....
Where the Object is stored???
A quick answer::an Object is stored on the heap??
what is the Heap???
The heap is a large block of memory that Java uses to store objects........
How the things are Happening?????
When a new object is created, the necessary amount of space is set aside for it in the heap. The new operator returns a reference to the block of memory in the heap where the object is stored. The virtual machine is responsible for managing the heap and making sure that the same block of memory is not used for two different objects or arrays at the same time.
The exact size of the heap is system-dependent. However, the heap is finite on all systems. In some Java implementations, the heap can grow if more space is needed. On others the size of the heap is fixed when the virtual machine starts up....
suppose this is a heap actually this is a fragmented heap..
Whenever we starts our program we come with a heap fairly empty,and after running our program for some time some objects are created and some are garbage collected so we leave up with a frangmented heap..
(suppose redblocks are occupied by some object and gray ones are free..and each block represent a word of memory)
and for a while just leave the references and think that you have been provided with the direct pointers to the heap memory as in C ..so our scenario will look like this:
This is the same heap, with object variables shown as ovals. The arrows are pointers into the heap. Each object has at least one pointer (to its own data), and some have multiple pointers if they themselves contain references to other objects.Furthermore, one object may be pointed to from several different places. This interconnected web of pointers makes it very difficult to move objects in the heap, because you have to update all of the different pointers that can exist in hundreds of different objects, methods, and threads...
Now suppose an object is created which requires 4 words of memory ,,now as we can see on the heap there is no free 4 words contiguous memory for the object,So the Heap has to be Defragmented.and this is what a Defragmented Heap looks like::
This is the Frangmented Heap and now there is space for a four-word object. However, many — perhaps most — of the pointers are broken. Some now point to the wrong object. Others point to nowhere in particular. The VM can try to identify every reference to each moved object in the running program and update it with the new address of its data, but there can be thousands of these, and the operation can be extremely time consuming in a large program.
Now How the problem can be solved?????
One way to look at the problem is that references point to areas of different sizes in the heap. If you could somehow arrange it so that every object needed exactly the same amount of space in the heap, then fragmentation would not be a problem. As long as there was any free space at all,it could be used.
References TO the Rescue....
Of course, different objects do take different amounts of space, but references always take same amount of memory. The solution is to insert an extra block of references between the references in your source code and the heap. When an object is moved in the heap,only one link needs to be updated: the one between the offset table and the data in the heap. The many more pointers to the offset table do not need to be updated.
Now our scenario will look like this::
So,we are provided with reference ,,not the direct pointer to the heap,another reason may be the security or platform independence to give the preference to the reference variables over pointers..........
0 comments:
Post a Comment