Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Sending data through WWWForm

Discussion in 'Web' started by jvo3dc, Mar 15, 2018.

  1. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    I'm trying to use Texture2D.EncodeToJPG in a WebGL build and I'm getting memory exceptions. I already added 256 MB of memory to the player with no effect, so the actual memory doesn't seem the issue.

    Has anyone used this function with success in a WebGL build?

    Edit: This is not the problem, it's WWWForm, read below
     
    Last edited: Mar 15, 2018
  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    could you post a stacktrace from a development build?
     
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Here you are, but I can't make much out of it:
    uncaught exception: abort("Cannot enlarge memory arrays. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value 536870912, (2) compile with -s ALLOW_MEMORY_GROWTH=1 which adjusts the size at runtime but prevents some optimizations, (3) set Module.TOTAL_MEMORY to a higher value before the program runs, or if you want malloc to return NULL (0) instead of this abort, compile with -s ABORTING_MALLOC=0 ") at jsStackTrace@blob:http://flexmo3d.de/7ef81c6d-04a3-4a69-a2dd-b66902fbc10a:1154:12 stackTrace@blob:http://flexmo3d.de/7ef81c6d-04a3-4a69-a2dd-b66902fbc10a:1168:11 abort@blob:http://flexmo3d.de/7ef81c6d-04a3-4a69-a2dd-b66902fbc10a:19153:43 abortOnCannotGrowMemory@blob:http://flexmo3d.de/7ef81c6d-04a3-4a69-a2dd-b66902fbc10a:1210:2 enlargeMemory@blob:http://flexmo3d.de/7ef81c6d-04a3-4a69-a2dd-b66902fbc10a:1213:2 _sbrk@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1691423:1 _GC_unix_sbrk_get_mem@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1730035:1 _GC_expand_hp_inner@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1278919:1 _GC_collect_or_expand@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1198683:1 _GC_alloc_large@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1555883:1 _GC_generic_malloc@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1518219:1 _GC_malloc_atomic@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1676831:1 __ZN6il2cpp2vm6Object15AllocatePtrFreeEjP11Il2CppClass [il2cpp::vm::Object::AllocatePtrFree(unsigned int, Il2CppClass*)]@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1659318:1 __ZN6il2cpp2vm6String7NewSizeEi [il2cpp::vm::String::NewSize(int)]@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1619065:1 __ZN6il2cpp6icalls8mscorlib6System6String19InternalAllocateStrEi [il2cpp::icalls::mscorlib::System::String::InternalAllocateStr(int)]@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1831921:1 _String_Concat_m2596409543@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1254877:1 _VMFAssetLoader_GetFormData_m3703292816@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1345112:1 _VMFAssetLoader_GetHash_m4240188214@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1644405:1 dynCall_iiiii@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1806502:1 invoke_iiiii@blob:http://flexmo3d.de/7ef81c6d-04a3-4a69-a2dd-b66902fbc10a:15631:10 _VMFAssetLoader_Load_m1838932651@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1036288:1 _VMFAssetLoader_Load_m1202532006@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1608391:1 _KOGLServerXML_Load_m3803997860@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1274881:1 _KOGLGUIMain_SaveScreenshot_m372743406@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1541182:1 _KOGLGUISideTools_SetOffer_m4187775824@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1226624:1 _KOGLGUISide_SetOffer_m3769513517@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1800672:1 _KOGLGUIMain_SetOffer_m2229651077@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1432481:1 _KOGLOfferCreateAuthorized_Update_m519014630@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1750429:1 __Z31RuntimeInvoker_Void_t1841601450PK10MethodInfoPvPS2_ [RuntimeInvoker_Void_t1841601450(MethodInfo const*, void*, void**)]@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1809371:1 dynCall_iiii@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1821462:1 invoke_iiii@blob:http://flexmo3d.de/7ef81c6d-04a3-4a69-a2dd-b66902fbc10a:16319:10 __ZN6il2cpp2vm7Runtime6InvokeEPK10MethodInfoPvPS5_PP15Il2CppException [il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**)]@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1550359:1 _il2cpp_runtime_invoke@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1736118:1 __Z23scripting_method_invoke18ScriptingMethodPtr18ScriptingObjectPtrR18ScriptingArgumentsP21ScriptingExceptionPtrb [scripting_method_invoke(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingArguments&, ScriptingExceptionPtr*, bool)]@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1628244:1 __ZN19ScriptingInvocation6InvokeEP21ScriptingExceptionPtrb [ScriptingInvocation::Invoke(ScriptingExceptionPtr*, bool)]@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1100105:1 __ZN13MonoBehaviour16CallUpdateMethodEi [MonoBehaviour::CallUpdateMethod(int)]@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:963200:1 __ZN13MonoBehaviour6UpdateEv [MonoBehaviour::Update()]@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1850306:1 __ZN20BaseBehaviourManager12CommonUpdateI16BehaviourManagerEEvv [void BaseBehaviourManager::CommonUpdate<BehaviourManager>()]@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:900050:1 __ZN16BehaviourManager6UpdateEv [BehaviourManager::Update()]@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1617685:1 __ZZ23InitPlayerLoopCallbacksvEN36UpdateScriptRunBehaviourUpdatestruct7ForwardEv [InitPlayerLoopCallbacks()::UpdateScriptRunBehaviourUpdatestruct::Forward()]@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1788927:1 __Z10PlayerLoopv [PlayerLoop()]@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:486958:1 __ZL8MainLoopv [MainLoop()]@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1248360:1 dynCall_v@blob:http://flexmo3d.de/ade773b9-1339-4db0-ab32-d1f4ecdc7936:1863673:1 browserIterationFunc@blob:http://flexmo3d.de/7ef81c6d-04a3-4a69-a2dd-b66902fbc10a:2389:4 runIter@blob:http://flexmo3d.de/7ef81c6d-04a3-4a69-a2dd-b66902fbc10a:2491:5 Browser_mainLoop_runner@blob:http://flexmo3d.de/7ef81c6d-04a3-4a69-a2dd-b66902fbc10a:2427:3

    In the sequence of things, I'm starting to become unsure it's actually EncodeToJPG. It might only happen when trying to send the encoded image through a http post. I'm going to make step by step builds:
    - Convert the current RenderTexture to a Texture2D (test 1, no problem here)
    - Convert this to a JPG byte array (test 2, no problem here)
    - Send a 1 byte dummy array through http post (test 3, no problem here)
    - Send a 1.000.000 byte dummy array through http post (test 4, here's the problem)
    - Send a the actual JPG byte array through http post

    For the record, the whole screenshot function is only executed once before the out of memory exception is thrown. And we're talking Unity 5.6.3f1.

    Edit: So the issue is actually sending data through WWWForm. If I try to send 1 byte, there is no problem. Sending 1 MB requires so much extra memory that adding 256 MB to the player is not enough. The actual image I'm trying to send is about 50 KB and even then 256 MB of memory is not enough.
     
    Last edited: Mar 15, 2018
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    I just tried a backup option of putting the binary data in a hexadecimal string, but that leads to the same memory issue. So it seems a general issue with WWWForm.

    And after some additional tests it seems the problem is not about adding it to the WWWForm, but sending it.
     
    Last edited: Mar 15, 2018
  5. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Ok, currently trying to work around it by doubling the memory use to 512 MB and adjusting the compression quality to make the image fit in 10 KB. But I'd still say this is a bug, using about 25 KB for every byte sent through WWWForm.

    It actually seems like the memory use is squared. Where sending a 10 KB image with 256 MB of additional memory works, sending a 20 KB image with 512 MB of additional memory still leads to a memory exception.

    Edit: Well, was trying to fit it in 10 KB, but that doesn't generally work even on quality 0, so there is no workaround besides having the WebGL version use 8 GB of memory. (Or sending the data in many small chunks.)
     
    Last edited: Mar 16, 2018