Java 带有HashMap和ArrayList的Android OutOfMemoryError
我正在使用opencsv解析csv文件,更具体地说是POI文件,并将信息读取到ArrayList中。我需要将信息缓存在内存中,这样当用户点击按钮时,我会检查每个POI,看看它是否在mapview的当前范围内。某些POI文件可以有10K-60K行。在我的应用程序force关闭之前,我可以读取大约50K行,所以我设置了30K的限制,以便为其他事情保留内存。我的问题是,当我加载另一个文件时,我清除了()我修剪过的ArrayList(),并且我尝试将ArrayList声明为新的ArrayList,但GC从未从内存中释放旧数据。我可以清除()它们并将新文件读入其中,但GC无法释放内存。我没有受过编程、IT或CS方面的培训。这是我用Java/Android开发或编写的第一个应用程序。我已经工作、阅读和学习了大约6天,现在我试图弄清楚为什么我会出现这种内存泄漏。任何帮助都将不胜感激,任何关于如何优化我的代码的建议也将不胜感激,因为我是一个彻头彻尾的noob。此外,下面的代码仅显示有关将文件读入内存的方法。你可以谷歌opencsv来查看它是如何工作的,如果你需要查看任何其他内容,请告诉我,我会发布它 提前谢谢Java 带有HashMap和ArrayList的Android OutOfMemoryError,java,android,arraylist,hashmap,out-of-memory,Java,Android,Arraylist,Hashmap,Out Of Memory,我正在使用opencsv解析csv文件,更具体地说是POI文件,并将信息读取到ArrayList中。我需要将信息缓存在内存中,这样当用户点击按钮时,我会检查每个POI,看看它是否在mapview的当前范围内。某些POI文件可以有10K-60K行。在我的应用程序force关闭之前,我可以读取大约50K行,所以我设置了30K的限制,以便为其他事情保留内存。我的问题是,当我加载另一个文件时,我清除了()我修剪过的ArrayList(),并且我尝试将ArrayList声明为新的ArrayList,但GC
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扩展MapActivity实现LocationListener{
私有静态地图视图;
int计数器=0;
private ArrayList arrLat=new ArrayList();
private ArrayList arrLong=new ArrayList();
private ArrayList arrName=new ArrayList();
private ArrayList arrInfo=new ArrayList();
private ArrayList arrCheck=new ArrayList();
私有进程对话;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//xml包含一个映射视图
setContentView(R.layout.main);
//从ListOfFiles活动类获取文件名
Bundle extras=getIntent().getExtras();
如果(附加值!=null){
布尔callreadPOIFile=extras.getBoolean(“callreadPOIFile”);
如果(callreadPOIFile){
String filePath=extras.getString(“filePath”);
readPOIFileInThread(文件路径);
}否则{
//显示用户警报框
}
}
}
public void readPOIFileInThread(字符串文件路径){
progressDialog=progressDialog.show(此“,”正在加载:\n“+文件路径+”\n请稍候…);
最终字符串finalFilePath=文件路径;
新线程(newrunnable()){
公开募捐{
试一试{
readPOIFile(最终文件路径);
}捕获(例外e){
runOnUiThread(新的Runnable(){
公开募捐{
Toast.makeText(getApplicationContext(),“Exception,readPOIFileInThread”,Toast.LENGTH_SHORT).show();
//progressDialog.disclose();
}
});
}
progressDialog.disclose();
}
}).start();
}
//解析并加载POI CSV文件
公共void readpoi文件(字符串文件路径){
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=newarraylist();
//arrLong=新的ArrayList();
//arrName=newarraylist();
//arrInfo=newarraylist();
//arrCheck=newarraylist();
System.out.println(arrLat.isEmpty());
字符串lat=null;
字符串lng=null;
双dLat;
双dLng;
国际拉丁语;
int-lngi;
字符串名称=null;
字符串信息=null;
CSVReader reader=null;
//System.out.println(文件路径);
试一试{
reader=new CSVReader(new FileReader(filePath));
}catch(filenotfounde异常){
//准备警报框
AlertDialog.Builder alertbox=新建AlertDialog.Builder(此);
//将消息设置为显示
setMessage(“读取文件时出错:”+filePath
+“\n请检查文件格式,然后重试。”);
//在警报框中添加一个中性按钮,并指定一个单击侦听器
alertbox.setNeutralButton(“确定”,新的DialogInterface.OnClickListener(){
//单击警报框上的侦听器
公共void onClick(对话框接口arg0,int arg1){
//按钮被点击了
//Toast.makeText(getApplicationContext(),“单击确定按钮”,Toast.LENGTH\u SHORT.show();
}
});
//表现出来
show();
e、 printStackTrace();
}
字符串[]nextLine=null;
整数计数=0;
试一试{
而((nextLine=reader.readNext())!=null){
//nextLine[]是该行中的值数组
//System.out.println(下一行[0]+“\n”+下一行[1]+“\n”+下一行[2]+“\n”+下一行[3]+“\n”);
试一试{
lng=nextLine[0];
}捕获(例外e){
lng=整数。toString(1);
}
试一试{
lat=下一行[1];
}捕获(例外e){
lat=整数。toString(1);
}
试一试{
name=nextLine[2];
}捕获(例外e){
name=“没有名字……”;
}
试一试{
info=nextLine[3];
}捕获(例外e){
info=“无信息…”;
}
//将lat和long转换为double
试一试{
dLat=Double.parseDouble(