πŸ’» Programming/Java

[Java] Heap Memory Check (μžλ°” νž™λ©”λͺ¨λ¦¬ μ‚¬μš©λŸ‰ μΈ‘μ •)

μΌ€μ΄μΉ˜ 2018. 7. 28. 08:35

μžλ°”λ‘œ κ°œλ°œμ„ ν•˜λ‹€κ°€ OOM이 λ°œμƒν•˜λŠ” κ²½μš°κ°€ λ°œμƒν•˜λ©΄ νž™λ©”λͺ¨λ¦¬κ°€ μ–΄λŠ μ§€μ μ—μ„œ μ¦κ°€ν•˜λŠ”μ§€ νŒŒμ•…μ„ ν•΄μ•Όν•œλ‹€. ν™•μΈν•˜λŠ” 방법은 크게 두 가지 방법이 μžˆλ‹€. μ†ŒμŠ€λ ˆλ²¨ μˆ˜μ •μ΄ κ°€λŠ₯ν•œ κ²½μš°μ™€ μ†ŒμŠ€λ ˆλ²¨ μˆ˜μ •μ΄ λΆˆκ°€λŠ₯ν•œ 경우둜 λ‚˜λ‰˜λŠ”λ°, μ†ŒμŠ€λ ˆλ²¨ μˆ˜μ •μ΄ λΆˆκ°€λŠ₯ν•˜λ©΄ μžλ°” νž™ 덀프λ₯Ό λ– μ„œ 확인을 ν•  수 μžˆλ‹€. 이번 ν¬μŠ€νŒ…μ—μ„œλŠ” μ†ŒμŠ€λ ˆλ²¨ μˆ˜μ •μ΄ κ°€λŠ₯ν•œ 경우, 즉, 개발 μ‹œμ μ—μ„œ ν™•μΈν•˜λŠ” 방법을 μ–˜κΈ°ν•΄λ³΄λ € ν•œλ‹€.


μžλ°”λŠ” κ°œλ°œμžλ“€μ—κ²Œ μœ μš©ν•œ λΌμ΄λΈŒλŸ¬λ¦¬λ“€μ€ 기본적으둜 μ œκ³΅ν•˜κ³ μžˆμœΌλ©°, λ©”λͺ¨λ¦¬ μ‚¬μš©λŸ‰ κ΄€λ ¨ν•˜μ—¬ Runtime ν΄λž˜μŠ€μ—μ„œ λ©”μ„œλ“œλ₯Ό μ œκ³΅ν•˜κ³ μžˆλ‹€.


Java 8 API λ¬Έμ„œλ₯Ό 보면 μ•„λž˜μ™€ 같이 μ„€λͺ…이 λ˜μ–΄μžˆλ‹€.


Class Runtime



  • public class Runtime
    extends Object
    Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.

    An application cannot create its own instance of this class.

즉, λͺ¨λ“  μžλ°” ν”„λ‘œκ·Έλž¨λ“€μ€ ν•˜λ‚˜μ˜ Runtime μΈμŠ€ν„΄μŠ€λ₯Ό 가지고 μžˆλŠ”λ°, 이 RuntimeμΈμŠ€ν„΄μŠ€λŠ” ν”„λ‘œκ·Έλž¨μ΄ μ‹€ν–‰λ˜κ³ μžˆλŠ” ν™˜κ²½κ³Ό μΈν„°νŽ˜μ΄μŠ€ν•  수 μžˆλ„λ‘ ν•΄μ€€λ‹€. 즉, μžλ°”ν”„λ‘œκ·Έλž¨μ΄ μ‹€ν–‰λ˜κ³ μžˆλŠ” μ‹œμŠ€ν…œμ˜ 정보λ₯Ό κ°€μ Έμ˜¬ 수 μžˆλ‹€λŠ” 것이닀. (λͺ…λ Ήμ–΄ 싀행도 κ°€λŠ₯ν•˜λ‹€.)


그럼 Runtime ν΄λž˜μŠ€μ—μ„œ μ–΄λ–€ λ©”μ„œλ“œλ“€μ„ μ œκ³΅ν•˜λŠ”μ§€ ν•œλ²ˆ 보자.


Modifier and Type

