'작업노트/JAVA'에 해당되는 글 8건

  1. 2010.07.27 자바의 가비지 콜렉터(Gavage Collector)
  2. 2009.07.17 Java TimeZone 관련 링크
  3. 2009.03.02 CSV파일
  4. 2008.02.28 자바 5 에서의 반복자와 컬렉션(for/in 반복문)
  5. 2008.01.05 Java API Map
  6. 2007.11.19 JVM 메모리구조와 스택 - 참조 ^^ 1
  7. 2007.11.19 자바에서 swap 구현하기
  8. 2007.05.29 Vector or ArrayList -- which is better?

http://qwe1qwe.tistory.com/1476

http://jikime.tistory.com/313

요약설명
가비지 콜렉터는, 더이상 참조하지 않는 오브젝트가 점유하고 있는 메모리를 회수하여, 할당가능한 상태로 만드는 것.
동작방식을 이해하려면 먼저 JVM의 3가지 메모리 영역를 알아야 한다.
1)new/young영역 : 생성된지 얼마 안된 객체가 저장된다.
a) eden : 새롭게 생성된 모든 객체가 저장된다.
b) survivor space 1, survivor space 2 : old 영역으로 넘어가기 전 객체들이 저장.
2)old영역 : 생성된지 오래된 객체가 저장된다.
3)permanent 영역 : jvm클래스와 메서드 객체가 저장된다.

이중에서 new/young영역과 old영역에서 서로 다른 GC(Gavage Collector)가 동작한다.
new/young영역에서는 minor GC가 동작하는 데, 한번 동작할때마다 다른 survivor 영역으로 객체를 이동시킨다. 즉 첫번째 동작에서는 eden영역과 survivor1에서 살아있는 객체들을 survivor2영역으로 이동시키고, 두번째 동작에서는 eden영역과 survivor2영역에서 살아있는 객체들을 survivor1으로 이동시킨다. 그리고 각 동작마다 죽어있는, 사용되지 않는 객체들은 clear시킨다.
new/young영역에서는 이런식으로 동작을 하다가, 생성된지 오래된 객체들은 old영역으로 이동하게 된다. old영역에서는 full GC가 객체들을 관리하는데, 객체들의 reference를 따라가면서 연결이 끊긴 객체들을 마크하고 이것들을 삭제한다. 이 작업은 상당히 오래 걸리기 때문에, full gc가 동작하는 동안 사용자들의 요청이 쌓이게 되면 장애발생의 원인이 될수 있다..
작업노트/JAVA l 2010. 7. 27. 09:39
작업노트/JAVA l 2009. 7. 17. 13:15

출처 : http://narrowway.tistory.com/43


CSV파일

Comma Separated Value의 약자로,
데이타를 컴머(,)단위로 잘라서 나열한 파일 형태.

컴머단위로 잘라져 있으므로, 텍스트 파일로도 읽을수 있지만,
엑셀파일로도 바로 읽을수 있다는 장점이 있어서
많은 데이타 관련 소프트웨어에서 사용되고 있다.

1. 자바에서의 구현

    // 파일 다운로드 형식 지정
    res.setContentType("application/octet-stream;charset=Shift_JIS");
    res.setHeader("Content-Disposition","attachment; filename="+selected_Table+"_"+date+".csv");
    PrintWriter out=res.getWriter();

    // 데이타를 DB로 부터 읽어서 CSV로 만드는 과정
      query = "select * from " + selected_Table;
     
      PreparedStatement pstmt = con.prepareStatement(query);
      rs = pstmt.executeQuery();

      String strData = "";
      while(rs.next()){
       
        for(int i=0 ; i < selectedFieldList.length ; i++){
     strData = rs.getString(Integer.parseInt(selectedFieldList[i])+1);
     if(strData==null){strData="";}
      out.print( new String( strData.getBytes("8859_1"),"Shift_JIS") );
            out.print(i<selectedFieldList.length ? "," : "");
        }
        out.print(System.getProperty("line.separator"));
      }

      위와같이 컴마와 System.getProperty("line.separator")를 이용해서 제어가 가능하고,
      문자열처리과정을 일본어의 경우 new String( strData.getBytes("8859_1"),"Shift_JIS")
      처럼 해결해 주면 된다.

