my dog learned polymorphism*
The moose likes Beginning Java and the fly likes java.lang.OutOfMemoryError heap space.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.lang.OutOfMemoryError heap space.." Watch "java.lang.OutOfMemoryError heap space.." New topic
Author

java.lang.OutOfMemoryError heap space..

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi

I wrote the following code in eclipse. It reads the URL from an InputStream and converts it into a String.



But when I try and run it in eclipse, I get the following error:

java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2882)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:515)
at java.lang.StringBuffer.append(StringBuffer.java:306)
at test.ConvertUrlToString.main(ConvertUrlToString.java:28)

I have already tried adding -Xmx1024M to VM arguments of the respective class before running it. No success. Any pointers would be appreciated.



IT WORKED. But I have another doubt now.. I simply changed the class code to:



i.e. I simply separated the declaration of the int numCharsRead and put the comparison inside the while braces instead of doing them before hand. Can someone explain what is happening in the background here? What actually happens when I say



Why did this work? Mathematically, isn't it same as saying:



If not, why?


~ Mansukh
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 866
    
    5
This is suspicious. What should stop this loop?



Here the variable assignment is reevaluted again and again, numCharsRead gets new values, so it is possible to stop the loop:
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Ivan Jozsef Balazs wrote:This is suspicious. What should stop this loop?



That's what Ivan. Just try copying the two different classes and try executing them. The former one won't and the latter will give something like:

