loadrunner:java.lang.OutOfMemoryError: Java heap space 问题解决
问题原因:
一次测试需要想服务器中传递json串。
串内容为:
需要随机生成pid的个数和cid的个数。于是使用了一个简单的双层for循环来实现json串。格式如下:
for(i=0;i<Random;i++)
{
m_id...
prop...
vColor...
m_id...
prop...
for(j=0;j<Random;j++)
{
"c_id"..
}
}
然后将生成的字符串拼装,传递给一个变量,传递到被测程序接口中。(很简单吧。)
在执行过程中并发2000用户,不设置集合点,不持续,速度处理很快,貌似脚本没有问题。感觉良好。
但是,性能测试,怎能不加集合点、不持续30分钟以上呢?
先做个简单的,100user+30M
结果在压测的过程中发现,刚执行没有问题,当执行5分钟以上时,LR报错,提示java.lang.OutOfMemoryError: Java heap space。
百度之。。。
修改脚本的环境变量设置
【run-time setting】——【javavm】
选择
use specified JDK 设置JDK路径
additional VM parameters 设置JDK内存:-Xmx256M -Xms256M
我的机器是由4G可用内存,设置过高会提示:
Notify: Error occurred during initialization of VM
继续:100user+30M
情况有所好转,但依然报错:java.lang.OutOfMemoryError: Java heap space。
看来设置是治标治本,必须从脚本中解决问题。(大家尝试下能从上面发现问题吗)
第二遍程序(LR脚本):
test2=a2+a3+a4+a5+a6+test1.substring(0,test1.length()-2)+a8;
test2.append(a2).append(a3).append(a4).append(a5).append(a6).append(test1.substring(0, test1.length()-2)).append(a8);
它们的区别在哪里。
当然调整脚本后继续100user+30M。
可惜过程依然报错:java.lang.OutOfMemoryError: Java heap space。
为什么?透过现象看本质。
我被折磨的3个小时,让脑袋放空,然后回来看这个代码。
第三遍程序(LR脚本):
问题解决。
问题原因:
一次测试需要想服务器中传递json串。
串内容为:
{"m_id_1008":
<span style="white-space:pre"> </span>{"prop_1":
<span style="white-space:pre"> </span>{"vColor":"#bdb76b","pId":"183","pName":"颜色","vName":"深卡其布色","vPicture":"","vDefault":"","vId":"1008"},
"m_id":"1008",
"prop_2":[
{"c_id":"1121","property":{"orgPrice":"2","curPrice":"0.6","shelf":"","activeProductCount":"9990004","sellerClassNum":""},"pId":"182","pName":"身高","vName":"110cm","vPicture":"","vDefault":"","vId":"1121"},
{"c_id":"1122","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"9990000","sellerClassNum":""},"pId":"182","pName":"身高","vName":"120cm","vPicture":"","vDefault":"","vId":"1122"},
{"c_id":"1123","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"19990000","sellerClassNum":""},"pId":"182","pName":"身高","vName":"130cm","vPicture":"","vDefault":"","vId":"1123"},
{"c_id":"1124","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"9990009","sellerClassNum":""},"pId":"182","pName":"身高","vName":"140cm","vPicture":"","vDefault":"","vId":"1124"},
{"c_id":"1125","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"9990000","sellerClassNum":""},"pId":"182","pName":"身高","vName":"150cm","vPicture":"","vDefault":"","vId":"1125"}
]},
"m_id_1014":
{"prop_1":
{"vColor":"#dda0dd","pId":"183","pName":"颜色","vName":"紫罗兰","vPicture":"","vDefault":"","vId":"1014"},
"m_id":"1014",
"prop_2":[
{"c_id":"1121","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"19990000","sellerClassNum":""},"pId":"182","pName":"身高","vName":"110cm","vPicture":"","vDefault":"","vId":"1121"},
{"c_id":"1122","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"9990008","sellerClassNum":""},"pId":"182","pName":"身高","vName":"120cm","vPicture":"","vDefault":"","vId":"1122"},
{"c_id":"1123","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"9990007","sellerClassNum":""},"pId":"182","pName":"身高","vName":"130cm","vPicture":"","vDefault":"","vId":"1123"},
{"c_id":"1124","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"9990000","sellerClassNum":""},"pId":"182","pName":"身高","vName":"140cm","vPicture":"","vDefault":"","vId":"1124"},
{"c_id":"1125","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"9990009","sellerClassNum":""},"pId":"182","pName":"身高","vName":"150cm","vPicture":"","vDefault":"","vId":"1125"}
]},
"m_id_1016":{
"prop_1":
{"vColor":"#ff0000","pId":"183","pName":"颜色","vName":"红色","vPicture":"","vDefault":"","vId":"1016"},
"m_id":"1016",
"prop_2":[
{"c_id":"1121","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"19990000","sellerClassNum":""},"pId":"182","pName":"身高","vName":"110cm","vPicture":"","vDefault":"","vId":"1121"},
{"c_id":"1122","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"9990007","sellerClassNum":""},"pId":"182","pName":"身高","vName":"120cm","vPicture":"","vDefault":"","vId":"1122"},
{"c_id":"1123","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"9990000","sellerClassNum":""},"pId":"182","pName":"身高","vName":"130cm","vPicture":"","vDefault":"","vId":"1123"},
{"c_id":"1124","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"9990009","sellerClassNum":""},"pId":"182","pName":"身高","vName":"140cm","vPicture":"","vDefault":"","vId":"1124"},
{"c_id":"1125","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"999000","sellerClassNum":""},"pId":"182","pName":"身高","vName":"150cm","vPicture":"","vDefault":"","vId":"1125"}
]},
"m_id_1017":{
"prop_1":
{"vColor":"#008000","pId":"183","pName":"颜色","vName":"绿色","vPicture":"","vDefault":"","vId":"1017"},
"m_id":"1017",
"prop_2":[
<span style="white-space:pre"> </span>{"c_id":"1121","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"1999000","sellerClassNum":""},"pId":"182","pName":"身高","vName":"110cm","vPicture":"","vDefault":"","vId":"1121"},
{"c_id":"1122","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"999000","sellerClassNum":""},"pId":"182","pName":"身高","vName":"120cm","vPicture":"","vDefault":"","vId":"1122"},
{"c_id":"1123","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"9999000","sellerClassNum":""},"pId":"182","pName":"身高","vName":"130cm","vPicture":"","vDefault":"","vId":"1123"},
{"c_id":"1124","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"9999000","sellerClassNum":""},"pId":"182","pName":"身高","vName":"140cm","vPicture":"","vDefault":"","vId":"1124"},
{"c_id":"1125","property":{"orgPrice":"1","curPrice":"0.6","shelf":"","activeProductCount":"999000","sellerClassNum":""},"pId":"182","pName":"身高","vName":"150cm","vPicture":"","vDefault":"","vId":"1125"}
]}
}
需要随机生成pid的个数和cid的个数。于是使用了一个简单的双层for循环来实现json串。格式如下:
for(i=0;i<Random;i++)
{
m_id...
prop...
vColor...
m_id...
prop...
for(j=0;j<Random;j++)
{
"c_id"..
}
}
然后将生成的字符串拼装,传递给一个变量,传递到被测程序接口中。(很简单吧。)
第一遍程序(LR脚本):
String test="",test1="",test2="",test3="";
String a1,a2,a3,a4,a5,a6,a7,a8,a9;
String new_s;
int i,j;
a1="{ ";
// a2=" \"m_id_100"+i+"\": ";
a3=" {\"prop_1\": ";
// a4=" {\"vColor\":\"#bdb76b\",\"pId\":\"183\",\"pName\":\"颜色\",\"vName\":\"深卡其布色\",\"vPicture\":\"\",\"vDefault\":\"\",\"vId\":\"100"+i+"\"}, ";
// a5=" \"m_id\": 100"+i+", " ;
a6=" \"prop_2\": [ " ;
// a7=" {\"c_id\":\"112"+j+"\",\"property\":{\"orgPrice\":\"80\",\"curPrice\":\"40\",\"shelf\":\"\",\"activeProductCount\":\"9990004\",\"sellerClassNum\":\"\"},\"pId\":\"182\",\"pName\":\"身高\",\"vName\":\"110cm\",\"vPicture\":\"\",\"vDefault\":\"\",\"vId\":\"112"+j+"\"}, ";
a8="]},";
a9="}";
for(i=0;i<Integer.parseInt("<INum>");i++)
// for(i=0;i<1;i++)
{
a2=" \"m_id_100"+i+"\": ";
a4=" {\"vColor\":\"#bdb76b\",\"pId\":\"183\",\"pName\":\"颜色\",\"vName\":\"深卡其布色\",\"vPicture\":\"\",\"vDefault\":\"\",\"vId\":\"100"+i+"\"}, ";
a5=" \"m_id\": 100"+i+", " ;
test1="";
for(j=0;j<Integer.parseInt("<JNum>");j++)
// for(j=0;j<2;j++)
{
a7=" {\"c_id\":\"112"+j+"\",\"property\":{\"orgPrice\":\"<OrgPrice>\",\"curPrice\":\""+Integer.toString(Integer.parseInt("<OrgPrice>")/2)+"\",\"shelf\":\"\",\"activeProductCount\":\"9990004\",\"sellerClassNum\":\"\"},\"pId\":\"182\",\"pName\":\"身高\",\"vName\":\"110cm\",\"vPicture\":\"\",\"vDefault\":\"\",\"vId\":\"112"+j+"\"}, ";
test1=test1+a7;
}
test2=a2+a3+a4+a5+a6+test1.substring(0,test1.length()-2)+a8;
test3=test3+test2;
}
// lr.error_message(">>>>>"+test3);
test=a1+test3.substring(0,test3.length()-1)+a9;
new_s=test;
// lr.error_message(">>>>>"+new_s);
成绩斐然,脚本实现了需要的功能。
在执行过程中并发2000用户,不设置集合点,不持续,速度处理很快,貌似脚本没有问题。感觉良好。
但是,性能测试,怎能不加集合点、不持续30分钟以上呢?
先做个简单的,100user+30M
结果在压测的过程中发现,刚执行没有问题,当执行5分钟以上时,LR报错,提示java.lang.OutOfMemoryError: Java heap space。
百度之。。。
修改脚本的环境变量设置
【run-time setting】——【javavm】
选择
use specified JDK 设置JDK路径
additional VM parameters 设置JDK内存:-Xmx256M -Xms256M
我的机器是由4G可用内存,设置过高会提示:
Notify: Error occurred during initialization of VM
继续:100user+30M
情况有所好转,但依然报错:java.lang.OutOfMemoryError: Java heap space。
看来设置是治标治本,必须从脚本中解决问题。(大家尝试下能从上面发现问题吗)
第二遍程序(LR脚本):
int i,j;
StringBuilder a1 = new StringBuilder();
StringBuilder a2 = new StringBuilder();
StringBuilder a3 = new StringBuilder();
StringBuilder a4 = new StringBuilder();
StringBuilder a5 = new StringBuilder();
StringBuilder a6 = new StringBuilder();
StringBuilder a7 = new StringBuilder();
StringBuilder a8 = new StringBuilder();
StringBuilder a9 = new StringBuilder();
StringBuilder test1 = new StringBuilder();
StringBuilder test2 = new StringBuilder();
String new_s;
a1.append("{ ");
// a2=" \"m_id_100"+i+"\": ";
a3.append(" {\"prop_1\": ");
// a4=" {\"vColor\":\"#bdb76b\",\"pId\":\"183\",\"pName\":\"颜色\",\"vName\":\"深卡其布色\",\"vPicture\":\"\",\"vDefault\":\"\",\"vId\":\"100"+i+"\"}, ";
// a5=" \"m_id\": 100"+i+", " ;
a6.append(" \"prop_2\": [ " );
// a7=" {\"c_id\":\"112"+j+"\",\"property\":{\"orgPrice\":\"80\",\"curPrice\":\"40\",\"shelf\":\"\",\"activeProductCount\":\"9990004\",\"sellerClassNum\":\"\"},\"pId\":\"182\",\"pName\":\"身高\",\"vName\":\"110cm\",\"vPicture\":\"\",\"vDefault\":\"\",\"vId\":\"112"+j+"\"}, ";
a8.append("]},");
a9.append("}");
for(i=0;i<Integer.parseInt("<INum>");i++)
{
a2 =new StringBuilder();
a2.append(" \"m_id_100").append(i).append("\": ");
a4 =new StringBuilder();
a4.append(" {\"vColor\":\"#bdb76b\",\"pId\":\"183\",\"pName\":\"颜色\",\"vName\":\"深卡布色\",\"vPicture\":\"\",\"vDefault\":\"\",\"vId\":\"100").append(i).append("\"}, ");
a5 =new StringBuilder();
a5.append(" \"m_id\": 100").append(i).append(", ");
test1 =new StringBuilder();
for(j=0;j<Integer.parseInt("<JNum>");j++)
{
a7 =new StringBuilder();
a7.append(" {\"c_id\":\"112").append(j).append("\",\"property\":{\"orgPrice\":\"<OrgPrice>\",\"curPrice\":\"").append(Integer.toString(Integer.parseInt("<OrgPrice>")/2)).append("\",\"shelf\":\"\",\"activeProductCount\":\"9990004\",\"sellerClassNum\":\"\"},\"pId\":\"182\",\"pName\":\"身高\",\"vName\":\"110cm\",\"vPicture\":\"\",\"vDefault\":\"\",\"vId\":\"112").append(j).append("\"}, ");
}
test1.append(a7);
test2.append(a2).append(a3).append(a4).append(a5).append(a6).append(test1.substring(0, test1.length()-2)).append(a8);
}
//lr.error_message("test2>>>>>"+test2);
new_s=a1.append(test2.substring(0,test2.length()-1)).append(a9).toString();
// lr.error_message("new_s>>>>>"+new_s);
不知道是否有同学能够理解使用append所能够解决的问题。
test2=a2+a3+a4+a5+a6+test1.substring(0,test1.length()-2)+a8;
test2.append(a2).append(a3).append(a4).append(a5).append(a6).append(test1.substring(0, test1.length()-2)).append(a8);
它们的区别在哪里。
当然调整脚本后继续100user+30M。
可惜过程依然报错:java.lang.OutOfMemoryError: Java heap space。
为什么?透过现象看本质。
我被折磨的3个小时,让脑袋放空,然后回来看这个代码。
第三遍程序(LR脚本):
int i,j;
StringBuilder a1 = new StringBuilder();
StringBuilder a2 = new StringBuilder();
StringBuilder a3 = new StringBuilder();
StringBuilder a4 = new StringBuilder();
StringBuilder a5 = new StringBuilder();
StringBuilder a6 = new StringBuilder();
StringBuilder a7 = new StringBuilder();
StringBuilder a8 = new StringBuilder();
StringBuilder a9 = new StringBuilder();
StringBuilder test1 = new StringBuilder();
StringBuilder test2 = new StringBuilder();
String new_s;
a1.append("{ ");
// a2=" \"m_id_100"+i+"\": ";
a3.append(" {\"prop_1\": ");
// a4=" {\"vColor\":\"#bdb76b\",\"pId\":\"183\",\"pName\":\"颜色\",\"vName\":\"深卡其布色\",\"vPicture\":\"\",\"vDefault\":\"\",\"vId\":\"100"+i+"\"}, ";
// a5=" \"m_id\": 100"+i+", " ;
a6.append(" \"prop_2\": [ " );
// a7=" {\"c_id\":\"112"+j+"\",\"property\":{\"orgPrice\":\"80\",\"curPrice\":\"40\",\"shelf\":\"\",\"activeProductCount\":\"9990004\",\"sellerClassNum\":\"\"},\"pId\":\"182\",\"pName\":\"身高\",\"vName\":\"110cm\",\"vPicture\":\"\",\"vDefault\":\"\",\"vId\":\"112"+j+"\"}, ";
a8.append("]},");
a9.append("}");
for(i=0;i<Integer.parseInt("<INum>");i++)
{
a2 =new StringBuilder();
a2.append(" \"m_id_100").append(i).append("\": ");
a4 =new StringBuilder();
a4.append(" {\"vColor\":\"#bdb76b\",\"pId\":\"183\",\"pName\":\"颜色\",\"vName\":\"深卡布色\",\"vPicture\":\"\",\"vDefault\":\"\",\"vId\":\"100").append(i).append("\"}, ");
a5 =new StringBuilder();
a5.append(" \"m_id\": 100").append(i).append(", ");
test1 =new StringBuilder();
for(j=0;j<Integer.parseInt("<JNum>");j++)
{
a7 =new StringBuilder();
a7.append(" {\"c_id\":\"112").append(j).append("\",\"property\":{\"orgPrice\":\"<OrgPrice>\",\"curPrice\":\"").append(Integer.toString(Integer.parseInt("<OrgPrice>")/2)).append("\",\"shelf\":\"\",\"activeProductCount\":\"9990004\",\"sellerClassNum\":\"\"},\"pId\":\"182\",\"pName\":\"身高\",\"vName\":\"110cm\",\"vPicture\":\"\",\"vDefault\":\"\",\"vId\":\"112").append(j).append("\"}, ");
}
test1.append(a7);
test2.append(a2).append(a3).append(a4).append(a5).append(a6).append(test1.substring(0, test1.length()-2)).append(a8);
}
//lr.error_message("test2>>>>>"+test2);
new_s=a1.append(test2.substring(0,test2.length()-1)).append(a9).toString();
// lr.error_message("new_s>>>>>"+new_s);
lr.start_transaction("新建商品");
.....
lr.end_transaction("新建商品", lr.AUTO);
a1 = null;
a2 = null;
a3 = null;
a4 = null;
a5 = null;
a6 = null;
a7 = null;
a8 = null;
a9 = null;
test1 = null;
test2 = null;
new_s = null;
问题解决。