LinkedNestBlog

[Performance Tunning] Java System Class

by 이진원Dev
System Class


유용한 메서드

arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
특정 배열을 복사할 때 사용. src는 복사 원본 배열, dest는 복사한 값이 들어가는 배열, srcPos는 원본의 시작위치, destPos는 복사본의 시작 위치, length는 복사하는 개수

예제
1
2
3
4
5
6
7
8
9
...
public static void main(String args[]) {
       String[] arr = new String[] {"jan", "feb", "mar", "apr", "may"};   
       String[] copiedArr = new String[3];   
    System.arraycopy(arr, 2, copiedArr, 1, 2);   
    for ( String copiedVal : copiedArr ){      
        System.out.println(copiedVal);   
    }
...
cs

설명
원본 배열의  2번 위치부터 복사. 때문에 "mar"부터 복사를 시작한다.
copiedArr배열의 1번 위치부터 복사한 데이터를 저장하고, 복사하는 개수가 두 개이기 때문에 "apr"까지 복사를 수행한다.
결과
null
mar
apr
추가설명
만약 length를 3이상으로 지정하면 copiedArr의 크기가 3이기 떄문에 ArrayIndexOutOfBoundException이 발생.

Java JVM에서 사용할 수 있는 설정
1. 속성(property)값
- JVM에서 지정된 값
- 속성을 사용하는 메서드

static Properties getProperties() : 현재 자바 속성 값들을 받아온다.
static String getProperty(String key) : key에 지정된 Java 속성 값을 받아온다
static String getProperty(String key, String def) : key에 지정된 Java 속성 값을 받아온다. def는 해당 key가 존재하지 않을 경우 지정할 기본값.
static void setProperties(Properties props) : props 객체에 담겨 있는 내용을 java 속성에 지정한다.
static void setProperty(String key, String value) : Java 속성에 지정된 key의 값을 value값으로 변환한다.

예제
1
2
3
4
5
6
7
8
9
10
11
...
public static void main(String args[]){
         System.setProperty("Java Tunning", "곱슬리");
   Properties prop = System.getProperties();
   Set key = prop.KeySet();
   Iterator iter = key.iterator();
   while(iter.hasNext()){
      String currentKey = it.next().toString();
      System.out.format("%2=%2\n", currentKey, prop.getProperty(currentKey));
   }
}
cs

설명
Java Tuning이라는 키를 갖는 System Property에 '곱슬리'라는 value를 지정한 후, System Property 전체 값을 화면에 출력
static Map<String, String> getenv() : 현재 시스템 환경 값 목록을 String 형태의 MAP으로 return.
static String getenv(String name) : name에 지정된 환경 변수의 값을 얻는다.

예제
1
2
3
4
5
6
7
8
9
10
11
...
public static void main(String args[]){
      Map<String, String> envMap = System.getenv();
   Set key = envMap.keySet();
   Iterator iter = key.iterator();
   while(iter.hasNext()){
      String currentKey = iter.next().toString();
      System.out.format("%2=%2\n", currentKey, envMap.get(currentKey));
   }
}
...
cs

설명
윈도우 command 창에서 'set' 명령어를 치는 결과와 동일

2. 환경(environment)값

- 장비(서버)에 지정되어 있는 값

Native Library를 활용할 때 사용할 수 있는 System 클래스
- static void load(String filename) : 파일명을 지정하여 Native Library를 로딩
- static void loadLibrary(String libname) : Library 명을 지정하여 Native Library를 로딩

운영중인 코드에 절대로 사용해서는 안되는 메서드
- static void gc() : Java 에서 명시적으로 해제하도록 GC를 수행하는 메서드
- static void exit(int status) : 현재 수행중인 VM을 멈춘다. 이 메서드는 절대로 수행하면 안된다. 
- static void runFinalization() : Object객체에 있는 finalize()라는 메서드는 자동으로 호출되는데, 가비지 콜랙터가 알아서 해당 객체를 더 이상 참조할 필요가 없을 때 호출. 하지만 이 메서드를 호출하면 참조 해제 작업을 기다리는 모든 객체의 finalize() 메서드를 수동으로 수행해야 한다.

시간 관련 메서드(System.currentTimeMills와 System.nanoTime)
static long currentTimeMills() : 현재의 시간을 ms로 return (1/1000초). UTC라는 시간 표준 체계를 따르는데, 1970년 1월 1일부터의 시간을 long type으로 return. 이 시간 값을 변환하면 현재 날짜를 구할 수도 있다.
static long nanoTime() : 현재의 시간을 ns로 return (1/1,000,000,000초)