Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jvmti GetThreadGroupChildren allocates more memory than necessary #15706

Open
EricYangIBM opened this issue Aug 12, 2022 · 3 comments
Open

Jvmti GetThreadGroupChildren allocates more memory than necessary #15706

EricYangIBM opened this issue Aug 12, 2022 · 3 comments

Comments

@EricYangIBM
Copy link
Contributor

GetThreadGroupChildren returns two arrays that may allocate more memory than needed.

/* The groups array may have unused space as weakGroup may not be added. */
if (NULL != weakGroup) {
groups[i] = (jthreadGroup)vmFuncs->j9jni_createLocalRef((JNIEnv *)currentThread, weakGroup);
i += 1;
}

/* The threads array will have unused space since many threads will not belong to the given threadgroup. */
if (threadGroup == threadGroupObject) {
*currentThreadPtr++ = (jthread)vmFuncs->j9jni_createLocalRef((JNIEnv *)currentThread, threadObject);
nLiveThreads += 1;
}

See #15539 (comment)

@babsingh
Copy link
Contributor

Another place where more memory is used. Only live threads are stored in the array. But, the array size is based upon all threads in the threadGroupObject.

jint i = 0;
j9object_t childrenThreads = (j9object_t)J9VMJAVALANGTHREADGROUP_CHILDRENTHREADS(currentThread, threadGroupObject);
jint numLiveThreads = 0;
/* Include only live threads in the result */
numLiveThreads = 0;
for (i = 0; i < numThreads; ++i) {
j9object_t thread = J9JAVAARRAYOFOBJECT_LOAD(currentThread, childrenThreads, i);
J9VMThread *targetThread = NULL;
if (JVMTI_ERROR_NONE == getVMThread(currentThread, (jthread)&thread, &targetThread, FALSE, TRUE)) {
threads[numLiveThreads++] = (jthread)vmFuncs->j9jni_createLocalRef((JNIEnv *)currentThread, thread);
releaseVMThread(currentThread, targetThread);
}

@babsingh
Copy link
Contributor

Another location. Only live threads are stored in the array. But, the array size is based on vm->totalThreadCount.

/* Only count live threads */
if (threadObject != NULL) {
if (J9VMJAVALANGTHREAD_STARTED(currentThread, threadObject) && (J9VMJAVALANGTHREAD_THREADREF(currentThread, threadObject) != NULL)) {
*currentThreadPtr++ = (jthread)vm->internalVMFunctions->j9jni_createLocalRef((JNIEnv *)currentThread, (j9object_t)threadObject);
++threadCount;
}

@babsingh
Copy link
Contributor

More memory is used. Tradeoff for reducing memory usage will be more memory operations and processing time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants