[Eclipse] [HOWTO] Solve java.lang.OutOfMemoryError: PermGen space

Recently (since I have been using Eclipse for PHP developers) I am getting an error while executing Eclipse Helios and I try to commit a ‘non-simple’ task (for the IDE), like saving a project, cutting multiple code lines, auto-complete function, and so on:

  1. Foreword. OS and configuration
  2. Problem description
  3. Solution
1. Foreword: OS and configuration
  • Windows 7 Professional 64bits.
  • Eclipse Helios for PHP developers x64.
  • Oracle JDK 7u4.

2. Problem description: you might see below Eclipse’s output when the JVM crashes:

!SESSION 2012-04-28 18:15:40.832 -----------------------------------------------
eclipse.buildId=I20100608-0911
java.version=1.7.0_04
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=es_ES
Framework arguments: -product org.eclipse.epp.package.php.product
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.php.product
!ENTRY org.eclipse.core.resources 2 10035 2012-04-28 18:16:06.184
!MESSAGE The workspace exited with unsaved changes in the previous session; refreshing workspace to recover changes.


!SESSION Tue May 01 13:36:02 CEST 2012 -----------------------------------------
!ENTRY org.eclipse.equinox.launcher 4 0 2012-05-01 13:36:02.532
!MESSAGE Exception launching the Eclipse Platform:
!STACK
java.lang.OutOfMemoryError: PermGen space

While on the first paragraph it is shown the information about the Eclipse launching (OS, Eclipse version, Java version…) on the second paragraph it is shown the error information:

java.lang.OutOfMemoryError: PermGen space

Which means, as stated here (which redirects to here):

PermGen is the permanent generation of objects in the VM (Class names, internalized strings, objects that will never get garbage-collected)

3. Solution.

Looking up through the Internet about this error, I found (again on the Eclipse wiki) that it is necessary to increase the amount of memory that the JVM is authorized to use. It can be achieved by changing the launching configuration file (eclipse.ini), enhancing the XXMaxPermSize and Xmx parameters (by default to 256m and 384m respectively) as shown below:

– Complete eclipse.ini content:


-startup
plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.0.v20100503
-product
org.eclipse.epp.package.php.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
512M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
512m
--launcher.defaultAction
openFile
-vm
C:\Program Files\Java\jdk1.7.0_04\bin
-vmargs
-XX:MaxPermSize=512m
-Dosgi.requiredJavaVersion=1.5
-Xms40m
-Xmx1024m

Edit: It is important to add the “-XX:MaxPermSize=256m” line after “-vmargs” (in my case, I decided to put “-XX:MaxPermSize=512m”.

[HOWTO] Use AJAX to send a file through a form

As you know, AJAX is used to asynchronously transfer information from the client to the server without refreshing the web page. You normally use it as follows:

1) Client side HTML form code:

<div id="formulary">
<form name="example" method = "POST" enctype="multipart/form-data" onsubmit="send_form()">
<input type="text" name="param1">
<input type="text" name="param2">
<input type="submit" value="Cargar archivo" />
</form>
</div>

2) Client side AJAX script (Javascript):

<script>
function send_form(){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4){
if (xmlhttp.status==200 || window.location.href.indexOf("http")==-1){
document.getElementById("divresponse").innerHTML=xmlhttp.responseText;
}
else{
alert("An error has occured making the request");
}
}
}
//parameters for the POST request
var parameter1 =encodeURIComponent(document.getElementById("parameter1").value);
var usuariovalue=encodeURIComponent(document.getElementById("parameter2").value);
var parameters="parameter1"="+ parameter1+"&amp; parameter2="+ parameter2;

//open asynchronous connection, indicating the sending method, the webpage receiver, and “true”, which means the proper asynchronous sending
xmlhttp.open(“POST”, “sendform.php”, true);
xmlhttp.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
xmlhttp.send(parameters);
}

2) Server side (PHP, for example):

<?php
echo 'parameters: ' . $_POST['parameter1'] . $_POST['parameter2']
?>

But the problem is that XMLHttpRequest was not thought to send a file within. There are different implementations to solve it, the most extended one is shown below. It consists on reloading the page into an invisible , sending the output from the form into this :

1)Client side (HTML):

<div id="message"></div>

2)Server side (PHP):

<?php
$tmp = $_FILES['fichero']['tmp_name'];
//check if the file has been sent
if(isset($_FILES['fichero'])){
//definitely file route (where you want the file to be stored into the server)
$name = "../folder/" . $_FILE['usuario'] . end(explode(".", $tmp)+1);
if(move_uploaded_file($tmp, $name)){
//the file has been succesfully received and stored
echo '
<script>
parent.document.getElementById("mensaje").innerHTML="file received";
new_f.type="file";
new_f.name="fichero";
new_f.value="";
new_f.id="fichero";
new_f.onchange=function(){
parent.document.getElementById("message").innerHTML="";
}
old_f.parentNode.replaceChild(new_f,old_f);
</script>';
exit;
}
}
?>