HttpClient.execute throwing OutOfMemoryError

前端 未结 3 1126
醉话见心
醉话见心 2021-02-14 00:42

I\'ve an Android application that posting JSONObject as a entity by using ByteArrayEntity object. Here how it looks like:

post.setEntit         


        
相关标签:
3条回答
  • 2021-02-14 01:13

    Android devices have a per-process memory limit. The default default is 24MB, but some devices have a lower value, eg 16MB or perhaps lower. The X10i appears to have 384MB RAM, which is low for a modern Android device (1GB is now standard), and this may place additional constraints.

    I would suggest:

    1. First check the per-process limit on your device. Use ActivityManager.getMemoryClass() to check.
    2. Look at your actual memory usage using DDMS.

    You may find that, just before you are making this call, you are already at the upper limit of memory; the initialisation of the HTTPClient and the call might take you just over the limit. This will be more likely if the OOM occurs on the first call.

    If this happens only sporadically, or after a number of calls, you might have a memory leak. DDMS will help you track that down using the get allocations feature.

    0 讨论(0)
  • 2021-02-14 01:20

    On the surface, only 200 bytes which are allocated not very often will not run the process out of memory even on Android. Therefore one of these assumptions is wrong. Most likely one of the following is true:

    1) More than 200 bytes is being allocated in the failure case (for example, is a new HttpClient being allocated for every call).

    2) This code is getting called very often

    3) There is already a memory problem (like with images) and this was just the last allocation that took the app past its limits.

    How many times has this line of code been the culprit of this problem? If the answer is many then 1) or 2) look likely. Since we do not have all the apps code it is hard to diagnose, but look to the event/circumstance that triggers this code -- is it getting called from a touch or move -- and if so is this happening every millisecond as the user drags their finger across the screen because there is no sleep in the onTouch loop? This is the kind of thing that could vary from platform to platform.

    If this line of code has only caused the problem once or twice it may be time to look for a general memory usage problem. Try to determine how close to the memory limit the application is. The allowance varies with Android version, so this may be another reason why you don't see it in the emulator. Try to test with the same Android version that is experiencing the problem.

    0 讨论(0)
  • 2021-02-14 01:31

    In case of java.lang.OutOfMemoryError it's likely that your application keeps references to objects which should be freed. Try to debug this application on this phone, not in emulator.

    0 讨论(0)
提交回复
热议问题