1

Background

I'm learning how to use Renderscript, and I found this part in the docs:

http://developer.android.com/guide/topics/renderscript/advanced.html#mem-allocation

To support this memory allocation system, there are a set of APIs that allow the Android VM to allocate memory and offer similar functionality to a malloc call.

...

To better understand how these classes work, it is useful to think of them in relation to a simple malloc call that can look like this:

array = (int *)malloc(sizeof(int)*10);

The problem

I'm failing to understand something: What if I want to allocate memory within RS itself?

For example, suppose I need to have a calculation that requires a lot of memory compared to the input and output, can I do it all inside RS itself, instead of from Java?

What I've tried

I can't even succeed in creating a new array, even if it's in constant size:

static int array[1024*1024*100] ; //this is ok

void init() {
   rsDebug("init called. array check:",(int)(*array));
   array[0] = 2; // this is not ok
   rsDebug("init called. array check:",(int)(*array));
}

And all I get for this code is this error:

Error:Execution failed for task ':app:compileDebugRenderscript'.

com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'D:\android\Sdk\build-tools\22.0.1\llvm-rs-cc.exe'' finished with non-zero exit value -1073741819

The questions

  1. Is it possible to dynamically allocate memory on RS ? Instead of doing so from Java ?

  2. If it's possible, is this memory a part of the heap, or a part of the global, available RAM of the device? Will it be auto-GC-ed when the Renderscript object on Java gets GC-ed ?

  3. What about all the static variables that are set inside RS ? Are they a part of the heap?

  4. Would all kinds of allocations of RS be automatically freed like on GC , once the Renderscript object on Java gets GC-ed ?

    I ask this because of some weird explanations on the docs.

android developer
  • 114,585
  • 152
  • 739
  • 1,270

2 Answers2

1

There is no way to allocate memory within a .rs file itself. You need to do all of your allocation within Java/C++, and then pass it into RS.

Stephen Hines
  • 2,612
  • 1
  • 13
  • 12
  • How come? It's a very basic thing to have... And how do you pass C/C++ allocated memory to it? Is it part of the heap, or part of the total memory of the device? – android developer Oct 05 '15 at 21:20
  • 1
    Allocating memory is time-consuming and not something we currently enable. You need to create memory using the Allocation class in either Java or C++ (http://developer.android.com/reference/android/renderscript/Allocation.html). – Stephen Hines Oct 05 '15 at 22:15
  • Sometimes I need to create a large chunk of memory. I can't even create an array in RS ? Why should I need to use JNI to do this (as the heap might not be enough and could cause OOM) ? I could initialize the RS and only later do the heavy computation. Would you please consider adding this? I also think that allocating the memory from outside of RS would be much more time consuming (for both the app and developer) than just do it inside RS – android developer Oct 06 '15 at 16:23
0

No, it's not possible using malloc(); you are mis-reading the documentation.

It doesn't say the API is malloc(), it says it's like malloc(). The very same page you linked goes on to show this example code:

//Create an element manually and allocate memory for the int pointer
intPointer = Allocation.createSized(myRenderScript, Element.I32(myRenderScript), 2);

Which doesn't cast, but also doesn't look like it's C code.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    That would be not C. Is the question wrongly tagged (which would explain casting `void *`)? – too honest for this site Oct 05 '15 at 13:11
  • ok edited the tags to avoid confusion. How do I allocate stuff inside RS itself? For example, if I want to create a custom sized array, what should I do there? Also, Renderscript it based on C99 : https://en.wikipedia.org/wiki/C99 – android developer Oct 05 '15 at 13:18