1.问题描述
通过压测发现使用JAXB进行xml转Bean的时,java.lang.OutOfMemoryError: Metaspace
通过Jconsole发现,类一直在飙升
2.解决方案
通过修改代码增加缓存,不废话直接上代码
public class JaxbUtil {
//增加缓存
public static Map<Class<?>, JAXBContext> contextMap = new ConcurrentHashMap<>();
public static <T> T converyToJavaBean(String xml, Class<T> c) {
T t = null;
StringReader reader = null;
try {
//JAXBContext context = JAXBContext.newInstance(clazz);
JAXBContext context = getContext(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
reader = new StringReader(xml);
t = (T) unmarshaller.unmarshal(reader);
} catch (Exception e) {
e.printStackTrace();
} finally {