본문 바로가기
Program Language/JAVA

Java Object메모리 와 String Class고찰

by Leo 리오 2011. 8. 2.
반응형

자바 여러가지나
퍼포먼스 등 잘나와있다.

http://www.javamex.com/



Java Object
메모리 사용량

Java type

Bytes required

boolean

1

byte

char

2

short

int

4

float

long

8

double

reference

(pointer)

4

array

+4 (for length)

모든 Object8byteHeader(Housekeeping data)를 갖고, 무조건 8의 배수이다.

ex) 1개의 boolean을 가지고 있다면

Header 8 + boolean 1 + padding 7 = 18bytes

 

Array 기본

Header 8 + length정보 4 + Object*length + padding

(boolean array는 무조건 boolean하나당 8bytes)

 

 

String

C라면 unicode를 다루지 않기 때문에 1byte*문자수 + 1byte이지만 Java에선 전혀 다르다.

Minimum String memory usage (bytes) = 8 * (int) ((((no chars) * 2) + 45) / 8)

 

Housekeeping 8 + array 12 + padding(multiple of 16bytes (32-bit))

문자 1개당 2byte

다른 여러 변수들(사용하지 않더라도)

 

다시, 아무것도 없는 String

{ char[]Rerference 4 + 3개의 int변수 4*3 + header 8 + padding(8) 0 } + emptyArrayString 12 + padding(16) 4

=> 40bytes를 사용. (C에서는 1bytes)

 

ex) 17개의 문자열은

{ char[]Rerference 4 + 3개의 int변수 4*3 + header 8 + padding(8) 0 } + 17ArrayString (12+17*2) + padding(16) 2

=> 72바이트 사용. (C에서는 18bytes)

1. We're actually being slightly unfair here. In C, a 17-character string plus terminator may well require just 18 bytes on the stack. But if you were to allocate 18 bytes to store the string via malloc(), then that allocation from the malloc heap would generally require some extra bytes of "housekeeping", just as for a Java object. (It would typically require in the order of 8 or so bytes, howeverstill a big difference!)

 

(substringparent와 메모리 공유 -> 절약가능)

 

http://www.javamex.com/tutorials/memory/string_saving_memory.shtml

 

public class CompactCharSequence implements CharSequence, Serializable

{

static final long serialVersionUID = 1L;

 

private static final String ENCODING = "ISO-8859-1"

private final int offset

private final int end

private final byte[] data

 

public CompactCharSequence(String str)

{

try

{

data = str.getBytes(ENCODING);

offset = 0;

end = data.length

}

catch (UnsupportedEncodingException e)

{

throw new RuntimeException("Unexpected: " + ENCODING + " not supported!");

}

}

 

@Override

public char charAt(int index)

{

int ix = index + offset

if (ix >= end)

{

throw new StringIndexOutOfBoundsException("Invalid index " + index + " length " + length());

}

return (char) (data[ix] & 0xff);

}

 

@Override

public int length()

{

return end - offset

}

private CompactCharSequence(byte[] data, int offset, int end)

{

this.data = data;

this.offset = offset;

this.end = end;

}

@Override

public CharSequence subSequence(int start, int end)

{

if (start < 0 || end >= (this.end - offset))

{

throw new IllegalArgumentException("Illegal range " + start + "-" + end + " for sequence of length " + length());

}

return new CompactCharSequence(data, start + offset, end + offset);

}

@Override

public String toString()

{

try

{

return new String(data, offset, end - offset, ENCODING);

}

catch (UnsupportedEncodingException e)

{

throw new RuntimeException("Unexpected: " + ENCODING + " not supported");

}

}

}

반응형

댓글