3. This question is about the principles of memory management and garbage collection for object oriented programming. Throughout the following, we consider a single running example involving fragments...

3. This question is about the principles of memory management and garbage collection for object oriented programming.
Throughout the following, we consider a single running example involving fragments in a Java-like language; each snippet of Java should be considered to build this running example further. (You will not be asked to write any code in your answers.) In your answers, make reference to the stack, activation frame, method table, object header, or heap as appropriate.
Please see the attached PDF file, thanks.



3. This question is about the principles of memory management and garbage collection for object oriented programming. Throughout the following, we consider a single running example involving fragments in a Java-like language; each snippet of Java should be considered to build this running example further. (You will not be asked to write any code in your answers.) In your answers, make reference to the stack, activation frame, method table, object header, or heap as appropriate. (a) Consider the following class definition. class Animal { List sounds; Random mood; public Animal () { mood = new Random (); sounds = new LinkedList ( Arrays.asList("quiet")); } public String makeSound () { String sound; int index = mood.nextInt(sounds.size ()); if (sounds.isEmpty ()) sound = ""; else sound = sounds.get(index); return sound; } } At run-time, the instructions for the methods Animal() and makeSound() have to be stored somewhere in memory. i. Where in memory would the instructions for these methods be stored? [2 marks] ii. How could this location be found at run-time, for an instance of to generate the code for a method invocation? [4 marks] (b) Consider two further class definitions: class Cat extends Animal { int lives; public Cat () { super (); lives = 9; kittens = new LinkedList (); sounds.addAll(Arrays.asList("meow", "purr", "hiss")); } public String watch () { return "(watches lazily)"; } } class Tiger extends Cat { public Tiger () { super (); } @Override public String makeSound () { return "ROAR"; } @Override public String watch () { return "(watches hungrily)"; } } where in both cases, “super()” is actually an alias for the constructor of the corresponding super-class (which the defined class is extending). i. Where are the instructions for the methods Cat(), Tiger(), makeSound(), and watch() stored for the Cat and Tiger classes? [2 marks] ii. Do any of the method memory locations have something in common with each other, or the methods of the class Animal? [6 marks] Turn over/ iii. How can these memory locations be found at run-time? [2 marks] (c) Consider the following snippet: Cat newCatWithSounds () { Cat figaro = new Cat(); System.out.println(figaro.makeSound ()); System.out.println(figaro.watch ()); return figaro; } i. When the method newCatWithSounds() is invoked, where in memory is the instance of Cat stored, which is created by the command “ new Cat() ” ? What is the relationship between this and the location in memory indicated by the identifier figaro? [6 marks] ii. That object (the Cat figaro) has members: an integer ‘ lives ’, a ‘Random’ object mood, and a list of Strings ‘sounds’. Where is figaro.lives stored, and where in memory are the object figaro.mood and the list pointed to by sounds stored? Is there anything about the memory locations that the members lives and sounds have in common? [8 marks] (d) Consider the following snippet: void ghostCat () { Cat cheshire = new Cat(); System.out.println(cheshire.makeSound ()); System.out.println(cheshire.watch ()); return; } i. When the method ghostCat() is invoked, and the invocation comes to an end, what happens to the memory address associated with the identifier cheshire, and the Cat object which is assigned to it? [4 marks] ii. For both the location of cheshire in memory, and the location of the Cat object to which cheshire refers, state how long it takes to make the memory they reference reusable again. What factors are important in making the memory reusable? [8 marks] iii. Describe the following possible techniques for reclaiming the memory stored by the new Cat() object: A. Mark and sweep; B. Stop and copy. In each case, describe the principle behind the technique. [8 marks] Coversheet G5035-2021-exam-resit.pdf
Aug 11, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here