1 public class Accumulator 2 { 3 private double total: 4 private int N: 5 } 6 7
ID: 3883421 • Letter: 1
Question
1 public class Accumulator 2 { 3 private double total: 4 private int N: 5 } 6 7 public class Transaction 8 { 9 private final String who: 10 private final Date when: 11 private final double amount: 12 } 13 14 public class FixedCapacityStackOfStrings 15 { 16 private String [] a = new String[C]: // stack entries 17 private int N: // size 18 } 19 21 22 class INode implements Comparablecbyte [] > 23 { 24 protected byte [] name: 25 protected long modificationTime: 26 protected long accessTime: 27 } 28 29 class INodeFile extends INode { 30 private long header: 31 private Block [] blocks: 32 } 33 34 class INodeDirectory extends INode 35 { 36 private INodeFile[] children: 37 } 38 39 public class Block implements Comparable 40 { 41 private long blockId: 42 private long numBytes: 43 private long generationStamp: 44 } Assuming a 64-bit CPU architecture and Java 7, given the class definitions described in Listing 1 compute the amount of memory needed by each of the following objects considering their respective assumptions: Transaction size is ? (assuming references to String and Date objects) FixedCapacityStackOfStrings size is ? (assuming capacity C = 8 and fixed size string entries of size S = 8 chars. Remember to include the amount of memory required to store the strings in your calculation) Please write at least 50 words to explain their sizes. Also: note that in all of your calculations you should assume a 64-bit CPU architecture and Java 7. Don't forget to consider the padding in your calculation.Explanation / Answer
public class accumulator // 16 bytes for reference to the class definition as machine is 64 bit
{
private double total; // 8 bytes
private int N; // 2 bytes
}
Total memory is 16+8+2=26 ,However, due to alignment (also called padding), the JVM allocates the memory in multiples of 8 bytes, so instead of 26 bytes it would allocate 32 bytes.
So memory for class accumulator is : 32 bytes
public class transaction // 16 bytes for reference to the class definition
{
private final string who; // 48 bytes for string
private final date when; // 8 bytes
private final double amount; // 8 bytes for double
}
This means even if the string contains no characters, it will require 4 bytes for the char array reference, plus 3*4=12 bytes for the three int fields, plus 8 bytes of object header. This gives 24 bytes (which is a multiple of 8 so no "padding" bytes are needed so far).
Then, the (empty) char array will require a further 12 bytes (arrays have an extra 4 bytes to store their length), plus in this case 4 bytes of padding to bring the memory used by the char array object up to a multiple of 16. So in total, an empty string uses 40 bytes.
If the String contains, here, 3 characters, then the String object itself still requires 24 bytes. But now the char array requires 12 bytes of header plus 3*2=6 bytes for the 3 chars. Since 12+6=18 isn't a multiple of 8, we also need to round up to the next multiple of 8 (24). So overall, our 3-character String will use up 24+24 = 48 bytes.
so finally 16+48+8+8 =80 which is multiple of 8, no extra bytes are required
public class FixedCapacityofstrings // 16 bytes
{
private string[] a = new String[C]; // As of now considering string of size C is 0 then 50 bytes
private int N; // 4 bytes
}
So 16+4+50=70 , which is not multiple of 8-- so 72 bytes
calss Inode implements Comparable <byte[]> // 16 bytes
{
protected byte[] name; // 24 bytes
protected long modificationtime; // 8 bytes
protected long accesstime; // 8 bytes
}
Arrays. Arrays in Java are implemented as objects, typically with extra overhead for the length. An array of primitive-type values typically requires 24 bytes of header information (16 bytes of object overhead, 4 bytes for the length, and 4 bytes of padding) plus the memory needed to store the values.
so total is 56 bytes.
Similarly for class block requires 16+8+8+8 = 40 bytes
class INodeFile extends Inode // requires 16 bytes
{
private long header; // 8 bytes
private block[] blocks; // This array of refreneces So 24+8+40
}
A reference to an object typically is a memory address and thus uses 8 bytes of memory (on a 64-bit machine).
It requires 96 bytes.
For class INodeDirectory is : 16+ 24+8+96 = 144 bytes
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.