SQL游标引发内存不足而调用的getString

编程入门 行业动态 更新时间:2024-10-25 14:33:29
本文介绍了SQL游标引发内存不足而调用的getString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

附件参考我先前的问题: - 超出内存

Attachment Reference to my earlier question :- Out of memory

我会尽量为precise越好。我正在从我的Web服务调用长的base64字符串的响应。我去code字符串,并获得其中包含我的数据一个巨大的字符串。我反序列化字符串并使用如下字符串创建我的类的对象。

I would try to be as precise as possible. I am getting a response from my web-service call a long base64 string. I decode the string and get a huge string which contains my data. I de-serialize the string and create the object of my class using the string as below.

String decryptedXml = XmlObject.toDecryptedXmlString(gameDetail.getGameData(), app.getSessionEncryptionKey()); Game noviceGame = deserialiseGame(decryptedXml, NoviceGamer.class);

desrialiseGame()就是其中desrialise数据和创建并返回我的游戏实例的方法。为了保持这一对象多会话(登录/注销)我存储我的整个GAMEDATA(我的字符串,其反序列化给了我游戏实例)的数据库中。

desrialiseGame() is just a method which desrialise the data and create and return my game instance. To maintain this object to multiple session(login/logout) I store my entire gameData(my string whose De-serialization gave me my Game instance) in database.

当用户登录,创建游戏实例,我从我的数据库获取串并再次尝试反序列化,让我找回我的游戏实例下一次。但是,当我试图从我的数据库获取字符串,我得到异常内存已满,而获取的字符串。

Next time when user login, to create the Game instance I fetch the string from my DB and again try to De-serialize so that I get back my Game instance. But when I try to get the string from my DB, I get 'OUT OF MEMORY' exception while fetching the string.

调用方法反序列化游戏的下面。

Method called to De-serialize the game is below.

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

我的数据库事务如下:

My Database transaction is as below:

@Override protected GameEntity getEntityFromCursor(Cursor cursor) { String gameRef = cursor.getString(cursor.getColumnIndex(GAME_REF)); String detailLevel = cursor.getString(cursor.getColumnIndex(DETAIL_LEVEL)); int gameVersion = cursor.getInt(cursor.getColumnIndex(GAME_VERSION)); String gameData = cursor.getString(cursor.getColumnIndex(GAME_DATA)); String status = cursor.getString(cursor.getColumnIndex(GAME_STATUS)); long longStart = cursor.getLong(cursor.getColumnIndex(VISIT_START_TIME)); Date startTime = longStart == -1 ? null : new Date(longStart); long longFinish = cursor.getLong(cursor.getColumnIndex(VISIT_END_TIME)); Date finishTime = longFinish == -1 ? null : new Date(longFinish); long longComplete = cursor.getLong(cursor.getColumnIndex(GAME_COMPLETION_TIME)); Date completionTime = longComplete == -1 ? null : new Date(longComplete); GameEntity entity = new GameEntity(gameRef, detailLevel, gameVersion, gameData, ); entity.setGameStatus(status); entity.setStartTime(startTime); entity.setFinishTime(finishTime); entity.setGameCompletionTime(completionTime); return entity; }

但是当我尝试获取来自数据库@Line数据字符串GAMEDATA = cursor.getString(cursor.getColumnIndex(GAME_DATA)); 我收到内存不足的错误。按我发现,当我添加了标志largeHeap = true在清单中应用的标签,我的应用程序得到该死的缓慢。也developer.android状态

but when I try to fetch the data from database @Line String gameData = cursor.getString(cursor.getColumnIndex(GAME_DATA)); I get out of memory error. As per my finding, When I add the flag largeHeap=true in manifest in application tag, my application gets damn slow. And also developer.android states

从不要求一大堆,只是因为你已经用完了内存  你需要速战速决,你应该使用它,只有当你确切地知道  您所有的内存被分配以及为什么必须保留。  然而,即使你确信你的应用程序可以证明一大堆,  你应该避免请求到任何程度地。

Never request a large heap simply because you've run out of memory and you need a quick fix—you should use it only when you know exactly where all your memory is being allocated and why it must be retained. Yet, even when you're confident your app can justify the large heap, you should avoid requesting it to whatever extent possible.

任何人都可以建议我如何避免这个问题。有没有使用位图其中大部分的SO问题包含的内容。任何帮助将AP preciated。

Can anybody suggest me how to avoid this. There is no bitmap use which most of the SO question contains. Any help will be appreciated.

推荐答案

我遇到了问题,因为一个简单的原因,字符串的大小是巨大的。巨大。所以我决定减少字符串的大小。我已经从XML隔离的图像数据,并存储在不同的表中。这减少了需要反序列化的数据量。我单独重新加载额外的数据。感谢您的答案和你的precious时间。

I ran into problem because of the simple reason, the size of the string was huge. HUGE. 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.

更多推荐

SQL游标引发内存不足而调用的getString

本文发布于:2023-11-04 17:41:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1558587.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:游标   内存不足   SQL   getString

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!