2. 주의할 점

     CSV파일의 가장 주의점이라 한다면 데이타 자체에 컴마나 줄바꿈 표시가 들어가 있는 경우로,
     이때는 의도하지 않는 데이타상의 구분이 생겨버리므로,
     향후 엑셀로 읽으려 할때에는 전체 테이블이 깨져버린다.
    
      private String checkDataFormat(String data){

           String returnVal = data;
   
           returnVal = returnVal.replaceAll(","," ");
           returnVal = returnVal.replaceAll("\n"," ");
           returnVal = returnVal.replaceAll("\r"," ");
           returnVal = returnVal.replaceAll("\r\n"," ");
   
           return returnVal;
     }

     와 같은 방법으로 데이타 치환을 해주어야 한다. 

작업노트/JAVA l 2009. 3. 2. 17:03

 디자인 패턴 책을 보던 중 새로운 사실을 알게 되었다.


 for/in이라는 선언문이 자바 5 부터 추가되었다는 내용이다.


for/in 선언문은 collection 류의 객체들과 배열에 대해서
편리하게 순환문(반복문)을 돌릴 수 있게 해준다.


형식은 다음과 같다.

for(Object obj: collection) {

    ...

}


단, 컬렉션은 제네릭을 사용하여 어떤 객체를 담는 컬렉션인지 명시해야한다.


ArrayList<Movie> movieList = (ArrayList<Movie>)getMovieList();


for(Movie movie: movieList) {

System.out.println("Movie Name is : " + movie.getName() );

}


이런 식으로 말이다.

예전 C#을 잠깐 공부할때 유사한 문법을 본적있는데 역시 자바에도 존재하였다.

좀 더 편리하게 코딩을 할 수 있을 듯.


작업노트/JAVA l 2008. 2. 28. 16:02
사용자 삽입 이미지
작업노트/JAVA l 2008. 1. 5. 12:50

원본 http://blog.naver.com/solleil7829/30023682694


JVM의 메모리구조와 호출스택에 대해서 설명합니다.


호출스택은 예외처리(Exception Handling)를 바르게 이해하는데 꼭 필요하므로


잘봐두시기 바랍니다.

======================================================================


응용프로그램이 실행되면, JVM은 시스템으로부터 프로그램을 수행하는데 필요한 메모리를 할당받고 JVM은 이 메모리를 용도에 따라 여러 영역으로 나누어 관리한다.
그 중 3가지 주요영역(Method Area, 호출스택, Heap)에 대해서 알아보도록 하자.



[참고] cv는 클래스변수, lv는 지역변수, iv는 인스턴스변수를 뜻한다.

1. 메소드영역(Method Area)
- 프로그램 실행 중 어떤 클래스가 사용되면, JVM은 해당 클래스의 클래스파일(*.class)을 읽어서 분석하여 클래스에 대한 정보(클래스 데이타)를 Method Area에 저장한다.
이 때, 그 클래스의 클래스변수(class variable)도 이 영역에 함께 생성된다.


2. 힙(Heap)
- 인스턴스가 생성되는 공간. 프로그램 실행 중 생성되는 인스턴스는 모두 이 곳에 생성된다. 즉, 인스턴스변수(instance variable)들이 생성되는 공간이다.

3. 호출스택(Call Stack 또는 Execution Stack)
호출스택은 메서드의 작업에 필요한 메모리 공간을 제공한다. 메서드가 호출되면, 호출스택에 호출된 메서드를 위한 메모리가 할당되며, 이 메모리는 메서드가 작업을 수행하는 동안 지역변수(매개변수 포함)들과 연산의 중간결과 등을 저장하는데 사용된다. 그리고, 메서드가 작업을 마치게 되면, 할당되었던 메모리공간은 반환되어 비워진다.
각 메서드를 위한 메모리상의 작업공간은 서로 구별되며, 첫 번째로 호출된 메서드를 위한 작업공간이 호출스택의 맨 밑에 마련되고, 첫 번째 메서드 수행중에 다른 메서드를 호출하게 되면, 첫 번째 메서드의 바로 위에 두 번째로 호출된 메서드를 위한 공간이 마련된다.
이 때 첫 번째 메서드는 수행을 멈추고, 두 번째 메서드가 수행되기 시작한다. 두 번째로 호출된 메서드가 수행을 마치게 되면, 두 번째 메서드를 위해 제공되었던 호출스택의 메모리공간이 반환되며, 첫 번째 메서드는 다시 수행을 계속하게 된다. 첫 번째 메서드가 수행을 마치면, 역시 제공되었던 메모리 공간이 호출스택에서 제거되며 호출스택은 완전히 비워지게 된다.
호출스택의 제일 상위에 위치하는 메서드가 현재 실행 중인 메서드이며, 나머지는 대기상태에 있게 된다.
따라서, 호출스택을 조사해 보면 메서드 간의 호출관계와 현재 수행중인 메서드가 어느 것인지 알 수 있다.
호출스택의 특징을 요약해보면 다음과 같다.


