在Android中处理大字符串(OutOfMemeryError)

在Android中处理大字符串(OutOfMemeryError),android,Android,我目前正在开发一个Android应用程序,在Motorola Defy上遇到了OutOfMemory错误 发生错误的地方是,我将5MP图像转换为Base64编码的字符串,该字符串随后被发送到SOAP文档中包含的web服务 我知道,图像的实际编码和使用SOAP都是开销——我希望自己能够消除这些开销——但现在这是不可能的 我的问题更多的是关于如何处理大字符串,因为我不太了解Java和内存处理,所以我需要一些帮助 代码大致如下所示: String soapMessage = registration.

我目前正在开发一个Android应用程序,在Motorola Defy上遇到了OutOfMemory错误

发生错误的地方是,我将5MP图像转换为Base64编码的字符串,该字符串随后被发送到SOAP文档中包含的web服务

我知道,图像的实际编码和使用SOAP都是开销——我希望自己能够消除这些开销——但现在这是不可能的

我的问题更多的是关于如何处理大字符串,因为我不太了解Java和内存处理,所以我需要一些帮助

代码大致如下所示:

String soapMessage = registration.createSoapRequest();
//In this method a StringBuilder is used to build the message, and returns it 
//as new String(soap.toString();

//At this point I have to objects in memory using approx 6 and 9 MB each.
//I would expect it to be the StringBuilder and the string soapMessage. 
//But I can't figure out how to GC the StringBuilder - which I don't need to use 
//any more.

//Declaring the entity to post
StringEntity entity = new StringEntity(soapMessage, HTTP.UTF_8);

//By declaring the entity, I receive my OutOfMemoryException, due to having 
//the 6-7 MB string now replicated 3 times - giving 21 MB alone. 

entity.setContentType("text/xml");

//Creating client
HttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost("http://MYSERVER/services/registration.svc");
post.setHeader("Content-Type","text/xml; charset=utf-8");
post.setHeader("SOAPAction", "http://tempuri.org/IRegistration/PostRegistration");

post.setEntity(entity);

BasicHttpResponse response = (BasicHttpResponse)client.execute(post);
这里明显的内存问题似乎使我只能在内存中保存字符串的一个实例

我该怎么做

我希望有人能帮我解决这个问题,因为我觉得自己陷入了Java内存处理的困境

致意 /安德斯

我在这里“几乎”解决了我的问题。解决方案是避免使用面向对象的方法,并用相同的方法完成所有工作。这样我就能够GC使用过的对象,而且现在看起来进展顺利。