Memory Leak Issue causing GC overhead limit error



I seem to have a memory leak somewhere. I keep getting GC Overhead Limit and Eclipses crashes. Does anyone see anything that might not be closed that would cause this to happen. It says



history_layout.xml GC overhead limit exceeded
history_layout.xml Java heap space


Here's a screenshot of errors I'm getting too http://postimg.org/image/4kr5ouij9/


Here's my code


history_layout.xml



![<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/floorc"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date " />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game1 " />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game2 " />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game3 " />

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Series" />

<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Average" />

</LinearLayout>

<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >

</ListView>

</LinearLayout>][2]


EnterScoresActivity.java



import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

public class EnterScoresActivity extends Activity {

private Button saveButton;
private Button historyButton;
private EditText game1ScoreEditText;
private EditText game2ScoreEditText;
private EditText game3ScoreEditText;
private TextView date;
private TextView seriesTotal;
private Button changeDateButton;

private int month; // private within this class
private int day;
private int year;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_scores);

setUpViews();

// put today's date on the screen
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);

Date today = calendar.getTime();
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
String cs = df.format(today);
date.setText(cs);
}

/*
* Get the 3 scores from the interface, validate the scores create an object
* to hold the scores.
*/
public void saveClickHandler(View v) {
String rawScore;
rawScore = game1ScoreEditText.getText().toString();
int score1 = Integer.parseInt(rawScore);
rawScore = game2ScoreEditText.getText().toString();
int score2 = Integer.parseInt(rawScore);
rawScore = game3ScoreEditText.getText().toString();
int score3 = Integer.parseInt(rawScore);

Log.d("EnterScores", "I hear the Save Button");


if (isValid(score1) && isValid(score2) && isValid(score3)) {
BowlingScores bowlingScores;
Date dateOfGames = new GregorianCalendar(year, month, day).getTime();

bowlingScores = new BowlingScores(score1, score2, score3,
dateOfGames);

seriesTotal.setText("" + bowlingScores.calculateSeriesScore());

MyBowlingScoresApplication app = (MyBowlingScoresApplication) getApplication();
app.addBowlingScores(bowlingScores);

} else {
// pop up a dialog indicating that data is invalid
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Invalid Scores")
.setMessage("Bowling scores cannot be greater than 300")
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {

dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}

private boolean isValid(int score) {
if (score >= 0 && score <= 300)
return true;
return false;
}
public void handleShowHistoryClick(View v){
Intent intent = new Intent(this, HistoryActivity.class);
startActivity(intent);

}
public void changeDateClickHandler(View v) {
Log.d("Enter Scores", "I hear the Change Date Button");

DatePickerDialog.OnDateSetListener datePickerListener;

datePickerListener = new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int yearOfYear, int monthOfYear,
int dayOfMonth) {
year = yearOfYear;
month = monthOfYear;
day = dayOfMonth;

Calendar cal = new GregorianCalendar(year, month, day);
Date dateOfGames = cal.getTime();
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
String cs = df.format(dateOfGames);
date.setText(cs);
}
};

DatePickerDialog dpd = new DatePickerDialog(this, datePickerListener, year, month,day);
dpd.show();

}

private void setUpViews() {
saveButton = (Button) findViewById(R.id.SaveButton);
historyButton = (Button) findViewById(R.id.ShowHistory);
changeDateButton = (Button) findViewById(R.id.changeDateButton);
game1ScoreEditText = (EditText) findViewById(R.id.Game1EditText);
game2ScoreEditText = (EditText) findViewById(R.id.Game2EditText);
game3ScoreEditText = (EditText) findViewById(R.id.Game3EditText);
date = (TextView) findViewById(R.id.DateTextView);
seriesTotal = (TextView) findViewById(R.id.SeriesTotalTextView);
}
}


BowlingScoresDatabaseHelper.java



package com.valenciaprogrammers.mybowlingscores;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class BowlingScoresDatabaseHelper extends SQLiteOpenHelper{

public static final String DB_NAME= "MyBowlingScores.SQLite";
public static final int DB_VERSION=1;
public static String BOWLING_SCORES_TABLE= "BowlingScoresTable";
public static String RECORD_ID="ID";
public static String DATE = "Date";
public static String GAME1 = "Game1";
public static String GAME2 = "Game2";
public static String GAME3= "Game3";

public BowlingScoresDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase bowlingScoresDB) {
String sqlStatement = "create table " + BOWLING_SCORES_TABLE
+ " ("
+ RECORD_ID + " integer primary key autoincrement not null,"
+ DATE + " integer,"
+ GAME1 + " integer,"
+ GAME2 + " integer,"
+ GAME3 + " integer"
+ ");";

Log.d("Bowling Database", sqlStatement);
bowlingScoresDB.execSQL(sqlStatement);

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {


}

}


MyBowlingScoresApplication.java



import java.util.ArrayList;

import android.app.Application;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import static com.valenciaprogrammers.mybowlingscores.BowlingScoresDatabaseHelper.*;

public class MyBowlingScoresApplication extends Application {

private ArrayList<BowlingScores> allBowlingScores;
private SQLiteDatabase bowlingScoresDB;

@Override
public void onCreate() {
super.onCreate();
BowlingScoresDatabaseHelper databaseHelper = new BowlingScoresDatabaseHelper(
this);
bowlingScoresDB = databaseHelper.getWritableDatabase();

readBowlingScoresFromDB();
}

private void readBowlingScoresFromDB() {
allBowlingScores = new ArrayList<BowlingScores>();
Cursor bowlingScoresCursor;

bowlingScoresCursor = bowlingScoresDB.query(BOWLING_SCORES_TABLE,
new String[] { RECORD_ID, DATE, GAME1, GAME2, GAME3 },
null, null, null, null, DATE);

bowlingScoresCursor.moveToFirst();
BowlingScores tempBS;

if (!bowlingScoresCursor.isAfterLast()) {

do {
long id = bowlingScoresCursor.getLong(0);
long dateEpoch = bowlingScoresCursor.getLong(1);
int game1 = bowlingScoresCursor.getInt(2);
int game2 = bowlingScoresCursor.getInt(3);
int game3 = bowlingScoresCursor.getInt(4);
tempBS = new BowlingScores(id, dateEpoch, game1, game2, game3);
allBowlingScores.add(tempBS);

Log.d("Bowling Database", tempBS.toString());
} while (bowlingScoresCursor.moveToNext());
}
bowlingScoresCursor.close();
}

public void addBowlingScores(BowlingScores bowlingScores) {
assert bowlingScores != null;

ContentValues cv = new ContentValues();
cv.put(BowlingScoresDatabaseHelper.DATE, bowlingScores.getDateEpoch());
cv.put(BowlingScoresDatabaseHelper.GAME1, bowlingScores.getGame1());
cv.put(BowlingScoresDatabaseHelper.GAME2, bowlingScores.getGame2());
cv.put(BowlingScoresDatabaseHelper.GAME3, bowlingScores.getGame3());

Log.d("Bowling Database", "Before Inserting a record" + bowlingScores);
long idPassedBack = bowlingScoresDB.insert(
BowlingScoresDatabaseHelper.BOWLING_SCORES_TABLE, null, cv);
bowlingScores.setId(idPassedBack);
Log.d("Bowling Database", "After Inserting a record" + bowlingScores);
allBowlingScores.add(bowlingScores);


}

public ArrayList<BowlingScores> getAllBowlingScores() {
return allBowlingScores;
}

private void setAllBowlingScores(ArrayList<BowlingScores> allBowlingScores) {
this.allBowlingScores = allBowlingScores;
}

}

No comments:

Post a Comment