✨
Java
  • 자바의 역사
  • Ch1
    • Before Start Java
      • History of Java
      • Feature of Java
      • API Documentation
      • Hello World!
      • eclipse shortcut
      • Eclipse IDE
      • GitHub to eclipse
  • Ch2
    • Variable
      • Variable
    • Variable Type
    • Character and String
    • Primitive type and range
    • printf and formatter
    • Scanner
    • Overflow of Int
    • Transition between different variable types
    • Object Array
  • CH3
    • Operator
  • CH4
    • 조건문과 반복문
    • if statement
    • switch
    • Math.random()
    • for statement
    • while statement
    • break, continue, named iterated statement
  • CH5
    • Array
    • 배열 활용
    • String Array
  • OOP
    • Intro
  • Class and Object
  • Make multiple Classes in one file
  • How to use Object
  • Object Array
  • Variable type according to declared location
  • Class Variable and Instance Variable
  • Method
  • Call Stack
  • Parameter
  • Static method(Class method) and Instance method
  • (Method)Overloading
  • Constructor
  • Constructor this()
  • Reference type variable "this"
  • Initialize variable
  • Inheritance
  • Composite
  • Single Inheritance, Object Class
  • (Method)Overriding
  • super, super()
  • package, class path
  • import, static import
  • modifier
  • access modifier
  • Encapsulation
  • Polymorphism
  • reference type transition
  • instanceof operator
  • Parameter polymorphism
  • Multiple object using one array
  • Abstract Class, Abstract Method
  • Creating Abstract Class
  • Interface
  • Interface Polymorphism
  • Interface Pros
  • Default method and static method
  • Inner Class
  • Anonymous Class
  • java.lang package and useful class
    • Object class
    • hashCode(), toString()
    • String class
    • StringBuffer class
    • StringBuilder class
    • Math class
    • Wrapper class
    • Number class
    • String to Number, String to Wrapper class
    • Auto-boxing and (auto)Unboxing
  • Collection Framework
    • Collections framework
    • List, Set, Map Interface
    • List의 removeAll() 과 clear() 비교
    • List Interface - ArrayList
    • How to view Java API source
    • List Interface - LinkedList
    • Stack and Queue
    • Iterator, ListIterator, Enumeration
    • Array
    • Comparator와 Comparable
    • Stack
    • String
    • String + char = String
    • String.toCharArray()
    • BufferedReader / BufferWriter
    • Scanner로 String 입력 - next( )와 nextLine( )
    • 공백없이 2차원배열 데이터 String으로 한번에 한줄씩 입력받기(문자/숫자)
    • 공백을 사이에 두고 빠른 2차원 배열 입출력
    • arr[index++]과 arr[index] index++의 차이
    • int와 long의 차이
    • Untitled
    • 타입 간의 변환
    • Array 와 ArrayList
    • valueOf()
    • Char
    • 변수, 객체 초기화(초기값 설정)
  • error troubleshooting
    • No enclosing instance of type
    • ASCII Code Table
    • java.lang.NumberFormatException
    • No enclosing instance..
  • reference
    • String을 생성하는 2가지 방법과 차이점
    • StackOverflowError(스택, 힙 메모리)
    • swtich-case 반복문
Powered by GitBook
On this page

Was this helpful?

  1. reference

StackOverflowError(스택, 힙 메모리)

이슈 트러블 슈팅

PreviousString을 생성하는 2가지 방법과 차이점Nextswtich-case 반복문

Last updated 3 years ago

Was this helpful?

Java 메모리 영역 중 스택과 힙 영역

  1. 스택

  • 힙 영역에 생성된 Object 타입(String,Integer,ArrayList,...) 타입의 데이터의 참조값(주소라고 생각)이 할당된다.

  • 원시타입의 데이터가 값과 함께 할당된다. - byte, short, int, long, double, float, boolean, char 타입

  • 각 Thread는 자신만의 스택을 가진다 : 스레드 하나가 새롭게 생성되는 순간 해당 스레드를 위한 스택도 함께 생성되며, 각 스레드에서 다른 스레드의 스택 영역에는 접근할 수 없다. (함수를 많이 사용해봐서 익히 알고 있는 내용)

2. 힙

  • 주로 긴 생명주기를 가지는 데이터들이 저장된다.(대부분의 Object는 크기가 크고, 코드블럭에서 공유되는 경우가 많다.)

  • 애플리케이션의 모든 메모리 중 스택에 있는 데이터를 제외한 부분이라고 보면 된다.

  • 모든 Object 타입(Integer,String,ArrayList...)은 힙 영역에 생성된다.

  • 몇 개의 스레드가 존재하든 상관없이 단 하나의 힙 영역만 존재한다.

  • 힙영역에 있는 Object들을 가리키는 레퍼런스 변수가 스택에 올라간다.

ArrayList 자료구조와 재귀함수를 이용할 때 메모리 초과가 났던 이유!

ArrayList를 사용할 경우, 힙영역에 생성되고, 재귀함수를 호출할 경우 스택에 쌓인다. 재귀함수를 호출하면서 조건에 해당하는 경우 ArrayList에 정답을 넣는다.

불필요하게 과도한 재귀함수 호출은 스택을 과도하게 사용하게 되고, 한정적인 스택 영역을 초과해버린다.

매개 변수와 지역 변수는 스택에 할당된다. (참조 유형을 사용하면 객체는 힙 에 있고 스택의 변수는 힙의 해당 객체를 참조한다.) 스택은 일반적으로 주소 공간 의 상단에 있으며, 사용 되면 주소 공간 의 하단 (즉, 0 쪽)으로 향한다.

메모리를 할당하면이 힙은 주소 공간의 상단으로 갈수록 커질 수 있다. 위의 그림처 힙 이 스택과 "충돌" 할 가능성이 있다.

스택 오버플로의 일반적인 원인은 잘못된 재귀 호출 다. 일반적으로 이는 재귀 함수에 올바른 종료 조건이 없어서 자신을 영원히 호출 할 때 발생합또다. 또는 종료 조건이 양호한 경우이를 충족하기 전에 너무 많은 재귀 호출을 요구하여 발생할 수 있다.

메모리상에서 힙과 스택