我有一个类KeywordCount,它标记给定的句子,并使用Apache OpenNLP POS标记器的maxent标记器对其进行标记。我首先将输出标记化,然后将其提供给标记器。我有一个问题,在jar完成任务后,RAM使用量高达165 MB。程序的其余部分只是进行一个DB调用并检查新任务。我已将泄漏隔离到这个班级。您可以安全地忽略Apache POI Excel代码。我想知道你们中是否有人能在代码中找到漏洞。
public class KeywordCount {
Task task;
String taskFolder = "";
List<String> listOfWords;
public KeywordCount(String taskFolder) {
this.taskFolder = taskFolder;
listOfWords = new ArrayList<String>();
}
public void tagText() throws Exception {
String xlsxOutput = taskFolder + File.separator + "results_pe.xlsx";
FileInputStream fis = new FileInputStream(new File(xlsxOutput));
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.createSheet("Keyword Count");
XSSFRow row = sheet.createRow(0);
Cell cell = row.createCell(0);
XSSFCellStyle csf = (XSSFCellStyle)wb.createCellStyle();
csf.setVerticalAlignment(CellStyle.VERTICAL_TOP);
csf.setBorderBottom(CellStyle.BORDER_THICK);
csf.setBorderRight(CellStyle.BORDER_THICK);
csf.setBorderTop(CellStyle.BORDER_THICK);
csf.setBorderLeft(CellStyle.BORDER_THICK);
Font fontf = wb.createFont();
fontf.setColor(IndexedColors.GREEN.getIndex());
fontf.setBoldweight(Font.BOLDWEIGHT_BOLD);
csf.setFont(fontf);
int rowNum = 0;
BufferedReader br = null;
InputStream modelIn = null;
POSModel model = null;
try {
modelIn = new FileInputStream("taggers" + File.separator + "en-pos-maxent.bin");
model = new POSModel(modelIn);
}
catch (IOException e) {
// Model loading failed, handle the error
e.printStackTrace();
}
finally {
if (modelIn != null) {
try {
modelIn.close();
}
catch (IOException e) {
}
}
}
File ftmp = new File(taskFolder + File.separator + "phrase_tmp.txt");
if(ftmp.exists()) {
br = new BufferedReader(new FileReader(ftmp));
POSTaggerME tagger = new POSTaggerME(model);
String line = "";
while((line = br.readLine()) != null) {
if (line.equals("")) {
break;
}
row = sheet.createRow(rowNum++);
if(line.startsWith("Match")) {
int index = line.indexOf(":");
line = line.substring(index + 1);
String[] sent = getTokens(line);
String[] tags = tagger.tag(sent);
for(int i = 0; i < tags.length; i++) {
if (tags[i].equals("NN") || tags[i].equals("NNP") || tags[i].equals("NNS") || tags[i].equals("NNPS")) {
listOfWords.add(sent[i].toLowerCase());
} else if (tags[i].equals("JJ") || tags[i].equals("JJR") || tags[i].equals("JJS")) {
listOfWords.add(sent[i].toLowerCase());
}
}
Map<String, Integer> treeMap = new TreeMap<String, Integer>();
for(String temp : listOfWords) {
Integer counter = treeMap.get(temp);
treeMap.put(temp, (counter == null) ? 1 : counter + 1);
}
listOfWords.clear();
sent = null;
tags = null;
if (treeMap != null || !treeMap.isEmpty()) {
for(Map.Entry<String, Integer> entry : treeMap.entrySet()) {
row = sheet.createRow(rowNum++);
cell = row.createCell(0);
cell.setCellValue(entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1));
XSSFCell cell1 = row.createCell(1);
cell1.setCellValue(entry.getValue());
}
treeMap.clear();
}
treeMap = null;
}
rowNum++;
}
br.close();
tagger = null;
model = null;
}
sheet.autoSizeColumn(0);
fis.close();
FileOutputStream fos = new FileOutputStream(new File(xlsxOutput));
wb.write(fos);
fos.close();
System.out.println("Finished writing XLSX file for Keyword Count!!");
}
public String[] getTokens(String match) throws Exception {
InputStream modelIn = new FileInputStream("taggers" + File.separator + "en-token.bin");
TokenizerModel model = null;
try {
model = new TokenizerModel(modelIn);
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (modelIn != null) {
try {
modelIn.close();
}
catch (IOException e) {
}
}
}
Tokenizer tokenizer = new TokenizerME(model);
String tokens[] = tokenizer.tokenize(match);
model = null;
return tokens;
}
}
我的系统在165MB后GC RAM。。。但当我上传到服务器时,GC不会执行,它会上升到480 MB(RAM使用率的49%)。
首先,堆使用率的增加并不是内存泄漏的证据。可能只是GC还没有运行。
话虽如此,任何人仅仅通过"观察"你的代码就能发现内存泄漏,这是值得怀疑的。解决此问题的正确方法是>>您<lt;以了解查找Java内存泄漏的技术,并且>>您<lt;然后使用相关工具(例如visualvm、jhat等)自己搜索问题。
以下是一些关于查找存储泄漏的参考资料:
-
带HotSpot VM的Java SE 6故障排除指南:内存泄漏故障排除。http://www.oracle.com/technetwork/java/javase/memleaks-137499.html-注1。
-
如何找到Java内存泄漏
注意1:这个链接很容易断开。如果是,请使用谷歌查找文章
我已将泄漏隔离到这个班级。您可以安全地忽略Apache POI Excel代码。
如果我们忽略Apache POI代码,那么潜在内存"泄漏"的唯一来源就是保留了单词列表(listOfWords
)。(调用clear()
将清空其内容,但保留后备数组,该数组的大小由最大列表大小决定。从内存占用的角度来看,最好用新的空列表替换该列表。)
但是,如果保留对KeywordCount
实例的引用,那么这只是一个"泄漏"。如果你这样做是因为你在使用这个实例,我根本不会称之为泄漏。