Method and Description
voidaddShutdownHook(Thread hook)
Registers a new virtual-machine shutdown hook.
intavailableProcessors()
Returns the number of processors available to the Java virtual machine.
Processexec(String command)
Executes the specified string command in a separate process.
Processexec(String[] cmdarray)
Executes the specified command and arguments in a separate process.
Processexec(String[] cmdarray, String[] envp)
Executes the specified command and arguments in a separate process with the specified environment.
Processexec(String[] cmdarray, String[] envp, File dir)
Executes the specified command and arguments in a separate process with the specified environment and working directory.
Processexec(String command, String[] envp)
Executes the specified string command in a separate process with the specified environment.
Processexec(String command, String[] envp, File dir)
Executes the specified string command in a separate process with the specified environment and working directory.
voidexit(int status)
Terminates the currently running Java virtual machine by initiating its shutdown sequence.
longfreeMemory()
Returns the amount of free memory in the Java Virtual Machine.
voidgc()
Runs the garbage collector.
InputStreamgetLocalizedInputStream(InputStream in)
Deprecated. 
As of JDK 1.1, the preferred way to translate a byte stream in the local encoding into a character stream in Unicode is via the InputStreamReader and BufferedReader classes.
OutputStreamgetLocalizedOutputStream(OutputStream out)
Deprecated. 
As of JDK 1.1, the preferred way to translate a Unicode character stream into a byte stream in the local encoding is via the OutputStreamWriterBufferedWriter, and PrintWriter classes.
static RuntimegetRuntime()
Returns the runtime object associated with the current Java application.
voidhalt(int status)
Forcibly terminates the currently running Java virtual machine.
voidload(String filename)
Loads the native library specified by the filename argument.
voidloadLibrary(String libname)
Loads the native library specified by the libname argument.
longmaxMemory()
Returns the maximum amount of memory that the Java virtual machine will attempt to use.
booleanremoveShutdownHook(Thread hook)
De-registers a previously-registered virtual-machine shutdown hook.
voidrunFinalization()
Runs the finalization methods of any objects pending finalization.
static voidrunFinalizersOnExit(boolean value)
Deprecated. 
This method is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock.
longtotalMemory()
Returns the total amount of memory in the Java virtual machine.
voidtraceInstructions(boolean on)
Enables/Disables tracing of instructions.
voidtraceMethodCalls(boolean on)
Enables/Disables tracing of method calls.


μœ„ λ©”μ„œλ“œλ“€μ΄ Runtime ν΄λž˜μŠ€μ—μ„œ μ œκ³΅ν•˜κ³ μžˆλŠ” λ©”μ„œλ“œλ“€μ΄λ‹€.

이 μ€‘μ—μ„œ λ©”λͺ¨λ¦¬ κ΄€λ ¨ λ©”μ„œλ“œλ“€μ΄ λͺ‡ 개 보인닀.

첫 번째둜 freeMemoryκ°€ μžˆλŠ”λ°, 이 λ©”μ„œλ“œλŠ” νž™λ©”λͺ¨λ¦¬ μ˜μ—­μ—μ„œ freeλ©”λͺ¨λ¦¬λ₯Ό λ°”μ΄νŠΈλ‹¨μœ„λ‘œ κ³„μ‚°ν•΄μ„œ λ°˜ν™˜ν•΄μ€€λ‹€. 그리고 maxMemory와 totalMemoryκ°€ μžˆλŠ”λ° totalMemoryλŠ” ν˜„μž¬ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ μ‚¬μš©ν•˜κ³ μžˆλŠ” 총 νž™ λ©”λͺ¨λ¦¬λ₯Ό μ˜λ―Έν•˜λ©°, maxMemoryλŠ” OOM(OutOfMemory)μ˜ˆμ™Έκ°€ λ°œμƒν•˜λŠ” λ©”λͺ¨λ¦¬ μ‚¬μ΄μ¦ˆλ₯Ό λ°˜ν™˜ν•΄μ€€λ‹€. μ—­μ‹œ λ°”μ΄νŠΈ λ‹¨μœ„μ΄λ©°, μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ 이 λ©”λͺ¨λ¦¬ ν•œκ³„λ₯Ό λ„˜μ–΄μ„œλŠ” μˆœκ°„ OOM이 λ°œμƒν•˜λ©΄μ„œ ν”„λ‘œκ·Έλž¨ 싀행이 μ€‘λ‹¨λœλ‹€.


μ‚¬μš©μ˜ˆλ₯Ό 보면 μ•„λž˜μ™€ κ°™λ‹€.


// ν˜„μž¬ νž™ λ©”λͺ¨λ¦¬ μ‚¬μ΄μ¦ˆλ₯Ό λ°”μ΄νŠΈ λ‹¨μœ„λ‘œ λ°˜ν™˜

long heapSize = Runtime.getRuntime().totalMemory(); 


// μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ— ν• λ‹Ήλœ νž™λ©”λͺ¨λ¦¬ μ‚¬μ΄μ¦ˆ. 이 μ‚¬μ΄μ¦ˆλ₯Ό λ„˜μ–΄μ„œλ©΄ OOM λ°œμƒ

long heapMaxSize = Runtime.getRuntime().maxMemory();


// ν˜„μž¬ λ‚¨μ•„μžˆλŠ” freeλ©”λͺ¨λ¦¬. ν”„λ‘œκ·Έλž¨μ΄ μ‹€ν–‰λ˜λŠ” λ™μ•ˆ 증감을 반볡

long heapFreeSize = Runtime.getRuntime().freeMemory();


ν•΄λ‹Ή μ‚¬μ΄μ¦ˆλ₯Ό λ°˜ν™˜λ°›μ€ λ’€ KB, MBλ‹¨μœ„λ‘œ λ³€ν™˜ν•΄μ£ΌλŠ” λ©”μ„œλ“œλ₯Ό λ§Œλ“€μ–΄μ„œ μ½˜μ†”μ— 좜λ ₯ν•΄μ£Όλ©΄ 읽기가 νŽΈν•˜λ‹€.