- 언제나 호출스택의 제일 위에 있는 메서드가 현재 실행 중인 메서드이다.
- 아래에 있는 메서드가 바로 위의 메서드를 호출한 메서드이다.


반환타입(return type)이 있는 메서드는 종료되면서 결과값을 자신을 호출한 메서드(caller)에게 반환한다. 대기상태에 있던 호출한 메서드(caller)는 넘겨받은 반환값으로 수행을 계속 진행하게 된다.

[예제6-6] CallStackTest.java

class CallStackTest {
      public static void main(String[] args) {
            firstMethod();
      }

      static void firstMethod() {
            secondMethod();
      }

      static void secondMethod() {
            System.out.println("secondMethod()");            
      }
}
[실행결과]
secondMethod()

위의 예제를 실행시켰을 때, 프로그램이 수행되는 동안 호출스택의 변화를 그림과 함께 살펴보도록 하자



(1)~(2) 위의 예제를 컴파일한 후 실행시키면, JVM에 의해서 main메서드가 호출됨으로써 프로그램이 시작된다. 이때, 호출스택에는 main메서드를 위한 메모리공간이 할당되고 main메서드의 코드가 수행되기 시작한다.
(3) main메서드에서 firstMethod()를 호출한 상태이다. 아직 main메서드가 끝난 것은 아니므로 main메서드는 호출스택에 대기상태로 남아있고 firstMethod()의 수행이 시작된다.
(4) firstMethod()에서 다시 secondMethod()를 호출했다. firstMethod()는 secondMethod()가 수행을 마칠 때까지 대기상태에 있게 된다. seoundMethod()가 수행을 마쳐야 firstMethod()의 나머지 문장들을 수행할 수 있기 때문이다.
(5) secondMethod()에서 println메서드를 호출했다. 이때, println메서드에 의해서 화면에 "secondMethod()"가 출력된다.
(6) println메서드의 수행이 완료되어 호출스택에서 사라지고 자신을 호출한 secondMethod()로 되돌아간다. 대기 중이던 secondMethod()는 println메서드를 호출한 이후부터 수행을 재개한다.
(7) secondMethod()에 더 이상 수행할 코드가 없으므로 종료되고, 자신을 호출한 firstMethod()로 돌아간다.
(8) firstMethod()에도 더 이상 수행할 코드가 없으므로 종료되고, 자신을 호출한 main메서드로 돌아간다.
(9) main메서드에도 더 이상 수행할 코드가 없으므로 종료되어, 호출스택은 완전히 비워지게 되고 프로그램은 종료된다.

[예제6-7] CallStackTest2.java

class CallStackTest2 {
      public static void main(String[] args) {
            System.out.println("main(String[] args)이 시작되었음.");
            firstMethod();
            System.out.println("main(String[] args)이 끝났음.");
     }
      static void firstMethod() {
            System.out.println("firstMethod()이 시작되었음.");
            secondMethod();
            System.out.println("firstMethod()이 끝났음.");            
     }

      static void secondMethod() {
            System.out.println("secondMethod()이 시작되었음.");
            System.out.println("secondMethod()이 끝났음.");            
     }

}
[실행결과]
main(String[] args)이 시작되었음.
firstMethod()이 시작되었음.
secondMethod()이 시작되었음.
secondMethod()이 끝났음.
firstMethod()이 끝났음.
main(String[] args)이 끝났음.
작업노트/JAVA l 2007. 11. 19. 23:33

