simulated pointers limitations of java pointers may be used for internal data structures only. data...
TRANSCRIPT
![Page 1: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/1.jpg)
Simulated Pointers
![Page 2: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/2.jpg)
Limitations Of Java Pointers
• May be used for internal data structures only.
• Data structure backup requires serialization and deserialization.
• No arithmetic.
![Page 3: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/3.jpg)
Simulated-Pointer Memory Layout
Data structure memory is an array, and each array position has an element field (type Object) and a next field (type int).
c a e d b
![Page 4: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/4.jpg)
Node Representationpackage dataStructures;
class SimulatedNode
{
// package visible data members
Object element;
int next;
// constructors not shown
}
![Page 5: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/5.jpg)
How It All Looks
14
c a e d b
c a e d b
0 1 2 3 4 5 8 11 14
14 011 8-1
firstNode = 4
next
element
![Page 6: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/6.jpg)
Still Drawn The Same
a b c d e-1
firstNode
![Page 7: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/7.jpg)
Memory Management
Linked system (Java or simulated pointer) requires:
• a way to keep track of the memory that is not in use (i.e., a storage pool)
• way to allocate a node
Java has the method new
• way to free a node that is no longer in use
Java uses garbage collection
![Page 8: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/8.jpg)
Garbage Collection
The system determines which nodes/memory are not in use and returns these nodes (this memory) to the pool of free storage.
This is done in two or three steps:
Mark nodes that are in use.
Compact free space (optional).
Move free nodes to storage pool.
![Page 9: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/9.jpg)
Marking
Unmark all nodes (set all mark bits to false).
Start at each program variable that contains a reference, follow all pointers, mark nodes that are reached.
c a e d b
firstNode
![Page 10: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/10.jpg)
Marking
c a e d b
firstNode
c a e d e
Repeat for all reference variables.
Start at firstNode and mark all nodes reachable from firstNode.
![Page 11: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/11.jpg)
Compaction
Move all marked nodes (i.e., nodes in use) to one end of memory, updating all pointers as necessary.
c b e d b
firstNode
a e d Free Memory
![Page 12: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/12.jpg)
Put Free Memory In Storage Pool
Scan memory for unmarked nodes (if no compaction done), otherwise put single free block (unless no free memory) into pool.
![Page 13: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/13.jpg)
Advantages Of Garbage Collection
• Programmer doesn’t have to worry about freeing nodes as they become free.
• However, for garbage collection to be effective, we must set reference variables to null when the object being referenced is no longer needed.
![Page 14: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/14.jpg)
Advantages Of Garbage Collection
• Applications may run faster when run on computers that have more memory.
![Page 15: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/15.jpg)
Disadvantage Of Garbage Collection
• Garbage collection time is linear in memory size (not in amount of free memory).
![Page 16: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/16.jpg)
Alternative To Garbage Collection
Provide a method to free/deallocate a node.
e.g., delete method of C++
Now free nodes are always in storage pool.
![Page 17: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/17.jpg)
Advantage Of Alternative
• Time to free nodes is proportional to number of nodes being freed and not to total memory size.
![Page 18: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/18.jpg)
Disadvantages Of Alternative
• User must write methods to free data structure nodes.
• Time is spent freeing nodes that may not be reused.
• Application run time does not improve with increase in memory size.
![Page 19: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/19.jpg)
Storage Pool Organization When All Nodes Have Same Size
a b c d enull
firstNode
• Maintain a chain of free nodes
• Allocate from front of chain
• Add node that is freed to chain front
![Page 20: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/20.jpg)
Simulated-Pointer Memory Management
/** memory management for simulated pointer classes */
package dataStructures;
import utilities.*;
public class SimulatedSpace1
{
// data members
private int firstNode;
SimulatedNode [] node; // package visible
// constructor and other methods come here
}
![Page 21: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/21.jpg)
Constructor public SimulatedSpace1(int numberOfNodes)
{
node = new SimulatedNode [numberOfNodes];
// create nodes and link into a chain
for (int i = 0; i < numberOfNodes - 1; i++)
node[i] = new SimulatedNode(i + 1);
// last node of array and chain
node[numberOfNodes - 1] = new SimulatedNode(-1);
// firstNode has the default initial value 0
}
![Page 22: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/22.jpg)
Allocate A Nodepublic int allocateNode(Object element, int next)
{// Allocate a free node and set its fields.
if (firstNode == -1)
{ // double number of nodes, code omitted
}
int i = firstNode; // allocate first node
firstNode = node[i].next; // firstNode points to next free node
node[i].element = element;
node[i].next = next;
return i;
}
![Page 23: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/23.jpg)
Free A Node
public void deallocateNode(int i)
{// Free node i.
// make i first node on free space list
node[i].next = firstNode;
firstNode = i;
// remove element reference so that space can be garbage
// collected
node[i].element = null;
}
![Page 24: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/24.jpg)
Simulated Pointers• Can allocate a chain of nodes without having to
relink.
• Can free a chain of nodes in O(1) time when first and last nodes of chain are known.
![Page 25: Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization](https://reader035.vdocument.in/reader035/viewer/2022062720/56649ef65503460f94c09ac2/html5/thumbnails/25.jpg)
Simulated Pointers
• Don’t use unless you see a clear advantage to using simulated pointers over Java references.