Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
gamedesigninitiativeat cornell university
the
Memory: The Basics
Lecture 8
gamedesigninitiativeat cornell university
the
� Playstation 3 � 256 MB Ram for system � 256 MB for graphics card
� X-Box 360 � 512 MB Ram for system � Shared w/ graphics card
� iPhone/iPad � 1GB Ram for system � Shared w/ graphics card
� Nintendo Wii � 88 MB Ram for system � 24 MB shared w/ graphics card
Memory Basics
Gaming Memory Constraints
2
gamedesigninitiativeat cornell university
the
� Separate pool for graphics card � Faster than general memory � Generally stores texture info � Also includes frame buffer
� But CPU has slower access � Access via general memory � Moving data to graphics
memory requires explicit I/O
� Standard structure for PCs � Game consoles are different! � Consoles: single memory pool
Memory Basics
Graphics Memory
Must Load Textures
3
gamedesigninitiativeat cornell university
the
� Shared Memory � Cores access same memory � Often have private caches � Coherency a big problem:
caches may not agree
� Nonuniform Memory � All memory is local only � Communicate w/ messages � Coherency less of an issue,
but programming is hard
Memory Basics
Multicore and Memory
X-Box 360
Playstation 3
Not Coherent
Explicit Memory Transfer
4
gamedesigninitiativeat cornell university
the
The Challenge of Memory
� Major challenge for gaming hardware � Must minimize memory footprint � Must ship memory between processors � Must ensure consistency between processors
� Less of an issue with PCs � Lots and lots of memory � Relatively fast access � Very few cores
Memory Basics 5
gamedesigninitiativeat cornell university
the
The Challenge of Memory
� Major challenge for gaming hardware � Must minimize memory footprint � Must ship memory between processors � Must ensure consistency between processors
� Less of an issue with PCs � Lots and lots of memory � Relatively fast access � Very few cores
Memory Basics
But Will Become an Issue in Future
6
gamedesigninitiativeat cornell university
the
� Secondary storage exists � Consoles have 250 GB HD
� iDevices have 64 GB Flash
� But access time is slow � HDs transfer at ~160 MB/s
� Best SSD is ~500 MB/s
� Recall 16 ms per frame � At best, can access 8 MB
� Yields uneven performance
Memory Basics 7
Why Not Virtual Memory?
gamedesigninitiativeat cornell university
the
� Pixel color is 4 bytes � 1 byte each for r, b, g, alpha � More if using HDR color
� Image a 2D array of pixels � 1280x1024 monitor size � 5,242,880 bytes ~ 5 MB
� More if using mipmaps � Graphic card texture feature � Smaller versions of image � Cached for performance � But can double memory use
Memory Basics
Memory Usage: Images
Original Image
MipMaps
8
gamedesigninitiativeat cornell university
the
� Formats often compressed � JPEG, PNG, GIF
� But not always TIFF
� Must uncompress to display � Need full pixels to uncompress � In RAM or graphics card
� Only load when needed � Texture loading is primary I/O
operation in high-end games � Many tricks to optimize � The cause of “texture popping”
Memory Basics
But My JPEG is only 8 KB!
9
gamedesigninitiativeat cornell university
the
Memory Basics 10
Traditional Memory Organization
Program Data
Heap
Stack
Free Space
Program Code Static Variables
Objects created via new Allocations with malloc
Function parameters Local variables Return values
Easy to Track
Easy to Track
Problems!
High Address
Low Address
gamedesigninitiativeat cornell university
the
Memory Basics 11
Traditional Memory Organization
Program Data
Heap
Stack
Free Space
Program Code Static Variables
Objects created via new Allocations with malloc
Function parameters Local variables Return values
High Address
Low Address
Dedicated to process.
Consists of machine addressable space.
Leverages Virtual Memory
gamedesigninitiativeat cornell university
the
Memory Basics 12
Mobile Memory Organization
Program Data
Stack
Program Data
Stack
Program Data
Stack
Program Data
Stack
Device Memory
Heap
gamedesigninitiativeat cornell university
the
� Active app takes what it can � Cannot take anything from OS � OS may suspend inactive apps
� App Suspension � App “quits” and memory freed � iOS: 5 minutes (or so) on exit � Android: If memory needed
� Suspend apps can recover � OS allows limited paging � Page out state on suspension � Page back in when restart app
Memory Basics 13
How Do Apps Compete for Memory?
gamedesigninitiativeat cornell university
the
� Active app takes what it can � Cannot steal from OS � OS may suspend inactive apps
� App Suspension � App “quits” and memory freed � iOS: 5 minutes (or so) on exit � Android: If memory needed
� Suspend apps can recover � OS allows limited paging � Page out state on suspension � Page back in when restart app
Memory Basics 14
How Do Apps Compete for Memory?
You must code this! Otherwise, data is lost.
gamedesigninitiativeat cornell university
the
Primitive Data Types
� byte: basic value (8 bits)
� char: 1 byte
� short: 2 bytes
� int: 4 bytes
� long: 8 bytes
� float: 4 bytes
� double: 8 bytes
Memory Basics
Sizing Up Memory
Complex Data Types
� Pointer: platform dependent � 4 bytes on 32 bit machine
� 8 bytes on 64 bit machine � Java reference is a pointer
� Array: data size * length � Strings similar (w/ trailing null)
� Struct: sum of struct fields � Same rule for classes � Structs = classes w/o methods
Not standard May change
IEEE standard Won’t change
15
gamedesigninitiativeat cornell university
the
class Date {
short year;
byte day;
byte month;
}
class Student {
int id;
Date birthdate;
Student* roommate;
}
2 byte
1 byte
1 bytes
4 bytes
4 bytes
4 bytes
4 or 8 bytes (32 or 64 bit)
12 or 16 bytes Memory Basics
Memory Example
16
gamedesigninitiativeat cornell university
the
class Date {
short year;
byte day;
byte month;
}
� All data types should align � Type starts at multiple of size
� Shorts at even addresses � Ints/words at multiple of 4 � Longs at multiple of 8
� Structs may require padding � Field order matters!
� Pad between fields to align � Worse on 64 bit machines
� Rule: Order large to small
Memory Basics
Memory Alignment
17
gamedesigninitiativeat cornell university
the
� All data types should align � Type starts at multiple of size
� Shorts at even addresses � Ints/words at multiple of 4 � Longs at multiple of 8
� Structs may require padding � Field order matters!
� Pad between fields to align � Worse on 64 bit machines
� Rule: Order large to small
Memory Basics
Memory Alignment
Struct w/ 3 shorts and an int: short short short int
short short short int
short short short int
short short short int
18
gamedesigninitiativeat cornell university
the
Memory Basics 19
Related Topic: Cache Lines
� All CPUs have caches � A6 (iOS): 32k L1, 1Mb L2 � Snapdragon (Nexus): 4k L0,
16k L2, 2Mb L2
� Populate with cache lines � Data block of fixed size � Relative to cache size � Fetch pulls in whole line
� Can affect performance � Accessing neighbors is fast! � Example: array scanning � Big deal for data structures
CPU Fetches the whole line into cache
gamedesigninitiativeat cornell university
the
� Collection types are costly � Even null pointers use memory
� Common for pointers to use as much memory as the pointees
� Unbalanced trees are very bad
� Even true of (pointer) arrays � Array uses additional memory
� Not so in array of structs � Objects stored directly in array � But memory alignment!
Memory Basics
Data Structures and Memory
20
gamedesigninitiativeat cornell university
the
� Collection types are costly � Even null pointers use memory
� Common for pointers to use as much memory as the pointees
� Unbalanced trees are very bad
� Even true of (pointer) arrays � Array uses additional memory
� Not so in array of structs � Objects stored directly in array � But memory alignment!
Memory Basics
Data Structures and Memory
21
gamedesigninitiativeat cornell university
the
malloc
� Based on memory size � Give it number of bytes � Typecast result to assign it � No initialization at all
� Example: char* p = (char*)malloc(4)
Memory Basics 22
C/C++: Allocation Process
new
� Based on data type � Give it a data type � If a class, calls constructor � Else no default initialization
� Example: Point* p = new Point();
Stack
? ?
… ?
Heap
n bytes
sizeof(Class)
Stack
1 0
… 1
Heap
gamedesigninitiativeat cornell university
the
malloc
� Based on memory size � Give it number of bytes � Typecast result to assign it � No initialization at all
� Example: char* p = (char*)malloc(4)
Memory Basics 23
C/C++: Allocation Process
new
� Based on data type � Give it a data type � If a class, calls constructor � Else no default initialization
� Example: Point* p = new Point();
Stack
? ?
… ?
Heap
n bytes
sizeof(Class)
Stack
1 0
… 1
Heap
Preferred in C Preferred in C++
gamedesigninitiativeat cornell university
the
malloc
� Based on memory size � Give it number of bytes � Typecast result to assign it � No initialization at all
� Example: char* p = (char*)malloc(4)
Memory Basics 24
C/C++: Allocation Process
new
� Can emulate malloc � Create a char (byte) array � Arrays not initialized � Typecast after creation
� Example: char* p = (char*)malloc(4)
Stack
? ?
… ?
Heap
n bytes
Stack
? ?
… ?
Heap
n bytes
gamedesigninitiativeat cornell university
the
Stack Based Object
� Call with () after variable � Calls constructor with args � Puts object entirely on stack � Deleted when stack popped
� Example: Point p(1,2);
Memory Basics 25
C++: Objects vs. Allocation
Heap Based Object
� Call with new syntax � Pointer on stack � Object in the heap � Must be manually deleted
� Example: Point p* = new Point(1,2)
1 2
Stack Stack
1 2
Heap
gamedesigninitiativeat cornell university
the
Stack Based Object
� Call with () after variable � Calls constructor with args � Puts object entirely on stack � Deleted when stack popped
� Example: Point p(1,2);
Memory Basics 26
C++: Objects vs. Allocation
Heap Based Object
� Call with new syntax � Pointer on stack � Object in the heap � Must be manually deleted
� Example: Point p* = new Point(1,2)
1 2
Stack Stack
1 2
Heap
� Not in Java, C#, Obj-C
� But C#/Obj-C have structs � Classes without any methods � Can exist on the stack
gamedesigninitiativeat cornell university
the
� Depends on allocation � malloc: free � new: delete
� What does deletion do? � Marks memory as available � Does not erase contents � Does not reset pointer
� Only crashes if pointer bad � Pointer is currently NULL � Pointer is illegal address
int main() {
cout << "Program started" << endl;
int* a = new int[LENGTH];
delete a;
for(int ii = 0; ii < LENGTH; ii++) {
cout << "a[" << ii << "]="
<< a[ii] << endl;
}
cout << "Program done" << endl;
}
Memory Basics 27
Manual Deletion in C/C++
gamedesigninitiativeat cornell university
the
Memory Leaks
Memory Basics
� Leak: Cannot release memory � Object allocated on the heap
� Only reference is moved � No way to reference object
� Consumes memory fast!
� Can even happen in Java � JNI supports native libraries � Method may allocate memory
� Need another method to free � Example: dispose() in JOGL
memoryArea = newArea;
28
gamedesigninitiativeat cornell university
the
A Question of Ownership
void foo() {
MyObject* o = � new MyObject();
o.doSomething();
o = null;
return;
}
void foo(int key) {
MyObject* o =� table.get(key);
o.doSomething();
o = null;
return;
}
Memory Basics
Memory Leak
Not a Leak
29
gamedesigninitiativeat cornell university
the
A Question of Ownership
void foo() {
MyObject* o =� table.get(key);
table.remove(key);
o = null;
return;
}
void foo(int key) {
MyObject* o =� table.get(key); table.remove(key); ntable.put(key,o); o = null;
return; }
Memory Basics
Memory Leak?
Not a Leak
30
gamedesigninitiativeat cornell university
the
Thread 1
void run() {
o.doSomething1();
}
Memory Basics 31
A Question of Ownership
Thread 2
void run() {
o.doSomething2();
}
Current owners of obj
Who deletes obj?
Topic of Next Lecture