简体   繁体   中英

Android Garbage Collector freeing memory in the middle of an AJAX request

I just ran into this problem. I have a XWalkView ( Crosswalk WebView) inside my Android Application. Inside the XWalkView i make a couple of AJAX requests, the problem is that the Java Garbage Collector is freeing up the memory whilst i'm doing the request. Therefor the request is unable to finish.

As for the AJAX part, i'm using qwest , a simple library for doing AJAX requests using promises.

The Java code for this is pretty simple, i don't think this is the problem:

webView = (XWalkView)findViewById(R.id.walk_view);
webView.setResourceClient(new MyAppWebViewClient(webView));
webView.setWillNotCacheDrawing(true);
webView.load("file:///android_asset/www/index.html", null);

I added the willNotCacheDrawing to try and free up more memory, so that the request can finish, this is not helping much.

MyAppWebViewClient is a subclass of XWalkResourceClient , it's not doing that much, just triggering a different Action when a PDF is loaded. The problem also occurs when i don't use my own ResourceClient.

The HTML / JavaScript part is super simple, it downloads no more than 0.5MB, with a request like this:

qwest.get('my.server.com/api')
 .then(function(xhr, response) {
    // do work with response
    // to bad it never reaches this
 })
 .catch(function(xhr, response, e) {
    // I just get a timeout here,
    // there is no way the server is timing out,
    // it works perfectly on iOS, Web and any other platform
 });

I figured this has something to do with the Garbage Collector because if i look at the Memory Monitor, this is what happens while doing the request:

垃圾收集器在工作吗?

The first 'rise' in memory is when the request starts, as soon as the memory usage becomes stable again, the request has failed. I figured the sudden drops is the Garbage Collector freeing up memory that i just allocated for my AJAX request.. hmm.

I am quite new to Android Development, especially when it comes to memory management. Is it normal that the garbage collector doesn't let me allocate more than 7,76MB of RAM? It seems a little low for a full app.

Do you guys have any idea?

Thanks!

This not really abnormal, slightly small but not surprising. If you need more information about available heap memory for a single process you may read How much memory for Android process or the official documentation on memory management under Android . You will see that heap size is in general very limited.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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