简体   繁体   English

如何使用字典解决WP7中的内存不足异常?

[英]How to resolve Out of memory exception in WP7 using dictionary?

I build an application in WP7 where i require to load around 20000 hard coded data({'a',"XYZ"},{'b',"mno"},....)on which i have to perform the search. 我在WP7中构建了一个应用程序,在该应用程序中,我需要加载大约2万个硬编码数据({'a',“ XYZ”},{'b',“ mno”} ...)。 。 So i trying to do this by creating a dictionary making 'a' as key and value as "XYZ". 所以我试图通过创建一个字典来做到这一点,使“ a”作为键,值作为“ XYZ”。 As soon as my dictionary gets filled it gives Out of memory exception. 一旦我的字典被填满,它就会发出“内存不足”异常。 How can i solve this problem considering that im building WP7 application? 考虑到正在构建WP7应用程序,我该如何解决此问题? Or Is there some way other than using dictionary? 还是除了词典以外还有其他方法吗?

Whenever you are loading so much data onto a phone , you're doing it wrong. 每当您将大量数据加载到手机上时 ,您就在做错事情。 Firstly, the bandwidth issue is going to kill your app. 首先,带宽问题将杀死您的应用程序。 Second, the memory issue has already killed your app. 其次,内存问题已经杀死了您的应用程序。 Thirdly, the CPU issue is going to kill your app. 第三,CPU问题将杀死您的应用程序。 The conclusion is, your user ends up killing your app. 结论是,您的用户最终杀死了您的应用程序。

Recommended solution: find a way to categorize the data so that not all of it must download to the phone. 推荐的解决方案:找到一种对数据进行分类的方法,以使并非所有数据都必须下载到电话中。 Do your processing on the server where it belongs (not on a phone). 在其所属的服务器(而不是电话)上进行处理。

If you insist on processing so much data on the phone, first try to manage the download size. 如果您坚持要在手机上处理大量数据,请首先尝试管理下载大小。 Remember you're talking about a mobile phone here, and not everywhere has max 3G speeds. 请记住,您在这里谈论的是手机,并不是每个地方都有最高3G速度。 Try to compress the data structure as much as possible (eg using a tree to store common prefixes). 尝试尽可能地压缩数据结构(例如,使用树来存储公共前缀)。 Also try to zip up the data before downloading. 在下载之前,也请尝试压缩数据。

Then count your per-object memory usage aggressively. 然后积极计算每个对象的内存使用量。 Putting in 20,000 strings can easily consume a lot of memory. 输入20,000个字符串很容易消耗大量内存。 You'd want to reduce the size of per-object memory usage as possible. 您希望尽可能减少每个对象的内存使用量。 In your example, you are just putting strings in there, so I can't guess how you'd be using up the tens of MB allowable on a WP7 app. 在您的示例中,您只是在其中放置了字符串,因此我无法猜测您将如何用尽WP7应用程序上允许的数十MB。 However, if you are putting not just strings, but large objects, count the bytes. 但是,如果您不仅要放置字符串,还要放置大对象,请计算字节数。

Also, manage fragementation aggressively. 另外,积极管理碎片化。 The last thing you'll want to do is to new Dictionary() then dict.Add(x,y); 您要做的最后一件事是先new Dictionary()然后是dict.Add(x,y); in a for-loop. 在for循环中。 When the dictionary's internal table space runs full, it gets allocated to a new place, and the entire dictionary copied to the new place, wasting the original space. 当字典的内部表空间用完时,会将其分配到新位置,然后将整个字典复制到新位置,浪费原始空间。 You end up having lots of fragmented memory space. 您最终将拥有大量零散的内存空间。 Do a new Dictionary(20000) or something to reserve the space first in one go. 进行一次new Dictionary(20000)或其他操作以一次性保留空间。

Instead of storing it in memory as a Dictionary you can store it in a Database ( wp7sqlite ) and fetch only the data required.In this way you can store whatever amount of data. 可以将其存储在Databasewp7sqlite )中并仅获取所需的数据, 而不必将其作为Dictionary存储在内存中,从而可以存储任意数量的数据。

Edit 编辑

No nothing is required in extra from user end.you can create the database using sqlite manager ,attach this to the project.Copy DB to Isolated storage on first usage.and you can access the DB whenever you want .Check this Link DB helper .This link uses sqlitewindowsphone instead of WP7Sqlite.I prefer wp7sqlite Since i got an error using sqlitewindowsphone. 用户端不需要任何额外的操作。您可以使用sqlite管理器创建数据库,将其附加到项目中。首次使用时将数据库复制到独立存储中。您可以随时访问该数据库。检查此链接数据库帮助器 。此链接使用sqlitewindowsphone而不是WP7Sqlite。我更喜欢wp7sqlite,因为使用sqlitewindowsphone时出现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM