1

I am de - serializing my xml in async task. At some particular instance I am getting out of memory error while de- serialization. I know there is a flag called largeHeap which I can use in my application. But is there any way to find out specifically avoid at that place.

As per my finding System.gc() is not probably a best solution to fix it. Can anybody help me through it. Below is the code snippet.

private HashMap<String, Game> games = new HashMap<String, Game>();

public void load(LocalDatabaseHelper localDbHelper) throws Exception
{
    synchronized(gameLockObject) {
        GameDetailDAO dao = new GameDetailDAO(localDbHelper);

        //this will fetch me the all the entities from databse
        ArrayList<GameDetailEntity> dbGameDetails = dao.getEntities(null, null);

        for (GameDetailEntity gameDetail : dbGameDetails) {
            String gameLevel = gameDetail.getDetailLevel();             

            String gameXml = gameDetail.getGameData();

            Game game = null;
            if(gameLevel.equalsIgnoreCase("Novice")) {
                game = Job.deserialiseJob(gameXml, NoviceLevel.class);
            }
            else if (gameLevel.equalsIgnoreCase("Expert")) { 
                game = Job.deserialiseJob(gameXml, ExpertLevel.class);
            }

            //set the job version
            game.setGameversion(gameDetail.getGameVersion());
            game.setMagicNumber(gameDetail.getMagicNumber());
            game.setInactiveUser(gameDetail.getInactiveUser());
            game.setStartTime(gameDetail.getStartTime());
            game.setFinishTime(gameDetail.getFinishTime());
            game.setGameCompletionTime(gameDetail.getGameCompletionTime());
            if (!StringUtils.isNullOrEmpty(gameDetail.getGameStatus())) {
                game.setGameStatus(GameStatus.valueOf(gameDetail.getGameStatus()));
            }

            //add the job to the store
            games.put(gameDetail.getGameRef().toLowerCase(Locale.getDefault()), game);
        }
    }
}
Android
  • 3,828
  • 9
  • 46
  • 79
  • How many of those game detail entities are there? – CL. Sep 22 '14 at 16:51
  • Not really a limit, it may have 20 or it may have 80. Depends on what service send me – Android Sep 22 '14 at 16:53
  • So where's all the data? In `gameXml`? How large is it? – CL. Sep 22 '14 at 16:54
  • yes. the entire data is there in gameXML. and how do I specify that how large is it. BTW I have elaborated my question with all information add in my another question http://stackoverflow.com/questions/25979248/sql-cursor-throws-out-of-memory-while-calling-getstring. Can you have a look at it. that would probably answer your questions – Android Sep 22 '14 at 16:58

2 Answers2

1

The problem is not with any specific code in your app, it is with the basic design. You have just too much data that you're trying to handle at once.

Do not serialize the data (and especially do not use XML; I'd guess you do not require any markup to begin with).

Instead, store all the data of the games in a properly normalized database (i.e., use tables/columns for everything). Do not load everything at once, but load only as much as you actually need.

CL.
  • 173,858
  • 17
  • 217
  • 259
0

I ran into problem because of the simple reason, the size of the string was huge. HUGE. Si simple solution would be reduce the amount of data that is processing. So I decided to reduce the size of the string. I have segregated the image data from the XML and stored in different table. This reduce the amount of data that needed to de-serialize. I reload the extra data separately. Thanks for answers and for your precious time.

Android
  • 3,828
  • 9
  • 46
  • 79
  • @ Android. Can you see my question ? https://stackoverflow.com/questions/48563909/illegalstateexception-fragment-already-added-android?noredirect=1#comment84192332_48563909 –  Feb 03 '18 at 14:45