forked from algorhythms/Algo-Quicksheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapterMemoryComplexity.tex
89 lines (74 loc) · 2.28 KB
/
chapterMemoryComplexity.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
\chapter{Memory Complexity}
\section{Introduction}
When discussing memory complexity, need to consider both
\begin{enumerate}
\item \textbf{Heap}: the declared variables' size.
\item \textbf{Stack}: the recursive functions' call stack.
\end{enumerate}}
\subsection{Memory for Data Type}
The memory usage is based on Java.\\
\begin{tabular}{ll}
\hline\noalign{\smallskip}
\textbf{Type} & \textbf{Bytes} \\
\noalign{\smallskip}\hline\noalign{\smallskip}
boolean & 1 \\
byte & 1 \\
char & 2 \\
int & 4 \\
float & 4 \\
long & 8 \\
double & 8\\
\noalign{\smallskip}\hline\noalign{\smallskip}
\caption {for primitive types}
\end{tabular}
\begin{tabular}{ll}
\hline\noalign{\smallskip}
\textbf{Type} & \textbf{Bytes} \\
\noalign{\smallskip}\hline\noalign{\smallskip}
char[] & 2N+24 \\
int[] & 4N+24 \\
double[] & 8N+24 \\
T[] & 8N+24 \\
\noalign{\smallskip}\hline\noalign{\smallskip}
\caption{for one-dimensional arrays}
\end{tabular}
\begin{tabular}{ll}
\hline\noalign{\smallskip}
\textbf{Type} & \textbf{Bytes} \\
\noalign{\smallskip}\hline\noalign{\smallskip}
char[][] & 2MN \\
int[][] & 4MN \\
double[][] & 8MN \\
\noalign{\smallskip}\hline\noalign{\smallskip}
\caption{for two-dimensional arrays}
\end{tabular}
\begin{tabular}{ll}
\hline\noalign{\smallskip}
\textbf{Type} & \textbf{Bytes} \\
\noalign{\smallskip}\hline\noalign{\smallskip}
Object overhead & 16 \\
Reference & 8 \\
Padding & 8x \\
\noalign{\smallskip}\hline\noalign{\smallskip}
\caption{for objects}
\end{tabular}
\\
Notice:
\begin{enumerate}
\item The reference takes memory of 8 bytes.
\item Reference includes object reference and innner class reference.
\item T[] only considers reference; if consider underlying data structure, the memory is 8N+24+xN, where $x$ is the underlying data structure memory for each element.
\item Padding is to make the object memory size as a 8's multiple.
\end{enumerate}
\subsection{Example}
The generics is passed as Boolean:
\begin{java}
public class Box<T> { // 16 (object overhead)
private in N; // 4 (int)
private T[] items; // 8 (reference to array)
// 8N+24 (array of Boolean references)
// 24N (underlying Boolean objects)
// 4 (padding to round up to a multiple)
}
\end{java}
Notice the multiple levels of references.