我正在使用Restlet 2.1 RC 4,org.restlet.ext.httpclient
,org.restlet.ext.jackson
和org.restlet.ext.json
从远程服务器获取数据。但是,对于仅6个物体,家中的无线数据连接需要6秒钟。
正如您所看到的,除了GC之外,还有许多消息,例如Starting the internal [HTTP/1.1] client
和Stopping the internal[HTTP/1.1] client
。这对应于需要花费大量时间的大型GC。
06-01 03:21:46.913: RestletTrip(2653): Querying: [SOMEURL/SOMENUMBER]
06-01 03:21:46.933: I/org.restlet(2653): Starting the internal [HTTP/1.1] client
06-01 03:21:47.053: D/dalvikvm(2653): GC_CONCURRENT freed 708K, 34% free 9214K/13831K, external 576K/1020K, paused 3ms+3ms
06-01 03:21:47.053: I/org.restlet(2653): Stopping the internal[HTTP/1.1] client
06-01 03:21:47.103: D/dalvikvm(2653): GC_FOR_MALLOC freed 4924K, 61% free 5523K/13831K, external 576K/1020K, paused 31ms
06-01 03:21:47.173: D/dalvikvm(2653): GC_CONCURRENT freed 95K, 53% free 6574K/13831K, external 576K/1020K, paused 2ms+3ms
06-01 03:21:47.253: D/dalvikvm(2653): GC_CONCURRENT freed 46K, 43% free 8012K/13831K, external 576K/1020K, paused 2ms+3ms
06-01 03:21:47.734: I/global(2653): Loaded time zone names for en_US in 397ms.
06-01 03:21:48.014: RestletTrip(2653): Querying: [SOMEURL/SOMENUMBER]
06-01 03:21:48.124: D/dalvikvm(2653): GC_CONCURRENT freed 1225K, 38% free 8688K/13831K, external 576K/1020K, paused 3ms+3ms
06-01 03:21:48.124: I/org.restlet(2653): Stopping the internal[HTTP/1.1] client
06-01 03:21:48.134: I/org.restlet(2653): Starting the internal [HTTP/1.1] client
06-01 03:21:48.184: D/dalvikvm(2653): GC_FOR_MALLOC freed 5022K, 62% free 5341K/13831K, external 576K/1020K, paused 31ms
06-01 03:21:48.244: D/dalvikvm(2653): GC_CONCURRENT freed 96K, 55% free 6307K/13831K, external 576K/1020K, paused 2ms+3ms
06-01 03:21:48.314: D/dalvikvm(2653): GC_CONCURRENT freed 44K, 45% free 7663K/13831K, external 576K/1020K, paused 3ms+4ms
06-01 03:21:48.795: I/global(2653): Loaded time zone names for en_US in 405ms.
06-01 03:21:49.015: D/dalvikvm(2653): GC_CONCURRENT freed 695K, 37% free 8736K/13831K, external 576K/1020K, paused 3ms+3ms
06-01 03:21:49.155: RestletTrip(2653): Querying: [SOMEURL/SOMENUMBER]
06-01 03:21:49.165: I/org.restlet(2653): Starting the internal [HTTP/1.1] client
06-01 03:21:49.225: D/dalvikvm(2653): GC_FOR_MALLOC freed 672K, 30% free 9778K/13831K, external 576K/1020K, paused 34ms
06-01 03:21:49.225: I/org.restlet(2653): Stopping the internal[HTTP/1.1] client
06-01 03:21:49.265: D/dalvikvm(2653): GC_FOR_MALLOC freed 4902K, 61% free 5637K/14343K, external 576K/1020K, paused 32ms
06-01 03:21:49.325: D/dalvikvm(2653): GC_CONCURRENT freed 97K, 54% free 6703K/14343K, external 576K/1020K, paused 3ms+4ms
06-01 03:21:49.395: D/dalvikvm(2653): GC_CONCURRENT freed 54K, 44% free 8167K/14343K, external 576K/1020K, paused 3ms+3ms
06-01 03:21:49.876: I/global(2653): Loaded time zone names for en_US in 407ms.
06-01 03:21:50.136: RestletTrip(2653): Querying: [SOMEURL/SOMENUMBER]
06-01 03:21:50.156: I/org.restlet(2653): Starting the internal [HTTP/1.1] client
06-01 03:21:50.236: D/dalvikvm(2653): GC_CONCURRENT freed 1295K, 38% free 8905K/14343K, external 576K/1020K, paused 3ms+4ms
06-01 03:21:50.236: I/org.restlet(2653): Stopping the internal[HTTP/1.1] client
06-01 03:21:50.286: D/dalvikvm(2653): GC_FOR_MALLOC freed 4991K, 61% free 5636K/14343K, external 576K/1020K, paused 32ms
06-01 03:21:50.346: D/dalvikvm(2653): GC_CONCURRENT freed 52K, 53% free 6748K/14343K, external 576K/1020K, paused 2ms+4ms
06-01 03:21:50.386: D/dalvikvm(2653): GC_FOR_MALLOC freed 50K, 43% free 8249K/14343K, external 576K/1020K, paused 30ms
06-01 03:21:50.867: I/global(2653): Loaded time zone names for en_US in 400ms.
06-01 03:21:51.107: RestletTrip(2653): Querying: [SOMEURL/SOMENUMBER]
06-01 03:21:51.127: I/org.restlet(2653): Starting the internal [HTTP/1.1] client
06-01 03:21:51.207: D/dalvikvm(2653): GC_CONCURRENT freed 1290K, 38% free 9006K/14343K, external 576K/1020K, paused 2ms+4ms
06-01 03:21:51.207: I/org.restlet(2653): Stopping the internal[HTTP/1.1] client
06-01 03:21:51.257: D/dalvikvm(2653): GC_FOR_MALLOC freed 4937K, 60% free 5791K/14343K, external 576K/1020K, paused 32ms
06-01 03:21:51.317: D/dalvikvm(2653): GC_CONCURRENT freed 106K, 52% free 6900K/14343K, external 576K/1020K, paused 2ms+4ms
06-01 03:21:51.387: D/dalvikvm(2653): GC_CONCURRENT freed 54K, 42% free 8448K/14343K, external 576K/1020K, paused 3ms+4ms
06-01 03:21:51.868: I/global(2653): Loaded time zone names for en_US in 406ms.
06-01 03:21:52.118: RestletTrip(2653): Querying: [SOMEURL/SOMENUMBER]
06-01 03:21:52.128: I/org.restlet(2653): Starting the internal [HTTP/1.1] client
06-01 03:21:52.218: D/dalvikvm(2653): GC_CONCURRENT freed 1297K, 36% free 9198K/14343K, external 576K/1020K, paused 3ms+3ms
06-01 03:21:52.218: I/org.restlet(2653): Stopping the internal[HTTP/1.1] client
我使用的代码是(剥离以便更快地理解):
public List<Trip> getTrips() {
// All the ids for the different trips
List<Long> ids = new ArrayList<Long>;
ids.add(new Long(14));
ids.add(new Long(45));
ids.add(new Long(34));
ids.add(new Long(11));
ids.add(new Long(33));
ids.add(new Long(17));
// All the Trips
List<Trip> result = new ArrayList<Trip>();
for (Long tripID : ids) {
Trip trip = getTrip(tripID);
// Add trip to the list
if (trip != null) {
result.add(trip);
}
}
return result;
}
public Trip getTrip(ISettings settings, Long tripID) throws GetTripException, NoSuchElementException {
// Create resource
final TripsResource tripResource = getClientResource(ServerPaths.getTrip(tripID), TripsResource.class);
// Get trip from server
return tripResource.getTrip();
}
我正在使用的引擎设置是
import org.restlet.engine.Engine;
import org.restlet.ext.httpclient.HttpClientHelper;
import org.restlet.ext.jackson.JacksonConverter;
import org.restlet.ext.json.JsonConverter;
import org.restlet.Client;
import org.restlet.data.Protocol;
protected void setupEngine() {
final Engine engine = Engine.getInstance();
engine.getRegisteredClients().add(new HttpClientHelper(new Client(Protocol.HTTP)));
engine.getRegisteredConverters().add(new JacksonConverter());
engine.getRegisteredConverters().add(new JsonConverter());
}
因此,由于GC,检索速度很慢。如何提高性能?
答案 0 :(得分:3)
您确实一遍又一遍地重新创建HTTP客户端连接器。
解决方案是创建一个实例:myClient = new Client(Protocol.HTTP);
然后,为您通过myClientResource.setNext(myClient)创建的每个ClientResource设置它;
BTW,如果您使用的是父Restlet Component&amp;应用程序,这将自动为您完成。