u010579316
龙马0
采纳率25%
2017-03-07 03:33 阅读 1.3k
已采纳

java.lang.OutOfMemoryError: PermGen space

10
在服务器中添加一个SSH项目时,服务器运行一段时间后报错。
    错误如下:

三月 06, 2017 5:45:50 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 21784 ms
三月 06, 2017 5:46:35 下午 org.apache.jasper.compiler.TldLocationsCache tldScanJar
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
三月 06, 2017 6:41:24 下午 org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
信息: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [468] milliseconds.
三月 06, 2017 7:00:53 下午 org.quartz.core.JobRunShell run
严重: Job HtmlChannelJob.HtmlChannelJob8757c397-a53d-4c3d-84a0-929aac8e6db5 threw an unhandled Exception: 
java.lang.OutOfMemoryError: PermGen space

原服务器中有2个项目,把新加的项目war包直接放入webapps中。运行几个小时后出现了内存溢出的错误。

服务器tomcat配置如下![图片说明](https://img-ask.csdn.net/upload/201703/07/1488857464_691095.png)

新加的SSH项目有 excl导入导出功能,代码如下:
 package zcib.recruitment.action;
import zcib.recruitment.action.utils.UploadFile;
import zcib.recruitment.domain.Student;
import zcib.recruitment.service.StudentService;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;

public class AddStudentAction extends ActionSupport implements ServletRequestAware {

    private InputStream excelFile;
    private File uploadFile;
    private String uploadFileFileName;
    private StudentService studentService;
    private Student student;
    private String verifyCode;
    private HttpServletRequest request;

    // 进入页面查询数据
    public String listAll() {
        HttpServletRequest request = ServletActionContext.getRequest();
        List<Student> sList = studentService.findAll();
        request.setAttribute("list", sList);
        return "listAll";
    }
    // 导出Excel
    public String ExcelExport() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        String ids = request.getParameter("ids");
        List<Student> list = new ArrayList<Student>();
        String[] array = ids.split(",");
        int[] id = new int[array.length];
        for (int i = 0; i < id.length; i++) {
            Student student = studentService
                    .findById(Integer.valueOf(array[i]));
            list.add(student);
        }
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("学生信息");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("考号");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("身份证号");
        row.createCell(3).setCellValue("性别");
        row.createCell(4).setCellValue("备注");
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
        for (int i = 1; i <= list.size(); i++) {
            Student stu = list.get(i - 1);
            row = sheet.createRow(i);
            row.createCell(0).setCellValue(stu.getExamId());
            row.createCell(1).setCellValue(stu.getName());
            row.createCell(2).setCellValue(stu.getMemberId());
            row.createCell(3).setCellValue(stu.getSex());
            row.createCell(4).setCellValue(stu.getRemark());
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        workbook.write(baos);
        excelFile = new ByteArrayInputStream(baos.toByteArray());
        baos.close();
        return "excel";
    }

    // 导入Excel
    public String ExcelInto() throws Exception {
        String directory = "/file";
        String targetDirectory = ServletActionContext.getServletContext().getRealPath(directory);
        File target = UploadFile.Upload(uploadFile, uploadFileFileName,targetDirectory);
        List<Student> sList = new ArrayList<Student>();
        excelFile = new FileInputStream(target);
        Workbook wb = new HSSFWorkbook(excelFile);
        Sheet sheet = wb.getSheetAt(0);
        int rowNum = sheet.getLastRowNum() + 1;
        for (int i = 1; i < rowNum; i++) {
            Student student = new Student();
            Row row = sheet.getRow(i);
            int cellNum = row.getLastCellNum();
            for (int j = 0; j < cellNum; j++) {
                Cell cell = row.getCell(j);
                String cellValue = null;
                switch (cell.getCellType()) { // 判断excel单元格内容的格式,并对其进行转换,以便插入数据库
                case 0:
                    cellValue = String
                            .valueOf((int) cell.getNumericCellValue());
                    break;
                case 1:
                    cellValue = cell.getStringCellValue();
                    break;
                case 2:
                    cellValue = String
                            .valueOf((int) cell.getNumericCellValue());
                    break;
                case 3:
                    cellValue = cell.getStringCellValue();
                    break;
                case 4:
                    cellValue = cell.getStringCellValue();
                    break;
                }

                switch (j) {// 通过列数来判断对应插如的字段
                case 0:
                    student.setExamId(cellValue);
                    break;
                case 1:
                    student.setName(cellValue);
                    break;
                case 2:
                    student.setMemberId(cellValue);
                    break;
                case 3:
                    student.setSex(Integer.valueOf(cellValue));
                    break;
                case 4:
                    student.setRemark(cellValue);
                }
            }
            Student stu = studentService.findByExamId(student.getExamId());//判断数据库中是否拥有重复学生
            if(stu==null){
                sList.add(student);
            }
        }
        studentService.save(sList);
        return "success";
    }


    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public String getVerifyCode() {
        return verifyCode;
    }

    public void setVerifyCode(String verifyCode) {
        this.verifyCode = verifyCode;
    }

    public StudentService getStudentService() {
        return studentService;
    }

    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    public InputStream getExcelFile() {
        return excelFile;
    }

    public void setExcelFile(InputStream excelFile) {
        this.excelFile = excelFile;
    }

    public File getUploadFile() {
        return uploadFile;
    }

    public void setUploadFile(File uploadFile) {
        this.uploadFile = uploadFile;
    }

    public String getUploadFileFileName() {
        return uploadFileFileName;
    }

    public void setUploadFileFileName(String uploadFileFileName) {
        this.uploadFileFileName = uploadFileFileName;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }
}

struts配置如下:
  <action name="export_*" class="zcib.recruitment.action.AddStudentAction" method="{1}">
            <result name="listAll">/recruitmentInfoList.jsp</result>
            <result name="success" type="redirect">export_listAll.action</result>
            <result name="excel" type="stream">
                <param name="contentType">application/vnd.ms-excel</param>
                <param name="contentDisposition">attachment;filename="AllUsers.xls"</param>
                <param name="inputName">excelFile</param>
            </result>
            <result name="pdf" type="stream">
                <param name="contentType">application/pdf; charset=gb2312</param>
                <param name="inputName">excelFile</param>
                <param name="contentDisposition">attachment; filename="contract.pdf"</param>
                <param name="bufferSize">2048</param>
            </result>
            <result name="word" type="redirect">export_listAll.action</result>
        </action>

求大神帮忙看一下 内存溢出到底什么问题。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    qq_20597679 csdn_诸葛先生 2017-03-07 03:52

    加大内存。手动设置Heap size 修改TOMCAT_HOME/bin/catalina.sh 在“echo "Using CATALINA_BASE: $CATALINA_BASE"”上面加入以下行: JAVA_OPTS="-server -Xms800m -Xmx800m -XX:MaxNewSize=256m"
    实例,以下给出1G内存环境下java jvm 的参数设置参考: JAVA_OPTS="-server -Xms800m -Xmx800m -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128m -Djava.awt.headless=true "

    点赞 1 评论 复制链接分享
  • wl827455432 un-understander 2017-03-07 05:42

    这个是永久区内存不足造成的,需要上调PermSize 和MaxPermSize 两个属性,建议下一个visualVM看一下现在的虚拟机中永久区大小在分配合适的内存

    点赞 1 评论 复制链接分享
  • qq_21744201 qq_21744201 2017-03-07 03:44

    好像是HtmlChannelJob报的异常,把这个贴出来

    点赞 评论 复制链接分享
  • u010579316 龙马0 2017-03-07 05:54

    点赞 评论 复制链接分享

相关推荐