> For the complete documentation index, see [llms.txt](https://hyune.gitbook.io/hyune-wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hyune.gitbook.io/hyune-wiki/legacy/study/framework-and-library/monitoring/visualvm/undefined-1.md).

# 문자열 생성으로 테스트

```java
// -Xmx512m -Xms512m

@Slf4j
@RestController
public class AddStringController {

	@GetMapping("/add-string/run")
	public void run(@RequestParam final Integer millis) throws InterruptedException {
		final Thread thread = new Thread(new AddStringRunnable());
		thread.start();

		Thread.sleep(millis);
		thread.interrupt();
	}

	public static class AddStringRunnable implements Runnable {

		private final List<String> list = new ArrayList<>();

		public void run() {
			log.info("### AddString start");
			final byte[] array = new byte[7];
			try {
				while (true) {
					new Random().nextBytes(array);
					list.add(new String(array, StandardCharsets.UTF_8));
					Thread.sleep(0);
				}
			} catch (final InterruptedException e) {
				log.info("### AddString interrupt");
			}
		}
	}
}
```

![](https://469240294-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmUhmp4iHOU0Crksy87Kh%2Fuploads%2FwUukgcpPleMAIizHodPp%2Fimage.png?alt=media\&token=7ab04182-3bbd-4b65-bef7-7e3c465cfbc8)

* OOM 발생

![](https://469240294-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmUhmp4iHOU0Crksy87Kh%2Fuploads%2FNlusWRSq9DZrPjckO6T2%2Fimage.png?alt=media\&token=f81d75dc-1b3d-445c-8af7-b02d5f7bac59)

* `heap dump summary`
  * String이 다수를 차지하고 있음을 확인.

![](https://469240294-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmUhmp4iHOU0Crksy87Kh%2Fuploads%2F9ZC9OfOWPU5mFqlQ1S2B%2Fimage.png?alt=media\&token=7d71adfe-8890-4a13-b307-ee7fe2c65bd8)

* `Objects - GC roots`
  * Retained가 압도적으로 높고 테스트를 위해 만든 클래스에서 발생함을 확인 가능.

### 기타

* 더 완벽한 로그를 위해서는 heap dump가 아닌 thread dump를 떠야 합니다.

{% embed url="<https://www.baeldung.com/java-thread-dump>" %}
