java.lang.OutOfMemoryError: Java heap space

一、环境

  • JDK 1.8
  • IDEA 2020.1.4
    相关工具包
  • hutool-all 5.5.6

二、报错信息

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOfRange(Arrays.java:3664)
	at java.lang.String.<init>(String.java:207)
	at java.lang.StringBuilder.toString(StringBuilder.java:407)
	at java.math.BigDecimal.toPlainString(BigDecimal.java:2976)
	at cn.hutool.core.util.NumberUtil.toStr(NumberUtil.java:2007)
	at cn.hutool.core.util.NumberUtil.toStr(NumberUtil.java:1980)
	at cn.hutool.json.InternalJSONUtil.writeValue(InternalJSONUtil.java:53)
	at cn.hutool.json.JSONObject.doWrite(JSONObject.java:606)
	at cn.hutool.json.JSONObject.write(JSONObject.java:561)
	at cn.hutool.json.JSON.toJSONString(JSON.java:112)
	at cn.hutool.json.JSONObject.toString(JSONObject.java:555)

三、精简后报错代码

    public static void main(String[] args) {
        JSONObject xmlJSONObj = XML.toJSONObject(s5);
        System.out.println(xmlJSONObj.toString());
    }

	private static  String s5 = "<number>12320322E828052845</number>";

四、解决思路

  1. 由于报错部分是用来解析XML的工具方法,此方法已经正常运行了1年了,均未出现问题,今天突然报出错误,基本可用排除代码问题,考虑特殊报文导致XML转译问题。
  2. 怀疑确实接口接收的信息过大导致,拉去接口日志信息,本地调试转移,调大本地idea内存和jvm内存,仍然报错,排除jvm参数不对。怀疑死循环导致。
  3. 一步步删减报文,最终发现在value值存在"E8"字段在中间时,可能会导致hutool转json时内存溢出,具体猜测和科学计数法有关系。

五、解决办法

定位问题后跟踪工具类方法,这里面的**toJSONObject(String string, boolean keepStrings)**方法会传入一个boolean参数,不传输时默认为false,打开后会默认把所有的值当作字符串类型解析,这样就不会引起内存溢出问题了。

	/**
	 * 转换XML为JSONObject
	 * 转换过程中一些信息可能会丢失,JSON中无法区分节点和属性,相同的节点将被处理为JSONArray。
	 * Content text may be placed in a "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]&gt;</code> are ignored.
	 *
	 * @param string The source string.
	 * @return A JSONObject containing the structured data from the XML string.
	 * @throws JSONException Thrown if there is an errors while parsing the string
	 */
	public static JSONObject toJSONObject(String string) throws JSONException {
		return toJSONObject(string, false);
	}


	/**
	 * 转换XML为JSONObject
	 * 转换过程中一些信息可能会丢失,JSON中无法区分节点和属性,相同的节点将被处理为JSONArray。
	 * Content text may be placed in a "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]&gt;</code> are ignored.
	 * All values are converted as strings, for 1, 01, 29.0 will not be coerced to numbers but will instead be the exact value as seen in the XML document.
	 *
	 * @param string      The source string.
	 * @param keepStrings If true, then values will not be coerced into boolean or numeric values and will instead be left as strings
	 * @return A JSONObject containing the structured data from the XML string.
	 * @throws JSONException Thrown if there is an errors while parsing the string
	 */
	public static JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
		return toJSONObject(new JSONObject(), string, keepStrings);
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值