자바에서는 두 프리미티브 변수의 값을 바꾸는 메소드인 swap을 구현하기가 까다로운 듯 하다

왜냐하면 자바에서는 프리미티브 타입에 대해서는 call by value만 지원하기 때문에

파라메터로 받은 값들은 서로 바꿀 수가 없다는 것이다.


어떻게 하면 좋을 것인가? 대략 생각할 수 있는 아이디어는 다음과 같다


1. swap함수에 넘어갈 파라메터를 배열로 구현한다

즉,

void swap(int[] values) {

   int temp;


   temp=values[0];

   values[0]=values[1];

   values[1]=temp;

}

문제점: 파라메터가 실제로 프리미티브 타입(기본 타입) 인데도 불구하고 배열을 만들어서 swap을 해야 한다


2. swap함수의 파라메터로 래퍼 클래스를 사용한다

void swap(Integer a, Integer b) {

     int temp;

     temp=a.intValue();

     a.setXXX(b.intValue()); // <-- Integer클래스에 setXXX에 해당하는 메소드가 없다!!!!

     b.setXXX(temp);           // <-- Integer클래스에 setXXX에 해당하는 메소드가 없다!!!!

}

문제점:

1. Integer 클래스에는 set계열 메소드가 없어서 새로운 Integer 인스턴스를 생성하지 않는한

값을 변경할 수 없다.

2. 기본 타입을 래퍼 클래스에 싸서 넘겨야 한다


3. swap 함수에 파라메터를 넘기지 않고, 멤버 변수로 넘겨서 값을 교환하도록 한다

class XXXX {

  int x, y;


  void setX(int newX) {

     x=newX;

  }

  void setY(int newY) {

     y=newY;

  }


  void swap() {

     int temp;

     temp=x;

     x=y;

     y=temp;

  }


  int getX() {

     return x;

  }

  int getY() {

     return y;

  }

};

문제점:

1. 이건 배보다 배꼽이 더크다... 사용하기 어려워 보인다 ㅡㅡ;


4. swap함수에 인덱스에 해당하는 파라메터 한개를 더 받는다

즉,

int swap(int a, int b, int index) {

   if(index==0) return a;

   if(index==1) return b;

   else return a;

}

사용할 때는

int a=100, b=90;

a=swap(a, b, 0);

b=swap(a, b, 1);


문제점:

1. 실제 돌려보면 알겠지만 이건 작동을 안한다 --;


5. 스택에 차례대로 저장했다가 꺼낼때 순서를 바꾸어서 받는다

Stack st=new Stack();


st.push(x);

st.push(y);


x=st.pop();

y=st.pop();


///////////////////////////////////////////////////////////////////////////////////

찾아서 나온 해답은 다음과 같다

///////////////////////////////////////////////////////////////////////////////////


6. JDK에서는 다음과 같이 해결했다~

swap함수에 첫번째는 배열에 해당하는 파라메터이고, 두번째와 세번째는 바꾸길 원하는 배열의

원소 번호이다. 이것의 특징은 앞에서의 swap함수는 x,y와 같이 단순한 값을 바꾸기 위한 목적이었다면 여기의 함수는 여러개 값이 있는 배열에서 특정한 값을 서로 바꾸기 위한 목적으로 사용된다는 것이다.

java.util
Class Collections

java.lang.Object | +--java.util.Collections 

...

swap

public static void swap(List list, int i, int j)
Swaps the elements at the specified positions in the specified list. (If the specified positions are equal, invoking this method leaves the list unchanged.)
Parameters:
list - The list in which to swap elements.
i - the index of one element to be swapped.
j - the index of the other element to be swapped.
Throws:
IndexOutOfBoundsException - if either i or j is out of range (i < 0 || i >= list.size() || j < 0 || j >= list.size()).
Since:
1.4

7. 흠.... 이건?

swap함수에 call by reference로 넘어 갈 수 있는 사용자 정의 객체를 만들어 넘기고

이 객체의 값을 바꿈으로써 swap 함수가 작동하도록 되어 있다.


