如何解决java.lang.OutOfMemoryError: Java heap space error
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23359868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve java.lang.OutOfMemoryError: Java heap space error
提问by Suresh Ram
I am working in real time project.
我正在实时项目中工作。
Working: I am getting data from twitter and moving the output to cassandra database and then using Testrestfull webservice i am getting the output in json format. And then finally outputs are displayed in wep page. i am using glass fish server to connect web service and web page. it is an real time project so i am refreshing my page in every 5 second.
工作:我从 twitter 获取数据并将输出移动到 cassandra 数据库,然后使用 Testrestfull webservice 我以 json 格式获取输出。然后最后输出显示在 web 页面中。我正在使用玻璃鱼服务器连接网络服务和网页。这是一个实时项目,所以我每 5 秒刷新一次我的页面。
First few seconds i am not getting any error it works fine after 5 to 10 minutes i am getting this error in glass fish server.
前几秒钟我没有收到任何错误它在 5 到 10 分钟后工作正常我在玻璃鱼服务器中收到此错误。
Error: WARNING: StandardWrapperValve[genric.ApplicationConfig]: Servlet.service() for servlet genric.ApplicationConfig threw exception java.lang.OutOfMemoryError: Java heap space
错误:警告:StandardWrapperValve[genric.ApplicationConfig]:servlet genric.ApplicationConfig 的 Servlet.service() 抛出异常 java.lang.OutOfMemoryError:Java 堆空间
I dont know why i am getting this error and how to solve this. Can any one help me.
我不知道为什么会出现此错误以及如何解决此问题。谁能帮我。
This is my webservice code:
这是我的网络服务代码:
public String gettweets(String st)
{
cluster=Cluster.builder().addContactPoint("localhost").build();
session=cluster.connect("space");
String query ="select * from tweet_count where createdtime='"+st+"' allow filtering;";
ResultSet result = session.execute(query);
String text = "[";
for(Row r : result){
System.out.println(r.getString("tag_name"));
text+="{\""+"x"+"\":\""+r.getString("tag_name")+"\",\""+"y"+"\":\""+r.getInt("count")+"\"},";
}
text=text.substring(0,text.length()-1);
text+="]";
return text;
}
This is my web page code:
这是我的网页代码:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body onload="load();grid();marqueeload();" style="background-color:white">
<pre style="height:25px;background-color:black;">
<div id = "title" style = "text-align:center;font-size:20px;width:100%;"><span>REAL TIME TWITTER TRENDS</span></div>
</pre>
<div><table>
<tr><td><lable>Select Date<lable></td><td> : </td><td><input id="datepicker" type="text" /></td></tr></table>
</div>
<div style = "width:100%">
<div id="container2" style = "width:670px;height:300px;float:left;background-color:white;border: 1px solid green;">
<div style = "text-align:center;font-size:20px;"><span1>CHART VIEW<span1></div>
<div id="container1"></div>
</div>
<div id="container3" style = "width:670px;overflow-y:auto;height:300px;float:right;background-color:white;border: 1px solid green;">
<div style = "text-align:center;font-size:20px;"><span1>GRID VIEW<span1></div>
<div id="Grid"></div>
</div>
</div>
<div style = "width:100%;border: 1px solid green;height:300px;float:left;background-color:white;">
<div style = "text-align:center;font-size:20px;"><span1>Trending tweets<span1></div>
<marquee id = "marqueeid" direction="up" height="200" scrollAmount=2 scrollDelay=130 class="sidelink" onMouseDown="this.stop()" onMouseOver="this.stop()" onMouseMove="this.stop()" onMouseOut="this.start()" vspace="10" >
</marquee>
</div>
<script type="text/javascript" language="javascript">
function marqueeload()
{
var str;
$.get("http://localhost:8080/WebApplication1/webresources/generic", function(str)
{
str=str.slice(12, str.length-14);
var div=document.getElementById('marqueeid');
div.innerHTML=str;
});
}
$(function () {
$("#datepicker").ejDatePicker({
select: "onSelected"
});
$("#datepicker").ejDatePicker({ enabled: true });
});
function onSelected(args) {
var str = args.value;
var newstr = str.split("/",3);
if(newstr[0].length == 1){
newstr[0] = '0'+newstr[0];
}
if(newstr[1].length == 1){
newstr[1] = '0'+newstr[1];
}
var dat = newstr[2]+'-'+newstr[0]+'-'+newstr[1];
window.datetweet = dat;
$("#container1").ejChart("destroy");
grid();
load();
}
function load() {
var str;
$.get("http://localhost:8080/WebApplication1/webresources/time/"+window.datetweet, function(str)
{
str=str.slice(12, str.length-14);
var data=JSON.parse(str);
$("#container1").ejChart(
{
primaryXAxis:
{
title: { text: 'Tagname' },
labelRotation: 45
},
primaryYAxis:
{
range: { min: 0, max: 1000, interval: 100 },
title: { text: 'Count' }
},
commonSeriesOptions: {
type: 'column', animation: true,
tooltipFormat: "#point.x# : #point.y#"
},
series: [
{
points: data,
}
],
load:"loadTheme",
showTooltip: true,
needResize:true,
size: { height: 300 },
legend: { visible: false, position: 'top' }
});
});
}
$(document).ready(function()
{
$("#Grid").ejGrid({
dataSource: [],
allowPaging: true,
allowSorting: true,
columns: [
{ field: "x", headerText: "Trend Name", textAlign: ej.textAlign.Right, width: 10 },
{ field: "y", headerText: "Count", textAlign: ej.textAlign.Right, width: 10 }
]
});
});
function grid()
{
var str;
var url="http://localhost:8080/WebApplication1/webresources/time/"+window.datetweet;
$.get(url, function(str)
{
str=str.slice(12, str.length-14);
var obj=JSON.parse(str);
var instance = $("#Grid").ejGrid("instance");
instance._dataManager = new ej.DataManager(obj);
$("#Grid").ejGrid("model.dataSource", instance._dataManager);
});
}
window.setInterval(function() { load(); grid(); marqueeload(); }, 10000);
</script>
</body>
</html>
采纳答案by Suresh Ram
Answer:
回答:
Properly close the session object and cluster object
正确关闭会话对象和集群对象
eg :
例如:
session.shutdown();
cluster.shutdown();
Thanks to @ceiling gecko
感谢@ceiling Gecko
回答by Lavekush Agrawal
An OOM or OOME (OutOfMemoryError) simply means that the JVM ran out of memory. When this occurs, you basically have 2 choices:
OOM 或 OOME (OutOfMemoryError) 仅表示 JVM 内存不足。发生这种情况时,您基本上有两种选择:
- Allow the JVM to use more memory using the -Xmx VM argument. For instance, to allow the JVM to use 1 GB (1024 MB) of memory:
- Improve/Fix the application so that it uses less memory.
- 使用 -Xmx VM 参数允许 JVM 使用更多内存。例如,要允许 JVM 使用 1 GB (1024 MB) 的内存:
- 改进/修复应用程序,使其使用更少的内存。
For moer info please go through this link
有关更多信息,请访问此链接
http://www.mkyong.com/eclipse/eclipse-java-lang-outofmemoryerror-java-heap-space/
http://www.mkyong.com/eclipse/eclipse-java-lang-outofmemoryerror-java-heap-space/
回答by Azee
- Try to configure your JVM to use more memory as shown before (-Xms750m -Xmx2048m -XX:MaxPermSize=1024m ).
- Enable Garbage Collection logging (-Xloggc:/var/log/YOUR_APP/YOUR_APP-gc.log) and see how it behaves, how heap is growing. Probably you have a memory leak.
- If so, take a HeapDump, use YourKit to open it and look for objects that use the largest amount of memory. Try to figure out why and fix it.
- 尝试配置您的 JVM 以使用更多内存,如前所示 (-Xms750m -Xmx2048m -XX:MaxPermSize=1024m )。
- 启用垃圾收集日志记录 (-Xloggc:/var/log/YOUR_APP/YOUR_APP-gc.log) 并查看它的行为,堆如何增长。可能你有内存泄漏。
- 如果是这样,请使用 HeapDump,使用 YourKit 打开它并查找使用最大内存量的对象。尝试找出原因并修复它。