java GC ์์ธ ๋ฐ ํด๊ฒฐ๋ฐฉ์
GC์์ธ
------- (์ถ์ฒ : http://ir.bagesoft.com/100 )
|
Caused by: java.lang.OutOfMemoryError: GC overhead limit exceeded ์์ธ: ๋ฉ๋ชจ๋ฆฌ ํ๊ณ ์ด๊ณผ
ํด๊ฒฐ: -Xmx ์ต์
์ผ๋ก ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ ํ ๋น.
๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ถ์กฑํ์ฌ ๊ฐ๋น์ง ์ปฌ๋ ์
์ด ์ด๋ฃจ์ด ์ก์ผ๋,
์๋ก ํ๋ณด๋ ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ ์ฒด ๋ฉ๋ชจ๋ฆฌ์ 2% ๋ฏธ๋ง์ด์ด์ ์๊ธฐ๋ ์ค๋ฅ. ํ ๋ง๋๋ก ๋ ์ด์ ๊ฐ๋น์ง ์ปฌ๋ ์
์ ํ ์ ์์ ์ ๋๋ก ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ค๋ ๊ฒ์.
๊ธฐ์กด) java -Xms1g -Xmx1g -jar Project.jar
ํด๊ฒฐ) java -Xms4g -Xmx4g -jar Project.jar |
์๋ ์ถ์ฒ : http://ir.bagesoft.com/97
|
64bit OS ๋ฅผ ์ฌ์ฉํ๊ณ , ์๋ผ์ง ๋ง๊ณ RAM์ ์ถ๊ฐ |
CPU๋ง ์ง์ํ๋ค๋ฉด, ๋ฌด์กฐ๊ฑด 64bit ์ด์์ฒด์ ๋ฅผ ์ค์นํด์ ์ฌ์ฉํ๋ผ. RAM์ด 4GB ์ดํ๋ผ๋ ์ค์นํด๋ผ. CPU์ ์ฒ๋ฆฌ ๋จ์ ์ฆ๊ฐ ๋ฟ๋ง ์๋๋ผ, ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ต๋ํ ์ฌ์ฉํ ์ ์๊ธฐ ๋๋ฌธ์ GC์ ํ์๊ฐ ๊ฐ์ํ์ฌ ์๋๊ฐ ๋ ๋์์ง๋ค.
Windows7 64bit ์์๋ OS๊ฐ ์ฌ์ฉํ๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ ์ธํ๊ณ ๋ชจ๋ JVM์ด ์ฌ์ฉํ ์ ์์๋ค. 1GB ํ์ผ์ ์์ฑํ๋๋ฐ, ๋๋ต 5~10๋ฐฐ ์ ๋์ ์คํ ์๊ฐ ์ฐจ์ด๊ฐ ์์๋ค. |
|
xms, xmx ์ต์ ์ ์ต๋ํ ํฌ๊ฒ, ๊ทธ๋ฆฌ๊ณ ๊ฐ๊ฒ |
(1) Windows7 64bit (๋ฌผ๋ฆฌ์ ๋ฉ๋ชจ๋ฆฌ๊ฐ 4g์ธ ๊ฒฝ์ฐ) java -jar -xms4g -xmx4g xxx.jar
์ฌ๋ฏธ์๋ ๊ฒ์ Windows7 64bit ์์๋ ์ด ๋ ๊ฐ์ ๋ฌผ๋ฆฌ์ ๋ฉ๋ชจ๋ฆฌ ๋ณด๋ค ํฌ๊ฒ ์ก์๋ ์คํ์ด ๋๋ค. ์ด์ ๋ ๋ชจ๋ฅด๊ฒ ๋ค.
(2) Windows XP 32bit (๋ฌผ๋ฆฌ์ ๋ฉ๋ชจ๋ฆฌ๊ฐ 4g์ธ ๊ฒฝ์ฐ)
java -jar -xms1.5g -xmx1.5g xxx.jar
์๋ง 1~1.5 ์ฌ์ด๊น์ง ๊ฐ๋ฅํ ๊ฒ์ด๋ค. ๋์ด ๊ฐ๋ฉด ์คํ ์์ฒด๊ฐ ์ ๋๋๋ฐ ์คํ๊ฐ๋ฅํ ์ต๋๊ฐ์ ์ฐพ์ ์ฌ์ฉํ์. |
|
String ๋์ StringBuilder๋ฅผ ์ฌ์ฉ |
๋ฌธ์์ด์ ์ฌ๋ฌ ๊ฐ์ง ์กฐ์์ ๊ฐํ ๊ฒฝ์ฐ์ด๋ค. ๋ณํ์ง ์๋ ๋ฌธ์์ด์ ๊ฒฝ์ฐ, String์ ์ฌ์ฉํ๋ฉด ๋๋ค. String์ ์ฌ์ฉํ์ฌ๋ ์ปดํ์ผ๋ฌ๊ฐ ์ ๋นํ ๋ณํํด ์ค๋ค๊ณ ๋ ํ์ง๋ง, ์ปดํ์ผ๋ฌ๋ ํญ์ ๊ฐ๋ฐ์๋ณด๋ค ๋ฌด์ํ๋ค.
์ฐธ๊ณ ๋ก ์คํ ์๋๋ StringBuilder > StringBuffer >> String ์ด๋ฉฐ,
๋ฉ๋ชจ๋ฆฌ์ฌ์ฉ๋์ StringBuilder = StringBuffer << String ์ด๋ค.
|
|
String.split() ๋์ StringTokenizer.nextToken()์ ์ฌ์ฉ |
StringTokenizer.nextToken()๋ ์ฌ์ฉ์ด ๋ถํธํ์ง๋ง ๋น ๋ฅด๋ฉฐ, String.split()๋ ์ฌ์ฉํ๊ธฐ ํธํ์ง๋ง ๋๋ฆฌ๋ค.
token์ด ๋น ๋ฌธ์์ด์ผ ๊ฒฝ์ฐ, String.split() ์ ""๋ฅผ ํด๋น ๋ฐฐ์ด ์์์ ์ ์ฅํด ์ฃผ์ง๋ง, StringTokenizer.nextToken() ๋ null์ ๋ฆฌํดํ๋ค.
๋จ ์ ๊ท์์ ๊ตฌ๋ถ์๋ก ์ฌ์ฉํ ๊ฒฝ์ฐ๋ ์ด์ฉ ์ ์์ด String.split() ์ ์ฌ์ฉํด์ผ ํ๋ค.
|
|
Buffered~๊ณ์ด ํด๋์ค (BufferedReader, BufferedWriter ๋ฑ) ๋ฅผ ์ฌ์ฉ |
์์ธํ ๊ฒ์ ๊ฒ์์ ํตํด |
์๋ ๋ถํฐ์ ์ถ์ฒ
์คํ๋ฌธ์ฅ
|
public void testSuite() throws Exception{ int stringSize[]={100,200,500,1000,5000}; for(int i=0;i<stringSize.length;i++){ LOG_STRING=""; for(int j=0;j<stringSize[i];j++) LOG_STRING+="1234567890"; log(stringSize[i]+"0 bytes"); testFileWriter(); System.gc(); testBufferedWriter(); System.gc(); testFileOutputStream(); System.gc(); testFileBufferedOutputStream(); System.gc(); testFileChannel(); System.gc(); testFileChannelOneBuffer(); System.gc(); } } |
1. FileWriter fw = new FileWriter(LOG_HOME+"writer.log");
|
// java.io.FileWriter
private void testFileWriter() throws Exception { FileWriter fw = new FileWriter(LOG_HOME+"writer.log"); long st = Timer.getCurrentTime(); for(int i =0;i<loop;i++){ fw.write(LOG_STRING); } log("FileWriter :"+Timer.getElapsedTime(st) +"ms"); fw.close(); }
|
2. BufferedWriter bw = new BufferedWriter(new FileWriter(LOG_HOME+"writer.log"));
|
|
3. FileOutputStream fos = new FileOutputStream(LOG_HOME+"outputstream.log");
|
|
4. BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(LOG_HOME+"bufferedoutputstream.log"));
|
|
5. FileChannel fc =(new FileOutputStream(new File(LOG_HOME+"filechannel.log"))).getChannel(); + Byte Buffer ๋งค๋ฒ ์์ฑ
|
|
6. FileChannel fc =(new FileOutputStream(new File(LOG_HOME+"filechannel.log"))).getChannel(); + ByteBuffer ์ฌ์ฌ์ฉ
|
|
| 1K | 2K | 5K | 10K | 50K | |
| FileWriter | 31 | 32 | 94 | 203 | 1281 |
| FileWriter + BufferedWriter | 15 | 31 | 94 | 188 | 1000 |
| FileOutputStream | 32 | 47 | 109 | 188 | 1063 |
| FileOutputStream + BufferedOutputStream | 31 | 47 | 109 | 203 | 1578 |
| FileChannel | 47 | 63 | 109 | 219 | 2906 |
| FileChannel + Byte Buffer ์ฌ์ฌ์ฉ | 31 | 47 | 188 | 250 | 2766 |
(ํด๋น ๋ ์ฝ๋๋ฅผ 1000๋ฒ์ฉ write)