我正在解析一个 csv 文件,更具体地说是一个 POI 文件,使用 opencsv 并将信息读入 ArrayList。我需要将信息缓存在内存中,因此当用户点击按钮时,我会检查每个 POI 并查看它是否在地图视图的当前范围内。一些 POI 文件可以有 10K - 60K 行。在我的应用程序强制关闭之前,我可以读取大约 50K 行,所以我设置了 30K 的限制,以便为其他事情留下内存。我的问题是当我去加载另一个文件时,我清除()我 trimToSize()的 Arraylists,我尝试将 ArrayLists 声明为新的 ArrayLists,但 GC 从不从内存中释放旧数据。我可以清除()它们并向其中读入一个新文件,但有些东西不允许 GC 释放内存。我没有接受过编程、IT 或 CS 方面的培训。这是我用 Java / Android 开发或编写的第一个应用程序。我工作过,现在阅读和研究了大约 6 天,试图弄清楚为什么我会出现这种内存泄漏。任何帮助将不胜感激,任何关于如何优化我的代码的建议也将不胜感激,因为我是一个完整的菜鸟。此外,下面的代码仅显示了有关将文件读入内存的方法。您可以谷歌 opencsv 以查看有关其工作原理的文档,如果您需要查看其他任何内容,请告诉我,我会发布它。
提前致谢!
public class MainActivity extends MapActivity implements LocationListener {
private static MapView mapView;
int counter = 0;
private ArrayList<String> arrLat = new ArrayList<String>();
private ArrayList<String> arrLong = new ArrayList<String>();
private ArrayList<String> arrName = new ArrayList<String>();
private ArrayList<String> arrInfo = new ArrayList<String>();
private ArrayList<Boolean> arrCheck = new ArrayList<Boolean>();
private ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// main.xml contains a MapView
setContentView(R.layout.main);
//Gets file name from ListOfFiles Activity Class
Bundle extras = getIntent().getExtras();
if (extras != null) {
boolean callreadPOIFile = extras.getBoolean("callreadPOIFile");
if(callreadPOIFile) {
String filePath = extras.getString("filePath");
readPOIFileInThread(filePath);
}else{
// Show user alert box
}
}
}
public void readPOIFileInThread(String filePath) {
progressDialog = ProgressDialog.show(this, "", "LOADING:\n" + filePath + "\nPLEASE WAIT...");
final String finalFilePath = filePath;
new Thread(new Runnable(){
public void run(){
try{
readPOIFile(finalFilePath);
}catch(Exception e){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Exception, readPOIFileInThread", Toast.LENGTH_SHORT).show();
//progressDialog.dismiss();
}
});
}
progressDialog.dismiss();
}
}).start();
}
//Parse and load POI CSV File
public void readPOIFile(String filePath){
arrLat.clear();
arrLong.clear();
arrName.clear();
arrInfo.clear();
arrCheck.clear();
arrLat.trimToSize();
arrLong.trimToSize();
arrName.trimToSize();
arrInfo.trimToSize();
arrCheck.trimToSize();
//arrLat = null;
//arrLong = null;
//arrName = null;
//arrInfo = null;
//arrCheck = null;
//arrLat = new ArrayList<String>();
//arrLong = new ArrayList<String>();
//arrName = new ArrayList<String>();
//arrInfo = new ArrayList<String>();
//arrCheck = new ArrayList<Boolean>();
System.out.println(arrLat.isEmpty());
String lat = null;
String lng = null;
Double dLat;
Double dLng;
int lati;
int lngi;
String name = null;
String info = null;
CSVReader reader = null;
//System.out.println(filePath);
try {
reader = new CSVReader(new FileReader(filePath));
} catch (FileNotFoundException e) {
// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// set the message to display
alertbox.setMessage("There was an error reading file: " + filePath
+ "\n Please check the file format and try again.");
// add a neutral button to the alert box and assign a click listener
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
//Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_SHORT).show();
}
});
// show it
alertbox.show();
e.printStackTrace();
}
String [] nextLine = null;
int count = 0;
try {
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
//System.out.println(nextLine[0]+ "\n" + nextLine[1]+ "\n" + nextLine[2]+ "\n" + nextLine[3] + "\n");
try {
lng = nextLine[0];
} catch (Exception e) {
lng = Integer.toString(1);
}
try {
lat = nextLine[1];
} catch (Exception e) {
lat = Integer.toString(1);
}
try {
name = nextLine[2];
} catch (Exception e) {
name = "No Name...";
}
try {
info = nextLine[3];
} catch (Exception e) {
info = "No Info...";
}
//convert lat and long to double
try{
dLat = Double.parseDouble(lat);
dLng = Double.parseDouble(lng);
}catch(Exception e){
System.out.println("error converting lat long to Double at row: " + count);
break;
}
//convert lat lng to int
lati = (int)(dLat * 1E6);
lngi = (int)(dLng * 1E6);
//add line to ArrayLists
try{
arrLat.add(Integer.toString(lati));
arrLong.add(Integer.toString(lngi));
arrName.add(name);
arrInfo.add(info);
arrCheck.add(false);
}catch (Exception e){
runOnUiThread(new Runnable() {
public void run() {
//Toast.makeText(getApplicationContext(), "Error reading. Please check the file. ", Toast.LENGTH_SHORT).show();
System.out.println("Error reading file.");
}
});
}
count++;
if(count == 10000 || count == 20000){
final int showcount = count;
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), showcount + " POI's loaded",
Toast.LENGTH_LONG).show();
}
});
}
if(count == 30000)
break;
System.out.println(count);
}
final String toastFilePath = filePath;
final int toastcount = count;
runOnUiThread(new Runnable() {
public void run() {
if(toastcount > 0){
Toast.makeText(getApplicationContext(), "File: " + toastFilePath + " read... \n"
+ toastcount + " point(s) were loaded...",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "INVALIDE FILE!\nFile: " + toastFilePath + " read... \n"
+ toastcount + " points.",
Toast.LENGTH_LONG).show();
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
固定的:
我终于找到了我的问题!在研究了活动生命周期后,我发现每次我去我的列表活动选择一个文件并缓存它时都会创建一个新实例,我正在创建我的 MainActivity 的一个新实例。我在清单中将 MainActivity 设置为 singleTop 模式,并将一些代码移至 onNewIntent() 方法,一切都很好。我的应用程序现在运行良好!