class  Swap {
    public  int  x ,  y ;
    public  static  void  swap ( Swap  obj ) {
        int  temp ;
        temp  =  obj.x ;  obj.x  =  obj.y ;  obj.y  =  temp ;
        System.out.println ( “   swap :  x  =     +  obj.x  +  “,  y  =     +  obj.y ) ;
    }
}
public  class  CallByReference {
    public  static  void  main ( String [ ]  args ) {
        Swap  a  =  new  Swap ( ) ;
        a.x  =  1,  a.y  =  2 ;
        System.out.println ( “before :  x  =    +  a.x  +  “ , y  =    +  a.y ) ;
        Swap.swap ( a ) ;
        System.out.println ( “after :  x  =    +  a.x  +  “ , y  =    +  a.y) ;
   }
작업노트/JAVA l 2007. 11. 19. 23:31

By Tony Sintes, JavaWorld.com, 06/22/01

(http://www.javaworld.com/javaworld/javaqa/2001-06/03-qa-0622-vector.html)




Answer:

Sometimes Vector is better; sometimes ArrayList is better; sometimes you don't want to use either. I hope you weren't looking for an easy answer because the answer depends upon what you are doing. There are four factors to consider:

  • API
  • Synchronization
  • Data growth
  • Usage patterns



 

Let's explore each in turn.

API

In The Java Programming Language (Addison-Wesley, June 2000) Ken Arnold, James Gosling, and David Holmes describe the Vector as an analog to the ArrayList. So, from an API perspective, the two classes are very similar. However, there are still some major differences between the two classes.

Synchronization

Vectors are synchronized. Any method that touches the Vector's contents is thread safe. ArrayList, on the other hand, is unsynchronized, making them, therefore, not thread safe. With that difference in mind, using synchronization will incur a performance hit. So if you don't need a thread-safe collection, use the ArrayList. Why pay the price of synchronization unnecessarily?

Data growth

Internally, both the ArrayList and Vector hold onto their contents using an Array. You need to keep this fact in mind while using either in your programs. When you insert an element into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent. Depending on how you use these classes, you could end up taking a large performance hit while adding new elements. It's always best to set the object's initial capacity to the largest capacity that your program will need. By carefully setting the capacity, you can avoid paying the penalty needed to resize the internal array later. If you don't know how much data you'll have, but you do know the rate at which it grows, Vector does possess a slight advantage since you can set the increment value.

Usage patterns

Both the ArrayList and Vector are good for retrieving elements from a specific position in the container or for adding and removing elements from the end of the container. All of these operations can be performed in constant time -- O(1). However, adding and removing elements from any other position proves more expensive -- linear to be exact: O(n-i), where n is the number of elements and i is the index of the element added or removed. These operations are more expensive because you have to shift all elements at index i and higher over by one element. So what does this all mean?

It means that if you want to index elements or add and remove elements at the end of the array, use either a Vector or an ArrayList. If you want to do anything else to the contents, go find yourself another container class. For example, the LinkedList can add or remove an element at any position in constant time -- O(1). However, indexing an element is a bit slower -- O(i) where i is the index of the element. Traversing an ArrayList is also easier since you can simply use an index instead of having to create an iterator. The LinkedList also creates an internal object for each element inserted. So you have to be aware of the extra garbage being created.

Finally, in "PRAXIS 41" from Practical Java (Addison-Wesley, Feb. 2000) Peter Haggar suggests that you use a plain old array in place of either Vector or ArrayList -- especially for performance-critical code. By using an array you can avoid synchronization, extra method calls, and suboptimal resizing. You just pay the cost of extra development time.

------------------------------------------------------------------------------------

아무래도 가장중요한건


스레드 안전 여부.

작업노트/JAVA l 2007. 5. 29. 21:55
1 

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

카테고리

분류 전체보기 (117)
작업노트 (98)
Error Handling (19)
JAVA (8)
JSP & Servlet (6)
Framework (10)
HTML & Script (15)
IDE (3)
LINUX (4)
Books (2)
Search Engine (7)
etc. (10)
Developer's Diary (13)
Patterns (0)
Database (1)
거미줄세상 (12)
쌓기 (1)
책읽기 (0)
Reviews (4)

달력

«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
get rsstistory! Tistory Tistory 가입하기!