import com.carrotsearch.sizeof.RamUsageEstimator;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public class MemoryTest {
static Map<String, String> map;// map是否为static,对里面的String和Date的内存释放没有什么影响
public static void main(String\[\] args) throws InterruptedException {
Map<String, String> map;
while (true) {
map = new HashMap<>();
for (int i = 0; i < 100000; i++) {
map.put(UuidUtil.get32UUID(), new Date().toString());
}
// 1w次为2368 K,内存跳到100M就稳定
// 10w次为23680 K,内存要跳到250M、500M、750M才稳定
log.info("Data Size:{} K", RamUsageEstimator.sizeOf(map)/1024);
// log.info("Data Size:{} M", RamUsageEstimator.sizeOf(map)/1024/1024);
Thread.sleep(50000);
}
}
}


2M大小

20M大小,用static

20M大小,没用static

**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。