我的应用程序168mb内存泄漏了 我试图了解泄漏的时间超过两天。 我添加了我认为泄漏的位置 ,谢谢你的帮助
感谢所有帮手。 [链接]到Img [1] *** [1]:http://i.stack.imgur.com/1WwCS.png
Log Cat:
I/art: Alloc partial concurrent mark sweep GC freed 651(39KB) AllocSpace objects, 0(0B) LOS objects, 9% free, 155MB/171MB, paused 506us total 12.495ms
I/art: Alloc concurrent mark sweep GC freed 2546(202KB) AllocSpace objects, 6(114MB) LOS objects, 28% free, 40MB/56MB, paused 725us total 49.030ms
I/art: Alloc partial concurrent mark sweep GC freed 541(47KB) AllocSpace objects, 12(7MB) LOS objects, 9% free, 157MB/173MB, paused 651us total 14.817ms
I/art: Alloc concurrent mark sweep GC freed 431(19KB) AllocSpace objects, 0(0B) LOS objects, 9% free, 157MB/173MB, paused 486us total 23.604ms
I/art: Alloc concurrent mark sweep GC freed 2265(177KB) AllocSpace objects, 6(114MB) LOS objects, 27% free, 42MB/58MB, paused 528us total 44.405ms
Eclipse内存分析器:
问题疑似1
One instance of "android.support.v7.widget.AppCompatImageView" loaded by "dalvik.system.PathClassLoader @ 0x12cd1540" occupies 51,841,080 (30.62%) bytes. The memory is accumulated in one instance of "byte[]" loaded by "<system class loader>".
关键字 字节[] android.support.v7.widget.AppCompatImageView dalvik.system.PathClassLoader @ 0x12cd1540
详细信息» 问题可疑2
One instance of "android.support.v7.widget.AppCompatImageView" loaded by "dalvik.system.PathClassLoader @ 0x12cd1540" occupies 47,187,000 (27.87%) bytes. The memory is accumulated in one instance of "byte[]" loaded by "<system class loader>".
关键字 字节[] android.support.v7.widget.AppCompatImageView dalvik.system.PathClassLoader @ 0x12cd1540
onItemClick
public class onItemClick extends AppCompatActivity implements View.OnTouchListener {
private String getImg1String, getImg2String, getImg3String, getImg4String, getImg5String;
private Dialog progressDialog;
private String GEThouseNumber, GETstreet, GETcity;
private int position;
private ImageView BIGimg, Image1IMG, Image2IMG, Image3IMG, Image4IMG, Image5IMG;
private int nowX = 0;
private int StopX = 0;
// final ImageView[] ImageViewName = new ImageView[]{Image1IMG, Image2IMG, Image3IMG, Image4IMG, Image5IMG};
private String[] StringUrlArray = new String[5];
private int counter = 0;
private Context context;
private MyApplication Application;
private int width;
private int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_on_item_click);
Toolbar toolbar = (Toolbar) findViewById(R.id.title_activity_on_item_click);
setSupportActionBar(toolbar);
ImageView customSearchImg = (ImageView) toolbar.findViewById(R.id.locationImg); // cast it
customSearchImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri IntentUri = Uri.parse("geo:0,0?q=" + GETcity + "," + GETstreet + " " + GEThouseNumber);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, IntentUri);
startActivity(mapIntent);
}
});
ImageView phoneImageView = (ImageView) toolbar.findViewById(R.id.phone);
phoneImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:04-696-5444 "));
startActivity(intent);
}
});
ImageView SendMailImageView = (ImageView) toolbar.findViewById(R.id.mail);
SendMailImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"nadlangolan@gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "התעניינות בנכס");
i.putExtra(Intent.EXTRA_TEXT, "");
try {
startActivity(Intent.createChooser(i, ""));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(onItemClick.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
});
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
context = this;
//showProgressBar(context.getResources().getString(R.string.Loading).toString());
//check if there is internet
if (AppStatus.getInstance(context).isOnline()) {
try {
onCreateInslaize();
} catch (Exception e) {
}
} else {
try {
dismissProgressBar();
buildDialog(getResources().getString(R.string.noInternet).toString());
finish();
} catch (Exception e) {
}
}
}
public void onCreateInslaize() {
try {
Application = ((MyApplication) context.getApplicationContext());
//showProgressBar(getResources().getString(R.string.Loading).toString());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
definitionView();
ProessDataBackground PData = new ProessDataBackground();
PData.execute();
handler.postDelayed(changeImage, 8000);
} catch (Exception e) {
buildDialog(getResources().getString(R.string.itemOnClickOnCreate).toString());
}
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(changeImage);
finish();
}
final Handler handler = new Handler();
Runnable changeImage = new Runnable() {
@Override
public void run() {
try {
if (counter + 1 >= StringUrlArray.length) {
counter = 0;
fadeOutAndHideImage(StringUrlArray[counter]);
} else {
counter++;
fadeOutAndHideImage(StringUrlArray[counter]);
}
handler.postDelayed(this, 8000);
} catch (Exception e) {
buildDialog(getResources().getString(R.string.galleryError).toString());
}
}
};
// define all the view items
public void definitionView() {
BIGimg = (ImageView) findViewById(R.id.itemcustom_BigIMG);
BIGimg.setOnTouchListener(this);
Image1IMG = (ImageView) findViewById(R.id.itemcustom_img1);
Image2IMG = (ImageView) findViewById(R.id.itemcustom_img2);
Image3IMG = (ImageView) findViewById(R.id.itemcustom_img3);
Image4IMG = (ImageView) findViewById(R.id.itemcustom_img4);
Image5IMG = (ImageView) findViewById(R.id.itemcustom_img5);
}
public void itemCDF(View view) {
try {
switch (view.getId()) {
case R.id.itemcustom_next:
if (counter + 1 == 5) {
fadeOutAndHideImage(StringUrlArray[0]);
counter = 0;
} else {
counter++;
fadeOutAndHideImage(StringUrlArray[counter]);
}
break;
case R.id.itemcustom_pre:
if (counter - 1 == -1) {
counter = 4;
fadeOutAndHideImage(StringUrlArray[counter]);
} else {
counter--;
fadeOutAndHideImage(StringUrlArray[counter]);
}
break;
case R.id.itemcustom_img1:
counter = 0;
fadeOutAndHideImage(StringUrlArray[counter]);
break;
case R.id.itemcustom_img2:
counter = 1;
fadeOutAndHideImage(StringUrlArray[counter]);
break;
case R.id.itemcustom_img3:
counter = 2;
fadeOutAndHideImage(StringUrlArray[counter]);
break;
case R.id.itemcustom_img4:
counter = 3;
fadeOutAndHideImage(StringUrlArray[counter]);
break;
case R.id.itemcustom_img5:
counter = 4;
fadeOutAndHideImage(StringUrlArray[counter]);
break;
}
} catch (Exception e) {
buildDialog(getResources().getString(R.string.onItemClickImageClick).toString());
}
}
class ProessDataBackground extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Bundle GetExtra = getIntent().getExtras();
if (GetExtra == null) {
return null;
} else {
try {
position = GetExtra.getInt("position");
GEThouseNumber = Application.getHouseNumberFromApp(position);
GETstreet = Application.getStreetFromApp(position);
GETcity = Application.getCityFromApp(position);
getImg1String = Application.getIMG1FromApp(position);
getImg2String = Application.getIMG2FromApp(position);
getImg3String = Application.getIMG3FromApp(position);
getImg4String = Application.getIMG4FromApp(position);
getImg5String = Application.getIMG5FromApp(position);
StringUrlArray = new String[]{getImg1String, getImg2String, getImg3String, getImg4String, getImg5String};
publishProgress();
} catch (Exception e) {
buildDialog(getResources().getString(R.string.getDataOnClick).toString());
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
TextView rooms = (TextView) findViewById(R.id.itemClick_roomsVal);
TextView price = (TextView) findViewById(R.id.itemClick_priceVal);
TextView floor = (TextView) findViewById(R.id.itemClick_FloorVal);
TextView houseNumber = (TextView) findViewById(R.id.onItem_numHouseVal);
TextView city = (TextView) findViewById(R.id.itemClick_cityVal);
TextView street = (TextView) findViewById(R.id.itemClick_streetVal);
TextView totalSize = (TextView) findViewById(R.id.itemClick_sizeHouseVal);
TextView buildingSize = (TextView) findViewById(R.id.itemClick_BuildingSize);
TextView type = (TextView) findViewById(R.id.onItem_typesVal);
TextView buildingSitu = (TextView) findViewById(R.id.itemClick_houseSituVal);
TextView description = (TextView) findViewById(R.id.itemClick_descriptionVal);
if (Application.getRoomsFromApp(position) % 1 == 0) {
int rm = (int)Application.getRoomsFromApp(position);
rooms.setText(" " + rm + " ");
} else {
rooms.setText(" " + Application.getRoomsFromApp(position) + " ");
}
price.setText(" " + NumberFormat.getNumberInstance(Locale.US).format(Application.getPriceFromApp(position)) + "₪");
floor.setText(" " + Application.getFloorFromApp(position) + " ");
houseNumber.setText(" " +GEThouseNumber + " ");
city.setText(" " + GETcity + " ");
street.setText(" " + GETstreet + " ");
totalSize.setText(" " + Application.getTotalSizeFromApp(position) + " מ״ר");
buildingSize.setText(" " + Application.getBuildSizeFromApp(position) + " מ״ר");
type.setText(" " + Application.getTypeFromApp(position) + " ");
buildingSitu.setText(" " + Application.getBuildSitFromApp(position) + " ");
description.setText(" " + Application.getDescripsionFromApp(position) + " ");
Picasso.with(context).load(Application.getIMG1FromApp(position)).resize(width,height/ 4).into(BIGimg);
Picasso.with(context).load(Application.getIMG1FromApp(position)).resize(55, 55).into(Image1IMG);
Picasso.with(context).load(Application.getIMG2FromApp(position)).resize(55, 55).into(Image2IMG);
Picasso.with(context).load(Application.getIMG3FromApp(position)).resize(55, 55).into(Image3IMG);
Picasso.with(context).load(Application.getIMG4FromApp(position)).resize(55, 55).into(Image4IMG);
Picasso.with(context).load(Application.getIMG5FromApp(position)).resize(55,55).into(Image5IMG);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
public void buildDialog(String msg) {
try {
AlertDialog.Builder builder = new AlertDialog.Builder(onItemClick.this);
builder.setMessage(msg)
.setTitle(R.string.alertDialogTitle)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
} catch (Exception e) {
}
}
private void fadeOutAndHideImage(final String imgUrl) {
try {
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setDuration(600);
fadeOut.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationEnd(Animation animation) {
Picasso.with(context).load(imgUrl).resize(width, height / 4).placeholder(R.drawable.noimg).into(BIGimg);
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
});
BIGimg.startAnimation(fadeOut);
} catch (Exception e) {
buildDialog(getResources().getString(R.string.onItemClickImageEffect).toString());
}
}
public void dismissProgressBar() {
try {
if (progressDialog != null && progressDialog.isShowing())
progressDialog.dismiss();
} catch (Exception e) {
buildDialog(getResources().getString(R.string.dismissProgressBarogressBarError).toString());
}
}
public void showProgressBar(final String msg) {
try {
progressDialog = ProgressDialog.show(context, "", msg, true);
} catch (Exception e) {
buildDialog(getResources().getString(R.string.showProgressBarError).toString());
}
}
}
适配器代码 :
public class CustomAdapter extends BaseAdapter {
private Context context;
private int RentOrSell = 0;
private MyApplication Application;
private LayoutInflater inflater;
private int width;
private int height;
public CustomAdapter(Context context) {
try {
this.context=context;
Application = ((MyApplication) context.getApplicationContext());
inflater = LayoutInflater.from(context);
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
width = metrics.widthPixels;
height = metrics.heightPixels;
}catch (Exception e){ }
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Application.getSizePosts();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView customtitle, customPriceVal, customRoom, customFloor, PriceTextView,cityNameVal,streetNameVal
,numHouseVal,buildSize;
ImageView BigIMG;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder;
try {
if(convertView == null || convertView.getTag()==null) {
holder = new Holder();
convertView = inflater.inflate(R.layout.custom_list_item, null);
holder.BigIMG = (ImageView) convertView.findViewById(R.id.custom_BIG_IMG);
holder.cityNameVal = (TextView) convertView.findViewById(R.id.cityNameVal);
holder.streetNameVal = (TextView) convertView.findViewById(R.id.streetNameVal);
holder.numHouseVal = (TextView) convertView.findViewById(R.id.numHouseVal);
holder.buildSize = (TextView) convertView.findViewById(R.id.buildSize);
holder.PriceTextView = (TextView) convertView.findViewById(R.id.priceText);
holder.customtitle = (TextView) convertView.findViewById(R.id.customTitle);
holder.customPriceVal = (TextView) convertView.findViewById(R.id.customPriceVal);
holder.customRoom = (TextView) convertView.findViewById(R.id.customRoomVal);
holder.customFloor = (TextView) convertView.findViewById(R.id.customFloorVal);
convertView.setTag(holder);
if (RentOrSell == 0) {
holder.PriceTextView.setText(" שכירות : ");
} else {
holder.PriceTextView.setText(" מחיר : ");
}
}
else
{
holder = (Holder) convertView.getTag();
}
holder.customtitle.setText(" "+ Application.getCityFromApp(position) + " , " + Application.getStreetFromApp(position) +
" " + Application.getHouseNumberFromApp(position)+" ");
holder.customPriceVal.setText(" " + NumberFormat.getNumberInstance(Locale.US).format(Application.getPriceFromApp(position)) + "₪");
if (Application.getRoomsFromApp(position) % 1 == 0) {
int rm = (int)Application.getRoomsFromApp(position);
holder.customRoom.setText(" " + rm + " ");
} else {
holder.customRoom.setText(" " + Application.getRoomsFromApp(position) + " ");
}
holder.cityNameVal.setText(" " + Application.getCityFromApp(position) + " ");
holder.streetNameVal.setText(" " + Application.getStreetFromApp(position) + " ");
holder.numHouseVal.setText(" " + Application.getHouseNumberFromApp(position) + " ");
holder.buildSize.setText(" " + Application.getBuildSizeFromApp(position) + " ");
holder.customFloor.setText(" " + Application.getFloorFromApp(position) + " ");
Log.i("1", "1" + "");
Picasso.with(context).load(Application.getIMG1FromApp(position)).resize(width / 2, height / 3)
.into(holder.BigIMG);
Log.i("1", "2" + "");
convertView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent onItemClickedIntent = new Intent(context, onItemClick.class);
onItemClickedIntent.putExtra("position", position);
context.startActivity(onItemClickedIntent);
}
});
} catch (Exception e) {
Log.i("1", "Error setView On Adapter ");
}
return convertView;
}
}
我从sellActivity删除“构建对话框”,“关闭进度”,因为字母列表显示进度
SellListView(发送到列表视图的活动)
:
public class sellListView extends AppCompatActivity {
private ListView lv;
private Context context;
private Toolbar toolbar;
private Dialog progressDialog;
private List<GetDataBaseCounstarctour> posts = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sell_list_view);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
InsalizeThingOfOnCreate();
}
@Override
protected void onDestroy() {
super.onDestroy();
finish();
}
@Override
protected void onStop() {
super.onStop();
try {
dismissProgressBar();
} catch (Exception e) {
}
}
public void InsalizeThingOfOnCreate() {
this.context = this;
try {
ImageView customSearchImg = (ImageView) toolbar.findViewById(R.id.customSearchImg); // cast it
customSearchImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
} catch (Exception e) {
buildDialog(getResources().getString(R.string.ImgONClick).toString());
}
// Refresh Img On Click
ImageView refreshImage = (ImageView) toolbar.findViewById(R.id.refreshImage);
refreshImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if ( ((MyApplication)getApplicationContext()).getNumOfObjects() != ((MyApplication) getApplicationContext()).getSizePosts()) {
showProgressBar(getResources().getString(R.string.Loading).toString());
RemoteDataTask getData = new RemoteDataTask();
getData.execute();
dismissProgressBar();
{} catch (Exception e) {
buildDialog(getResources().getString(R.string.ImgONClick).toString());
}
}
});
if (AppStatus.getInstance(this).isOnline()) {
try {
lv = (ListView) findViewById(R.id.listView);
Parse.initialize(this, xxxxxxxxx,xxxxxxxxx);
RemoteDataTask getData = new RemoteDataTask();
getData.execute();
}catch (Exception e){}}
else {
try{
dismissProgressBar();
buildDialog(getResources().getString(R.string.noInternet).toString());
}catch (Exception e){}
}
}
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
public RemoteDataTask() {
}
@Override
protected void onPreExecute() {
super.onPreExecute();
showProgressBar(context.getResources().getString(R.string.Loading).toString());
}
@Override
protected Void doInBackground(Void... params) {
try {
posts = new ArrayList<GetDataBaseCounstarctour>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Sell");
query.orderByDescending("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> postList, ParseException e) {
if (e == null) {
posts.clear();
for (ParseObject post : postList) {
ParseFile f1 = (ParseFile) post.get("ImageFile");
ParseFile f2 = (ParseFile) post.get("ImageFile2");
ParseFile f3 = (ParseFile) post.get("ImageFile3");
ParseFile f4 = (ParseFile) post.get("ImageFile4");
ParseFile f5 = (ParseFile) post.get("ImageFile5");
GetDataBaseCounstarctour GDB = new GetDataBaseCounstarctour(post.getString("city"), post.getString("street"), post.getString("totalSize")
, post.getString("buildSize"), post.getString("typeBuilding"), post.getString("BuildingSituation"), post.getString("FloorNumber"),
post.getDouble("roomNumber"), post.getInt("price"), post.getString("HouseNumber"), post.getString("description"), post.getObjectId()
, f1.getUrl(), f2.getUrl(), f3.getUrl(), f4.getUrl(), f5.getUrl());
posts.add(GDB);
}
}
else {
}
publishProgress();
}
});
}
catch (Exception e)
{
buildDialog(context.getResources().getString(R.string.errorGetDataFromServer).toString());
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
Log.i("1", "onPost ");
dismissProgressBar();
((MyApplication) getApplicationContext().getApplicationContext()).setNumOfObjects(posts.size());
((MyApplication) getApplicationContext().getApplicationContext()).setPosts(posts);
lv = (ListView) findViewById(R.id.listView);
posts = null;
lv.setAdapter(new CustomAdapter(context));
}
@Override
protected void onPostExecute(Void result) {
}
}
}