์ž๋ฐ”๋กœ ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๊ฐ€ 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๋‹จ์œ„๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ฝ˜์†”์— ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ์ฝ๊ธฐ๊ฐ€ ํŽธํ•˜๋‹ค.