*** BEGIN ***
<!doctype html><html itemscope="itemscope" itemtype="http://schema.org/WebPage"><head><meta itemprop="image" content="/images/google_favicon_128.png"><title>Google</title><script>(function(){
window.google={kEI:"oRUFUb-eGsG4rAf3moGgCg",getEI:function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||google.kEI},https:function(){return"https:"==window.location.protocol},kEXPI:"17259,39523,39976,4000116,4001569,4001959,4001975,4002001,4002208,4002378,4002436,4002524,4002562,4002700,4002826,4002858,4002928,4003035,4003215,4003225,4003316,4003318,4003335,4003340,4003371,4003518,4003523,4003526",kCSI:{e:"17259,39523,39976,4000116,4001569,4001959,4001975,4002001,4002208,4002378,4002436,4002524,4002562,4002700,4002826,4002858,4002928,4003035,4003215,4003225,4003316,4003318,4003335,4003340,4003371,4003518,4003523,4003526",ei:"oRUFUb-eGsG4rAf3moGgCg"},authuser:0,ml:function(){},kHL:"en",time:function(){return(new Date).getTime()},log:function(a,
b,c,i){var d=new Image,f=google.lc,e=google.li,g="";d.onerror=d.onload=d.onabort=function(){delete f[e]};f[e]=d;!c&&-1==b.search("&ei=")&&(g="&ei="+google.getEI(i));c=c||"/gen_204?atyp=i&ct="+a+"&cad="+b+g+"&zx="+google.time();a=/^http:/i;a.test(c)&&google.https()?(google.ml(Error("GLMM"),!1,{src:c}),delete f[e])d.src=c,google.li=e+1)},lc:[],li:0,Toolbelt:{},y:{},x:function(a,b){google.y[a.id]=[a,b];return!1},load:function(a,b){google.x({id:"l"+a},function(){google.load(a,b)})}};
})();
(function(){var d=!1;google.sn="webhp";google.timers={};google.startTick=function(a,b){google.timers[a]={t:{start:google.time()},bfr:!!b}};google.tick=function(a,b,h){google.timers[a]||google.startTick(a);google.timers[a].t[b]=h||google.time()};google.startTick("load",!0);
try{}catch(e){}})();
var _gjwl=location;function _gjuc(){var a=_gjwl.href.indexOf("#");if(0<=a&&(a=_gjwl.href.substring(a),0<a.indexOf("&q=")||0<=a.indexOf("#q=")))if(a=a.substring(1),-1==a.indexOf("#")){for(var d=0;d<a.length;){var b=d;"&"==a.charAt(b)&&++b;var c=a.indexOf("&",b);-1==c&&(c=a.length);b=a.substring(b,c);if(0==b.indexOf("fp="))a=a.substring(0,d)+a.substring(c,a.length),c=d;else if("cad=h"==b)return 0;d=c}_gjwl.href="/search?"+a+"&cad=h";return 1}return 0}
function _gjp(){(!window._gjwl.hash||!window._gjuc())&&setTimeout(_gjp,500)};
window._gjp&&_gjp();</script><style>#gbar,#guser{font-size:13px;padding-top:1px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important}</style><style>.h{font-family:arial,sans-serif}body{font-family:arial,sans-serif}td{font-family:arial,sans-serif}a{font-family:arial,sans-serif}p{font-family:arial,sans-serif}body{margin:0;overflow-y:scroll}#gog{padding:3px 8px 0}.h{color:#36c}.q{color:#00c}.ts{border-collapse:collapse}td{line-height:.8em}.gac_m td{line-height:17px}form{margin-bottom:20px}.ts td{padding:0}em{font-weight:bold;font-style:normal}.lst{height:25px;width:496px;font:18px arial,sans-serif}.gsfi{font:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.ds{display:inline-box;display: inline-block;margin:3px 0 4px;margin-left:4px}input{font-family:inherit}body{background:#fff;color:black}a.gb1{color:#11c !important}a.gb2{color:#11c !important}a.gb3{color:#11c !important}a.gb4{color:#11c !important}.sblc{padding-top:5px}.lsbb{background:#eee;border:solid 1px;border-color:#ccc #999 #999 #ccc;height:30px}a{color:#11c;text-decoration:none}a:hover{text-decoration:underline}a:active{text-decoration:underline}.fl a{color:#36c}a:visited{color:#551a8b}a.gb1{text-decoration:underline}a.gb4{text-decoration:underline}a.gb3:hover{text-decoration:none}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11px}#ghead a.gb2:hover{color:#fff !important}.lsbb{display:block}.ftl{display:inline-block;margin:0 12px}.lsb{background:url(/images/srpr/nav_logo80.png) 0 -258px repeat-x;border:none;color:#000;cursor:pointer;height:30px;margin:0;outline:0;font:15px arial,sans-serif;vertical-align:top}#fll a{display:inline-block;margin:0 12px}.lsb:active{background:#ccc}.lst:focus{outline:none}#addlang a{padding:0 3px}</style><script></script> </head><body dir="ltr" bgcolor="#fff"><script>(function(){var src='/images/srpr/nav_logo80.png';var iesg=false;document.body.onload = function(){window.n && window.n();if (document.images){new Image().src=src;}
if (!iesg){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();}
}
})();</script><textarea id="csi" style="display:none"></textarea><div id="mngb"><div id=gbar><nobr><b class=gb1>Search</b> <a class=gb1 href="http://www.google.co.in/imghp?hl=en&tab=wi">Images</a> <a class=gb1 href="http://maps.google.co.in/maps?hl=en&tab=wl">Maps</a> <a class=gb1 href="https://play.google.com/?hl=en&tab=w8">Play</a> <a class=gb1 href="http://www.youtube.com/?gl=IN&tab=w1">YouTube</a> <a class=gb1 href="http://news.google.co.in/nwshp?hl=en&tab=wn">News</a> <a class=gb1 href="https://mail.google.com/mail/?tab=wm">Gmail</a> <a class=gb1 href="https://drive.google.com/?tab=wo">Drive</a> <a class=gb1 style="text-decoration:none" href="http://www.google.co.in/intl/en/options/"><u>More</u> »</a></nobr></div><div id=guser width=100%><nobr><span id=gbn class=gbi></span><span id=gbf class=gbf></span><span id=gbe></span><a href="http://www.google.co.in/history/optout?hl=en" class=gb4>Web History</a> | <a href="/preferences?hl=en" class=gb4>Settings</a> | <a target=_top id=gb_70 href="https://accounts.google.com/ServiceLogin?hl=en&continue=http://www.google.co.in/" class=gb4>Sign in</a></nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div></div><center><br clear="all" id="lgpd"><div id="lga"><div style="padding:28px 0 3px"><div dir="ltr" title="Google" align="left" id="hplogo" onload="window.lol&&lol()" style="height:110px;width:276px;background:url(/images/srpr/logo1w.png) no-repeat"><div nowrap="nowrap" style="color:#777;font-size:16px;font-weight:bold;position:relative;left:214px;top:70px">India</div></div></div><br></div><form action="/search" name="f"><table cellpadding="0" cellspacing="0"><tr valign="top"><td width="25%"> </td><td align="center" nowrap="nowrap"><input name="ie" value="ISO-8859-1" type="hidden"><input value="en-IN" name="hl" type="hidden"><input name="source" type="hidden" value="hp"><div class="ds" style="height:32px;margin:4px 0"><input autocomplete="off" class="lst" value="" title="Google Search" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top"></div><br style="line-height:0"><span class="ds"><span class="lsbb"><input class="lsb" value="Google Search" name="btnG" type="submit"></span></span><span class="ds"><span class="lsbb"><input class="lsb" value="I'm Feeling Lucky" name="btnI" type="submit" onclick="if(this.form.q.value)this.checked=1; else top.location='/doodles/'"></span></span></td><td class="fl sblc" align="left" nowrap="nowrap" width="25%"><a href="/advanced_search?hl=en-IN&authuser=0">Advanced search</a><a href="/language_tools?hl=en-IN&authuser=0">Language tools</a></td></tr></table><input type="hidden" id="gbv" name="gbv" value="1"></form><div id="gac_scont"></div><div style="font-size:83%;min-height:3.5em"><br><div id=als><font size=-1 id=addlang>Google.co.in offered in: <a href="http://www.google.co.in/setprefs?sig=0_4pI06gXdFvARxev4zt7286vmVK0%3D&hl=hi&source=homepage">Hindi</a> <a href="http://www.google.co.in/setprefs?sig=0_4pI06gXdFvARxev4zt7286vmVK0%3D&hl=bn&source=homepage">Bengali</a> <a href="http://www.google.co.in/setprefs?sig=0_4pI06gXdFvARxev4zt7286vmVK0%3D&hl=te&source=homepage">Telugu</a> <a href="http://www.google.co.in/setprefs?sig=0_4pI06gXdFvARxev4zt7286vmVK0%3D&hl=mr&source=homepage">Marathi</a> <a href="http://www.google.co.in/setprefs?sig=0_4pI06gXdFvARxev4zt7286vmVK0%3D&hl=ta&source=homepage">Tamil</a> <a href="http://www.google.co.in/setprefs?sig=0_4pI06gXdFvARxev4zt7286vmVK0%3D&hl=gu&source=homepage">Gujarati</a> <a href="http://www.google.co.in/setprefs?sig=0_4pI06gXdFvARxev4zt7286vmVK0%3D&hl=kn&source=homepage">Kannada</a> <a href="http://www.google.co.in/setprefs?sig=0_4pI06gXdFvARxev4zt7286vmVK0%3D&hl=ml&source=homepage">Malayalam</a> <a href="http://www.google.co.in/setprefs?sig=0_4pI06gXdFvARxev4zt7286vmVK0%3D&hl=pa&source=homepage">Punjabi</a></font><br><br></div></div><span id="footer"><div style="font-size:10pt"><div id="fll" style="margin:19px auto;text-align:center"><a href="/intl/en/ads/">Advertising Programs</a><a href="http://www.google.co.in/services/">Business Solutions</a><a href="https://plus.google.com/104205742743787718296" rel="publisher">+Google</a><a href="/intl/en/about.html">About Google</a><a href="http://www.google.co.in/setprefdomain?prefdom=US&sig=0_RSg4kiJJjwEpDWZ6LGtYUSKhFm0%3D" id="fehl">Google.com</a></div></div><p style="color:#767676;font-size:8pt">© 2012 - <a href="/intl/en/policies/">Privacy & Terms</a></p></span></center><div id=xjsd></div><div id=xjsi><script>if(google.y)google.y.first=[];(function(){var b;function c(a){window.setTimeout(function(){var d=document.createElement("script");d.src=a;document.getElementById("xjsd").appendChild(d)},0)}google.dljp=function(a){b=a;google.xjsi||(google.xjsu=a,c(b))};google.dlj=c;})();
if(!google.xjs){google.dstr=[];google.rein=[];window._=window._||{};window._._DumpException=function(e){throw e};if(google.timers&&google.timers.load.t){google.timers.load.t.xjsls=new Date().getTime();}google.dljp('/xjs/_/js/hp/sb_he,pcc/rt\x3dj/ver\x3dib0Fx6HfNdE.en_US./d\x3d1/sv\x3d1/rs\x3dAItRSTPs_I6l2vjvlHM5zh2mZbojMbgtiA');google.xjs=1;}google.pmc={sb:{"agen":false,"cgen":true,"client":"heirloom-hp","dh":true,"ds":"","eqch":true,"fl":true,"host":"google.co.in","jsonp":true,"msgs":{"lcky":"I\u0026#39;m Feeling Lucky","lml":"Learn more","oskt":"Input tools","psrc":"This search was removed from your \u003Ca href=\"/history\"\u003EWeb History\u003C/a\u003E","psrl":"Remove","sbit":"Search by image","srch":"Google Search"},"ovr":{"ms":1},"pq":"","qcpw":false,"scd":10,"sce":5,"stok":"fhqaVTexGrTj1qbB6ELEnKyIdOA"},hp:{},pcc:{}};google.y.first.push(function(){if(google.med){google.med('init');google.initHistory();google.med('history');}google.History&&google.History.initialize('/');google.hs&&google.hs.init&&google.hs.init()});if(google.j&&google.j.en&&google.j.xi){window.setTimeout(google.j.xi,0);}</script></div><script>(function(){var b,c,d,e;function g(a,f){a.removeEventListener?(a.removeEventListener("load",f,!1),a.removeEventListener("error",f,!1))a.detachEvent("onload",f),a.detachEvent("onerror",f))}function h(a){e=(new Date).getTime();++c;a=a||window.event;a=a.target||a.srcElement;g(a,h)}var i=document.getElementsByTagName("img");b=i.length;
for(var j=c=0,k;j<b;++j)k=i[j],k.complete||"string"!=typeof k.src||!k.src?++c:k.addEventListener?(k.addEventListener("load",h,!1),k.addEventListener("error",h,!1))k.attachEvent("onload",h),k.attachEvent("onerror",h));d=b-c;
function l(){if(google.timers.load.t){google.timers.load.t.ol=(new Date).getTime();google.timers.load.t.iml=e;google.kCSI.imc=c;google.kCSI.imn=b;google.kCSI.imp=d;void 0!==google.stt&&(google.kCSI.stt=google.stt);google.csiReport&&google.csiReport()}}window.addEventListener?window.addEventListener("load",l,!1):window.attachEvent&&window.attachEvent("onload",l);google.timers.load.t.prt=e=(new Date).getTime();})();
</script></body></html>
*** END ***

