Zeta Oph's Study

[Data Structure (Java)] Memory Usage Analysis 본문

프로그래밍/Java

[Data Structure (Java)] Memory Usage Analysis

Zeta Oph 2024. 6. 12. 01:59

https://crane206265.tistory.com/93

 

[Data Structure (Java)] Tree (6) - 2-3 Tree & LLRB Tree

https://crane206265.tistory.com/92  [Data Structure (Java)] Tree (5) - AVL Treehttps://crane206265.tistory.com/90 [Data Structrue (Java)] Tree (4) - Binary Search Tree Maphttps://crane206265.tistory.com/89 [Data Structure (Java)] Sethttps://crane206265

crane206265.tistory.com

깜빡하고 메모리 사용을 안한거 같아서. 시험 전 마지막 글이지 않을까요..? (사실 소재는 남았는데, 시간이 없는..)


Background Knowledge

bit : 0 or 1

1 bite = 8 bits

 

* --> 64-bit computer : computer with 8 byte pointers

 

Memory Usage of Primitive Types

Type Memory Usage Type Usage
int 4 bytes boolean 1 byte
float 8 bytes byte 1 byte
long 8 bytes char 2 bytes
double 8 bytes    

Memory Usage of Personally Designed Object

reference : 8 bytes

 

object overhead : 16 bytes

- ref. to the object's class, garbage collection, information synchronization

--> 8 bytes (reference) + 8 bytes (for others) = 16 bytes

 

* padding : each object uses a multiple of 8 bytes (also padding for overhead)

 

--> object's memory usage = object overhead + variables in object + padding

 

 

* wrapper type memory != primitive type memory

 

 

Ex 1) int array of length N

array overhead : 24 bytes

[ object overhead 16 bytes + array length (int value) 4 bytes + padding 4 bytes ]

+

element : 4N bytes

= 24 + 4N bytes (if N is odd : add padding 4 bytes)

 

Ex 2) array of N objects 

array overhead : 24 bytes

+

references to objects : 8N bytes

+

each objects (K bytes) * N

= 24 + (8+K)N bytes (if needed, add padding)

 

 

* double a[];

: just using memory for reference (8 bytes), not already generated array