String์ ์ฐ์ง๋ง?
// ๊ฐ๋น์ง ์ปฌ๋ ์
์ ์คํํ์ฌ ์ด๊ธฐํ
System.gc();
try {
Thread.sleep(100); // GC๊ฐ ์๋ฃ๋ ์๊ฐ์ ์ฃผ๊ธฐ ์ํ ๋๊ธฐ ์๊ฐ
} catch (InterruptedException e) {
e.printStackTrace();
}
// ์คํ ์ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ & ์๊ฐ ์กฐํ
long startTime = System.currentTimeMillis();
long before = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
System.out.println("Before memory: " + before);
// ์ธก์ ํ๊ณ ์ถ์ ์ฝ๋
// ์คํ ํ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ์กฐํ
long after = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
System.out.println("After memory: " + after);
// ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ์ธก์
long usedMemory = (after - before) / 1024 / 1024;
System.out.println("Used Memory: " + usedMemory + " MB");
// ์ ํ๋ฆฌ์ผ์ด์
์ ํ ๋น๋ ํ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ด์ฆ. ์ด ์ฌ์ด์ฆ๋ฅผ ๋์ด์๋ฉด OOM ๋ฐ์
long heapSize = Runtime.getRuntime().maxMemory();
System.out.println("Heap Size: " + heapSize / 1024 / 1024 + " MB");
long endTime = System.currentTimeMillis();
long durationInMillis = endTime - startTime;
double durationInSeconds = durationInMillis / 1000.0;
System.out.println("์๋ต ์๊ฐ: " + durationInSeconds + "์ด");String, StringBuffer, StringBuilder ์ด๋ค๊ฑธ ์ฌ์ฉํ์ง?
์ธก์ ๊ฒฐ๊ณผ
๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋
์๋ต ์๊ฐ
String
StringBuffer
StringBuilder
Last updated