Is it possible to have memory managed by JVMTI Allocate/Deallocate and malloc/free simultaneously in one JVMTI Agent?
I know I can not free memory that was allocated by JVMTI Allocate with free and not use JVMTI Deallocate to free memory that was allocated with malloc.
My guess is that it works because malloc gets memory from the operating system, if necessary and I assume JVMTI Allocate does something similar. From that I conclude that JVMTI Allocate/Deallocate and malloc/free don't interfere with each other.
Background: I have libraries that use malloc/free that I'd like to use inside my JVMTI agent.
CodePudding user response:
Many complex software libraries use their own memory management. The advantage is that - especially for image processing - demands for memory are typically of the same size. In this case, memory is not really freed, just added to a pool and offered when needed next. Custom memory management can also reduce out-of-memory errors as the process memory can be better managed.
I noticed this when developing image processing plugins. I assume it is the same design philosophy for jvmti.
This being said, usage of new, delete or malloc, free are allowed and not a problem. As you have noticed, you cannot mix the custom alloc with regular delete or free. So, you have to make sure that malloc is followed by free and new with delete. Also, any custom alloc will use the custom free. And so on...