- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在 logcat 中我一直看到这个:
I/art:后台粘性并发标记扫描 GC 释放了 141468(7MB) 个 AllocSpace 对象,3(255KB) 个 LOS 对象,25% 空闲,21MB/29MB,暂停 1.228ms 总计 132.652ms
还有这个:
W/art:挂起所有线程花费了:xxx 毫秒
它让我得到一个 org.apache.http.conn.HttpHostConnectException Connection to https://plcloud.c6.ixsecure.com refused围绕我进行的最后一次 Web 服务方法调用。
我怎样才能停止“暂停所有线程”和大量垃圾收集的使用,以便我可以在不遇到内存问题的情况下完成所有 Web 服务方法调用?
目前它从网络服务获取数据并将其存储在本地 SQLite 数据库中。这是我的代码:
调用同步类“get”方法的代码(它们都从 web 服务获取数据并将其保存到 sqlite 数据库):
class SyncThread extends Thread {
@Override
public void run() {
try {
syncHandler.sendEmptyMessage(16);
Credentials credentials = new Credentials(usernameField
.getText().toString(), passwordField.getText()
.toString());
UserCredentialsOut userCredentials = credentials
.getCredentials();
if (userCredentials.getReturnCode() == 1) {
Settings.getInstance().setEntityId(
userCredentials.getEntityId());
syncHandler.sendEmptyMessage(1);
CustomerSync custSync = new CustomerSync(context,
syncProgressDialog);
Log.i("Customer Sync", "getCustomers");
custSync.getCustomers(credentials);
syncHandler.sendEmptyMessage(2);
PetSync petSync = new PetSync(context, syncProgressDialog);
Log.i("Pet Sync", "getPets");
petSync.getPets(credentials);
syncHandler.sendEmptyMessage(4);
SystemSync systemSync = new SystemSync(context,
syncProgressDialog);
Log.i("ListValues Sync", "getListValuesInitials");
systemSync.getListValuesInitials(credentials);
syncHandler.sendEmptyMessage(5);
Log.i("Vets Sync", "getVets");
systemSync.getVets(credentials);
syncHandler.sendEmptyMessage(6);
Log.i("Groomers Sync", "getGroomers");
try{
systemSync.getGroomers(credentials);
}catch (NullPointerException e) {
Log.e("NullPointerException", e.getMessage()+"benebne");
}
syncHandler.sendEmptyMessage(7);
Log.i("Services Sync", "getServices");
systemSync.getServices(credentials);
syncHandler.sendEmptyMessage(8);
Log.i("CalendarDays Sync", "getCalendarDays");
systemSync.getCalendarDays(credentials);
syncHandler.sendEmptyMessage(10);
Log.i("loyeeUnavailTimes Sync",
"getEmployeeUnavailTimes");
systemSync.getEmployeeUnavailTimes(credentials);
syncHandler.sendEmptyMessage(12);
Log.i("Runs Sync", "getRuns");
systemSync.getRuns(credentials);
syncHandler.sendEmptyMessage(14);
Log.i("DaycareGroups Sync", "getDaycareGroups");
systemSync.getDaycareGroups(credentials);
syncHandler.sendEmptyMessage(3);
OptionsSync optSync = new OptionsSync(context,
syncProgressDialog);
Log.i("SystemOptions Sync", "getSystemOptions");
SystemOptions systemOptions = optSync
.getSystemOptions(credentials);
if (systemOptions != null) {
if (systemOptions.isModuleGroom()) {
syncHandler.sendEmptyMessage(9);
PetGroomSync petGroomSync = new PetGroomSync(
context, syncProgressDialog);
Log.i("PetGrooms Sync", "getPetGrooms");
petGroomSync.getPetGrooms(credentials);
syncHandler.sendEmptyMessage(11);
AppointmentSync appointmentSync = new AppointmentSync(
context, syncProgressDialog);
Log.i("Appointments Sync", "getAppointments");
appointmentSync.getAppointments(credentials);
}
if (systemOptions.isModuleBoard()) {
syncHandler.sendEmptyMessage(13);
AppointmentBoardSync appointmentBoardSync = new AppointmentBoardSync(
context, syncProgressDialog);
Log.i("ApptBoards Sync", "getApptBoards");
appointmentBoardSync.getApptBoards(credentials);
}
if (systemOptions.isModuleDaycare()) {
syncHandler.sendEmptyMessage(15);
AppointmentDaycareSync appointmentDaycareSync = new AppointmentDaycareSync(
context, syncProgressDialog);
Log.i("ApptDayCares Sync", "getApptDayCares");
appointmentDaycareSync.getApptDaycares(credentials);
}
}
syncHandler.sendEmptyMessage(0);
return;
} else {
syncHandler.sendMessage(syncHandler.obtainMessage(-1,
userCredentials.getMessage()));
return;
}
} catch (ClientProtocolException e) {
Log.e("ClientProtocolException", e.toString());
} catch (IOException e) {
Log.e("IOException", e.toString());
} catch (Exception e) {
Log.e("SyncException", e.toString());
}
syncHandler.sendMessage(syncHandler.obtainMessage(-1,
getString(R.string.connError)));
}
}
一个实际的同步类 - 所有同步类都大致相同,因此您只需要查看其中一个 - 请注意调用“initCustomersFromJson”的“getCustomers”方法 - 这些是获取数据然后保存的部分它到 sqlite 数据库:
public class CustomerSync implements ICustomerSync {
public static final int QUANTITY = 0;
private long offset;
private ProgressDialog progressDialog;
private Context context;
private HttpClient client;
private final String HTTPS_GET_CUSTOMERS = "https://plcloud.c6.ixsecure.com/PLService.svc/GetCustomers";
private final String GET_URL = "{0}?quant={1}&offset={2}";
private final String HTTPS_SYNC_CUSTOMERS = "https://plcloud.c6.ixsecure.com/PLService.svc/SyncCustomers";
private CustomerSync() {
client = new DefaultHttpClient();
}
public CustomerSync(Context context, ProgressDialog progressDialog) {
this();
this.context = context;
this.progressDialog = progressDialog;
}
public Customer[] initCustomersFromJson(Credentials credentials)
throws ClientProtocolException, IOException, URISyntaxException {
String getUri;
getUri = MessageFormat.format(GET_URL, HTTPS_GET_CUSTOMERS,
QUANTITY + "", offset + "");
credentials.initGetOAuthStructure(HTTPS_GET_CUSTOMERS);
HttpGet request = new HttpGet(getUri + "&"
+ credentials.getOauthStructure());
String content = client.execute(request, new BasicResponseHandler());
Gson gson = new GsonBuilder().serializeNulls()
.excludeFieldsWithoutExposeAnnotation().create();
return gson.fromJson(content, Customer[].class);
}
@Override
public void getCustomers(Credentials credentials)
throws ClientProtocolException, IOException, URISyntaxException {
ICustomerDAO customerDAO = (ICustomerDAO) DAOFactory.getDAO(
ICustomerDAO.class.getName(), context);
customerDAO.open();
Customer[] customers;
customers = initCustomersFromJson(credentials);
progressDialog.setMax(customers.length);
progressDialog.setProgress(0);
customerDAO.saveCustomers(customers, progressDialog);
if (customers.length > 0) {
offset = customers[customers.length - 1].getId();
}
customerDAO.close();
Settings.getInstance().setLastSyncCustomerDatetime(new Date());
}
最佳答案
我认为如果数据非常大,最好流式传输数据。我在你的代码中看到你已经在使用 GSON,你可以使用 GSON 来流式传输 json 数据。
例如:
String getUri;
getUri = MessageFormat.format(GET_URL, HTTPS_GET_CUSTOMERS,
QUANTITY + "", offset + "");
credentials.initGetOAuthStructure(HTTPS_GET_CUSTOMERS);
HttpGet request = new HttpGet(getUri + "&"
+ credentials.getOauthStructure());
HttpResponse response = client.execute(request);
Reader streamReader = new InputStreamReader(response
.getEntity().getContent());
JsonReader reader = new JsonReader(streamReader);
ArrayList<Customer> customerList = new ArrayList<Customer>();
reader.beginArray();
while (reader.hasNext()) {
Customer customer = new Customer();
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("Action")) {
customer.setAction(reader.nextString());
} else if (name.equals("Addr1")) {
customer.setAddr1(reader.nextString());
} else if (name.equals("Addr2")) {
customer.setAddr2(reader.nextString());
} else if (//Other condition and value)
...
} else {
reader.skipValue();
}
}
reader.endObject();
customerList.add(customer);
}
reader.endArray();
reader.close();
使用 JsonReader,您可以一个接一个地流式传输数据并直接处理它,而不是将所有数据保存到一个数组中然后处理它,这可能会导致内存问题。
关于java - 线程负载过重导致内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31665803/
在 Web 应用程序架构设计期间,我必须从概念上计算我的服务器之一可以服务多少个当前客户端。然后我可以预算它。 那么,有什么公式可以遵循吗?或者,你如何计算这个?或者,通常,一个 httpd/tomc
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是
我正在使用 Angular 5,我正在尝试在加载 div 的背景图像时获取加载图标。 如果它是一个普通的 img,我对此没有问题,但如果我尝试将它作为背景,它就不起作用。 这里是一些示例代码 app.
我们怎么知道我们的程序在 CPU 上有多少负载? 我尝试使用 htop 找到它。但是 htop 不会给 cpu 负载。它实际上给出了我程序的 cpu 利用率(使用 pid)。 我正在使用 C 编程,L
我们发现从Spark 1.3到当前的Spark 2.0.1以来,从Oracle数据库使用Spark的API加载数据一直很慢。典型的代码在Java中是这样的: Map options =
我有时会收到 mnesia overloaded主要使用时的错误消息 async_dirty查询和 ram_copies表。所以为了了解发生了什么,我想获得更多关于 mnesia 状态的信息,例如每秒
对于通常使用很少 CPU 的程序来说,内核 CPU 非常高。 Linux 机器在状态之间交替。大多数时候,程序使用低 CPU 正常执行。在 CPU“激增”期间,程序使用 100% 可用 CPU 使用高
我正在使用 Raspberry Pi 2 来路由 wifi-eth 连接。因此,从 eth 方面来看,我有一台可以使用 Pi wifi 连接到互联网的计算机。在 Raspberry 上我启动 htop
基本上我有一个网页,其中有一个 iframe 可以从不同的域加载另一个网页。它移动得很慢,我想证明整个页面很慢只是因为 iframe 内的页面。 有什么方法可以测量总页面负载以及总页面负载中有多少%来
我们有一个基于 Spring 的应用程序,它充当使用其他 Rest API 的编排层。我只想测试这个组件的性能,而不测试正在使用的下游 api。 我正在寻找有关如何完成此操作的任何架构建议? 当前的方
我正在学习 hibernate 。为了进行测试,我使用无效 key 调用了 session.load 。当我在调试器(JB Idea)中跨过该行后,没有任何反应 - 我预计会得到 ObjectNotF
我正在开发一个小型的待办事项 PHP 应用程序。我正在使用 jQuery 构建 HTML。其中一个是一个按钮,用于启动一个模式,允许用户编辑该项目。我很好奇加载数据时更好的方法是什么: 1) 在初始加
我尝试在 twitch 播放器中使用 angular 作为覆盖标记。 我将 ng-repear 与(键,值)结合使用。 //player is here 设置是一个全局对象。但是当我尝试加载页面
我即将了解 C 语言中的特定进程如何在特定时间范围内加载 CPU。该进程可能会在运行时切换处理器核心,因此我也需要处理这个问题。 CPU为ARM处理器。 我研究了从标准顶部获取负载的不同方法,perf
这个问题在这里已经有了答案: XMLHttpRequest Origin null is not allowed Access-Control-Allow-Origin for file:/// t
您好,我正在用 Java 开发负载平衡算法。在我的系统中将有一个主节点和 n 个从节点。主节点将接收查询分发给它的从节点。但是在将查询分发到其从节点之一之前,我想测量从节点中的当前负载,以检查特定从节
我正在渲染由大约 50 万个三角形组成的相当重的对象。我使用 opengl 显示列表,在渲染方法中只调用 glCallList。我认为一旦图形基元被编译成显示列表,cpu 的工作就完成了,它只是告诉
我正在尝试加密 Sipdroid,为此我必须在 RTP 数据包获得编码的音频负载后对其进行加密。我在 RTP 数据包类中使用这个函数: public byte[] getPayload() {
我正在尝试解析以下 JSON 负载: { "results":[ [ 298.648132, 280.68692, 356.54
在动画期间 cpu 负载非常高(高达 75%) 是否有优化代码以降低 CPU 负载的方法? 我的代码: ImageView myImageView = (ImageView)findViewById(
我是一名优秀的程序员,十分优秀!