i.e. the whole URL as a String. Any clue what is going on here?

Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 866
    
    5
Once the value of numCharsRead is set, it does not change any longer, so this is an endless loop here:


Whereas here numCharsRead is reevaluated with every iteration, so it is possible for the loop to terminate.
It is indeed a typical construct going back to good old C times:

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Ivan Jozsef Balazs wrote:Once the value of numCharsRead is set, it does not change any longer, so this is an endless loop here:


Whereas here numCharsRead is reevaluated with every iteration, so it is possible for the loop to terminate.
It is indeed a typical construct going back to good old C times:



So the read method of the InputStreamReader class has to be explicitly invoked on the reader object every time. Why? Why does its value change every time it is evaluated? Is it because there is no surety how many bytes will the stream reader read and store into charArr at a time as charsets? Is that JVM dependent? Or does it depend on the URL we are trying to parse into a String?

P.S. Correct me if I am thinking on the wrong lines here.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 866
    
    5


Every time the while condition is checked, it includes an evaluation of the expression numCharsRead = reader.read(charArr), that is, charArr will be filled by reading from reader, and the number of the read bytes will be assigned to numCharsRead. If it is positive, the core of the loop will be executed, and the loop continues, otherwise, the loop will be broken. The other aspects, I hope, you understand.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Ivan Jozsef Balazs wrote:

Every time the while condition is checked, it includes an evaluation of the expression numCharsRead = reader.read(charArr), that is, charArr will be filled by reading from reader, and the number of the read bytes will be assigned to numCharsRead. If it is positive, the core of the loop will be executed, and the loop continues, otherwise, the loop will be broken. The other aspects, I hope, you understand.


I liked your explanation Ivan. Thanks a pile..
 
Don't get me started about those stupid light bulbs.
 
subject: java.lang.OutOfMemoryError heap space..
 
Similar Threads
Reg:Reading file with 100,0000 lines
How to parse xml inpustream
I/O client server excercise
How to parse xml inpustream?
Multithreading client socket throws Stream corrupted exception