Out of memory during XML deserialisation

2019-08-02 13:33发布

问题:

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);
        }
    }
}

回答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.



回答2:

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.