์๋ฐ๋ก ๊ฐ๋ฐ์ ํ๋ค๊ฐ OOM์ด ๋ฐ์ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ฐ์ํ๋ฉด ํ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ด๋ ์ง์ ์์ ์ฆ๊ฐํ๋์ง ํ์ ์ ํด์ผํ๋ค. ํ์ธํ๋ ๋ฐฉ๋ฒ์ ํฌ๊ฒ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ค. ์์ค๋ ๋ฒจ ์์ ์ด ๊ฐ๋ฅํ ๊ฒฝ์ฐ์ ์์ค๋ ๋ฒจ ์์ ์ด ๋ถ๊ฐ๋ฅํ ๊ฒฝ์ฐ๋ก ๋๋๋๋ฐ, ์์ค๋ ๋ฒจ ์์ ์ด ๋ถ๊ฐ๋ฅํ๋ฉด ์๋ฐ ํ ๋คํ๋ฅผ ๋ ์ ํ์ธ์ ํ ์ ์๋ค. ์ด๋ฒ ํฌ์คํ ์์๋ ์์ค๋ ๋ฒจ ์์ ์ด ๊ฐ๋ฅํ ๊ฒฝ์ฐ, ์ฆ, ๊ฐ๋ฐ ์์ ์์ ํ์ธํ๋ ๋ฐฉ๋ฒ์ ์๊ธฐํด๋ณด๋ ค ํ๋ค.
์๋ฐ๋ ๊ฐ๋ฐ์๋ค์๊ฒ ์ ์ฉํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๊ณ ์์ผ๋ฉฐ, ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ๊ด๋ จํ์ฌ Runtime ํด๋์ค์์ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๊ณ ์๋ค.
Java 8 API ๋ฌธ์๋ฅผ ๋ณด๋ฉด ์๋์ ๊ฐ์ด ์ค๋ช ์ด ๋์ด์๋ค.
Class Runtime
- java.lang.Object
- java.lang.Runtime
public class Runtime extends Object
Every Java application has a single instance of classRuntime
that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from thegetRuntime
method.An application cannot create its own instance of this class.
์ฆ, ๋ชจ๋ ์๋ฐ ํ๋ก๊ทธ๋จ๋ค์ ํ๋์ Runtime ์ธ์คํด์ค๋ฅผ ๊ฐ์ง๊ณ ์๋๋ฐ, ์ด Runtime์ธ์คํด์ค๋ ํ๋ก๊ทธ๋จ์ด ์คํ๋๊ณ ์๋ ํ๊ฒฝ๊ณผ ์ธํฐํ์ด์คํ ์ ์๋๋ก ํด์ค๋ค. ์ฆ, ์๋ฐํ๋ก๊ทธ๋จ์ด ์คํ๋๊ณ ์๋ ์์คํ ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค๋ ๊ฒ์ด๋ค. (๋ช ๋ น์ด ์คํ๋ ๊ฐ๋ฅํ๋ค.)
๊ทธ๋ผ Runtime ํด๋์ค์์ ์ด๋ค ๋ฉ์๋๋ค์ ์ ๊ณตํ๋์ง ํ๋ฒ ๋ณด์.
Modifier and Type | Method and Description |
---|---|
void | addShutdownHook(Thread hook) Registers a new virtual-machine shutdown hook. |
int | availableProcessors() Returns the number of processors available to the Java virtual machine. |
Process | exec(String command) Executes the specified string command in a separate process. |
Process | exec(String[] cmdarray) Executes the specified command and arguments in a separate process. |
Process | exec(String[] cmdarray, String[] envp) Executes the specified command and arguments in a separate process with the specified environment. |
Process | exec(String[] cmdarray, String[] envp, File dir) Executes the specified command and arguments in a separate process with the specified environment and working directory. |
Process | exec(String command, String[] envp) Executes the specified string command in a separate process with the specified environment. |
Process | exec(String command, String[] envp, File dir) Executes the specified string command in a separate process with the specified environment and working directory. |
void | exit(int status) Terminates the currently running Java virtual machine by initiating its shutdown sequence. |
long | freeMemory() Returns the amount of free memory in the Java Virtual Machine. |
void | gc() Runs the garbage collector. |
InputStream | getLocalizedInputStream(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. |
OutputStream | getLocalizedOutputStream(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 OutputStreamWriter , BufferedWriter , and PrintWriter classes. |
static Runtime | getRuntime() Returns the runtime object associated with the current Java application. |
void | halt(int status) Forcibly terminates the currently running Java virtual machine. |
void | load(String filename) Loads the native library specified by the filename argument. |
void | loadLibrary(String libname) Loads the native library specified by the libname argument. |
long | maxMemory() Returns the maximum amount of memory that the Java virtual machine will attempt to use. |
boolean | removeShutdownHook(Thread hook) De-registers a previously-registered virtual-machine shutdown hook. |
void | runFinalization() Runs the finalization methods of any objects pending finalization. |
static void | runFinalizersOnExit(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. |
long | totalMemory() Returns the total amount of memory in the Java virtual machine. |
void | traceInstructions(boolean on) Enables/Disables tracing of instructions. |
void | traceMethodCalls(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๋จ์๋ก ๋ณํํด์ฃผ๋ ๋ฉ์๋๋ฅผ ๋ง๋ค์ด์ ์ฝ์์ ์ถ๋ ฅํด์ฃผ๋ฉด ์ฝ๊ธฐ๊ฐ ํธํ๋ค.
'๐ป Programming > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
AWS S3 Security Credentials ๋ง๋ค๊ธฐ (0) | 2018.08.17 |
---|---|
AWS S3 ๋ฒํท์ ํ์ผ ์ ๋ก๋ํ๊ธฐ (์๋ฐ) (0) | 2018.08.17 |
[Java/์๋ฐ] ํ ์คํธ ํ์ผ ๋ด์ฉ ์ฝ์ด์ ์ถ๋ ฅํ๊ธฐ (0) | 2016.12.04 |
ํ๋ก์ ํธ ํ์ฌ ๊ฒฝ๋ก ์ป๊ธฐ ( Getting current working directory ) (0) | 2016.06.15 |
์๋ฐ ๊ฐ์ฒด๋ฅผ byte[] ๋ก ๋ณํํ๊ธฐ (0) | 2015.08.08 |