AI ์ด์ „์‹œ๋Œ€ (↓์„๊ธฐ์‹œ๋Œ€ IT)/01. Java

java GC ์›์ธ ๋ฐ ํ•ด๊ฒฐ๋ฐฉ์•ˆ

๐Ÿ“ฆ DonzBox 2014. 3. 28. 09:50

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 ์ด๋‹ค.
์ž์„ธํ•œ ๋น„๊ต๋Š” http://hongsgo.egloos.com/2033998

String.split() ๋Œ€์‹  StringTokenizer.nextToken()์„ ์‚ฌ์šฉ

StringTokenizer.nextToken()๋Š” ์‚ฌ์šฉ์ด ๋ถˆํŽธํ•˜์ง€๋งŒ ๋น ๋ฅด๋ฉฐ, String.split()๋Š” ์‚ฌ์šฉํ•˜๊ธฐ ํŽธํ•˜์ง€๋งŒ ๋А๋ฆฌ๋‹ค.
token์ด ๋นˆ ๋ฌธ์ž์—ด์ผ ๊ฒฝ์šฐ, String.split() ์€ ""๋ฅผ ํ•ด๋‹น ๋ฐฐ์—ด ์›์†Œ์— ์ €์žฅํ•ด ์ฃผ์ง€๋งŒ, StringTokenizer.nextToken() ๋Š” null์„ ๋ฆฌํ„ดํ•œ๋‹ค.
๋‹จ ์ •๊ทœ์‹์„ ๊ตฌ๋ถ„์ž๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ๋Š” ์–ด์ฉ” ์ˆ˜ ์—†์ด  String.split() ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
์„ฑ๋Šฅ๋น„๊ต๋Š”  http://ir.bagesoft.com/622 ๋ฅผ ์ฐธ๊ณ 

Buffered~๊ณ„์—ด ํด๋ž˜์Šค (BufferedReader, BufferedWriter ๋“ฑ) ๋ฅผ ์‚ฌ์šฉ

์ž์„ธํ•œ ๊ฒƒ์€ ๊ฒ€์ƒ‰์„ ํ†ตํ•ด

 

 

 

์•„๋ž˜ ๋ถ€ํ„ฐ์˜ ์ถœ์ฒ˜

http://bcho.tistory.com/288

 

์‹คํ–‰๋ฌธ์žฅ

 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"));

 
 // java.io.BufferedWriter
 private void testBufferedWriter() throws Exception {
  BufferedWriter bw = new BufferedWriter(new FileWriter(LOG_HOME+"writer.log"));
  long st = Timer.getCurrentTime();
  for(int i =0;i<loop;i++){
   bw.write(LOG_STRING);
  }
  log("BufferedWriter :"+Timer.getElapsedTime(st) +"ms");
  bw.close();
 }

 

 

 

3. FileOutputStream fos = new FileOutputStream(LOG_HOME+"outputstream.log");

 
 // java.io.FileOutputStream
 private void testFileOutputStream() throws Exception{
  FileOutputStream fos = new FileOutputStream(LOG_HOME+"outputstream.log");
  long st = Timer.getCurrentTime();
  for(int i=0;i<loop;i++){
   byte[] buf = LOG_STRING.getBytes();
   fos.write(buf);
  }
  log("FileOutputStream :"+Timer.getElapsedTime(st) +"ms");
  fos.close();
 }

 

 

4. BufferedOutputStream fos =  new BufferedOutputStream(new FileOutputStream(LOG_HOME+"bufferedoutputstream.log"));

 
 // java.io.FileOutputStream
 // + java.io.BufferedOutputStream
 private void testFileBufferedOutputStream() throws Exception{
  BufferedOutputStream fos =
   new BufferedOutputStream(
     new FileOutputStream(LOG_HOME+"bufferedoutputstream.log"));
  long st = Timer.getCurrentTime();
  for(int i=0;i<loop;i++){
   byte[] buf = LOG_STRING.getBytes();
   fos.write(buf);
  }
  log("FileBufferedOutputStream :"+Timer.getElapsedTime(st) +"ms");
  fos.close();
 }

 

 

5. FileChannel fc =(new FileOutputStream(new File(LOG_HOME+"filechannel.log"))).getChannel(); + Byte Buffer ๋งค๋ฒˆ ์ƒ์„ฑ

 
 private void testFileChannel() throws Exception {
  FileChannel fc =(new FileOutputStream(new File(LOG_HOME+"filechannel.log"))).getChannel();
  long st = Timer.getCurrentTime();
  for(int i=0;i<loop;i++){
   byte[] buf = LOG_STRING.getBytes();
   ByteBuffer bytebuffer = ByteBuffer.allocate(buf.length);
   bytebuffer.put(buf);
   bytebuffer.flip();
   fc.write(bytebuffer);
  }
  log("FileChannel  :"+Timer.getElapsedTime(st) +"ms");
  fc.close();
 }

 

 

6. FileChannel fc =(new FileOutputStream(new File(LOG_HOME+"filechannel.log"))).getChannel(); + ByteBuffer ์žฌ์‚ฌ์šฉ

 
 private void testFileChannelOneBuffer() throws Exception {
  FileChannel fc =(new FileOutputStream(new File(LOG_HOME+"filechannelonebuf.log"))).getChannel();
  int BUF_SIZE=1000;
  long st = Timer.getCurrentTime();
  ByteBuffer bytebuffer = ByteBuffer.allocate(BUF_SIZE);
  for(int i=0;i<loop;i++){
   byte[] buf = LOG_STRING.getBytes();
   int offset=0;
   int length= buf.length;
   while(offset < length){
    int chunkSize = BUF_SIZE > length-offset ? length-offset-1 : BUF_SIZE;
    bytebuffer.put(buf, offset, chunkSize);
    bytebuffer.flip();
    offset+=BUF_SIZE;
    fc.write(bytebuffer);
    bytebuffer.clear();
   }
  }
  log("FileChannel with reusing buffer:"+Timer.getElapsedTime(st) +"ms");
  fc.close();
 }

 

 

 

์ˆ˜ํ–‰์‹œ๊ฐ„
   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)