Skip to content

Conversation

@coleenp
Copy link
Contributor

@coleenp coleenp commented May 16, 2025

This change uses a ConcurrentHashTable to associate Method* with jmethodID, instead of an indirection. JNI is deprecated in favor of using Panama to call methods, so I don't think we're concerned about JNI performance going forward. JVMTI uses a lot of jmethodIDs but there aren't any performance tests for JVMTI, but running vmTestbase/nsk/jvmti with in product build with and without this change had no difference in time.

The purpose of this change is to remove the memory leak when you unload classes: we were leaving the jmethodID memory just in case JVMTI code still had references to that jmethodID and instead of crashing, should get nullptr. With this change, if JVMTI looks up a jmethodID, we've removed it from the table and will return nullptr. Redefinition and the InstanceKlass::_jmethod_method_ids is somewhat complicated. When a method becomes "obsolete" in redefinition, which means that the code in the method is changed, afterward creating a jmethodID from an "obsolete" method will create a new entry in the InstanceKlass table. This mechanism increases the method_idnum to do this. In the future maybe we could throw NoSuchMethodError if you try to create a jmethodID out of an obsolete method and remove all this code. But that's not in this change.

Tested with tier1-4, 5-7.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8268406: Deallocate jmethodID native memory (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/25267/head:pull/25267
$ git checkout pull/25267

Update a local copy of the PR:
$ git checkout pull/25267
$ git pull https://git.openjdk.org/jdk.git pull/25267/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25267

View PR using the GUI difftool:
$ git pr show -t 25267

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/25267.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented May 16, 2025

👋 Welcome back coleenp! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented May 16, 2025

@coleenp This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8268406: Deallocate jmethodID native memory

Reviewed-by: dholmes, sspitsyn, dcubed, eosterlund, aboldtch

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 203 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk
Copy link

openjdk bot commented May 16, 2025

@coleenp The following labels will be automatically applied to this pull request:

  • hotspot
  • serviceability

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added serviceability serviceability-dev@openjdk.org hotspot hotspot-dev@openjdk.org labels May 16, 2025
@coleenp coleenp changed the title 8305626: Kitchensink7D.java failing because of native memory exhausting with ZGC 8268406 Deallocate jmethodID native memory May 16, 2025
@coleenp coleenp changed the title 8268406 Deallocate jmethodID native memory 8268406: Deallocate jmethodID native memory May 16, 2025
@coleenp coleenp force-pushed the jmethodid-cache branch 2 times, most recently from 334bdb9 to 8aa69f5 Compare May 29, 2025 19:42
@coleenp coleenp marked this pull request as ready for review June 12, 2025 12:41
@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 12, 2025
@mlbridge
Copy link

mlbridge bot commented Jun 12, 2025

@coleenp
Copy link
Contributor Author

coleenp commented Jun 12, 2025

Also, this was @fisk 's idea.

@dcubed-ojdk
Copy link
Member

dcubed-ojdk commented Jun 13, 2025

JNI is deprecated in favor of using Panama to call methods, so I don't think we're concerned about performance going forward.

s/concerned about performance/concerned about JNI performance/

@dcubed-ojdk
Copy link
Member

In the future maybe we could throw NoSuchMethodError if you try to create a jmethodID out of an obsolete method and remove all this code. But that's not in this change.

Hmmm... I'm rusty with redefinition, but I think there are legitimate scenarios where is "okay"
to call an obsolete method. I believe it has to do with an in-progress call to a method being
redefined and you might get the obsolete method or the new method depending on timing.

@coleenp
Copy link
Contributor Author

coleenp commented Jun 13, 2025

There is a way to create a jmethodID from an obsolete method and I thought we had tests that did this. I'm not finding them right now. The old and obsolete methods can still be running after a redefinition if they were running during the redefinition. The JIT deoptimizes them but they will be run in the interpreter. New invocations of the method will choose the new version of the method always. Always unless there's a bug that we don't know about. We have fixed a few old method invocations in the past coming from various places in the JVM but we fixed the "last" one fairly recently.
But technically, one could create a jmethodID to an obsolete method although it's not easy to do. I was musing above but not committed to breaking compatibility if there is code that requires this.

@coleenp
Copy link
Contributor Author

coleenp commented Jun 16, 2025

Dan, thank you for your first pass. I've tried to address the things you pointed out. I think I've used 'null' correctly in comments and strings though.

Copy link
Member

@dcubed-ojdk dcubed-ojdk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thumbs up with the latest version!

Thanks for fixing all the nits.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 16, 2025
Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. I'm still digesting it.

Thanks

int new_jmeths = 0;
void InstanceKlass::make_methods_jmethod_ids() {
MutexLocker ml(JmethodIdCreation_lock, Mutex::_no_safepoint_check_flag);
jmethodID* jmeths = methods_jmethod_ids_acquire();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically you don't need acquire semantics here as this value is not used to then access other data. But I see this is the only getter API available.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this does need an acquire getter outside the lock. It's better to not have a non-acquire version to be possibly used by accident.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acquire is only ever needed outside the lock. I don't like there only being acquire/release available generally speaking because it just confuses what memory operations are being synchronized.

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Jun 17, 2025
Copy link
Contributor

@sspitsyn sspitsyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really great optimization and refactoring!
Looks pretty good to me but I'd like to make one more pass through the changes.

Copy link
Member

@xmas92 xmas92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks.

Feels like JmethodIdCreation_lock might not be needed now but it is also easier to reason about things when we serialize the modifications.

@fisk
Copy link
Contributor

fisk commented Jun 24, 2025

the cls parameter is never actually used. So while it is supposed to refer to the class you have the static method jMethodID for, there is no requirement that it actually does, and could even be null.

Not passing in the cls parameter, would be a clear user error though, right? And one that would have crashed before, because if you racingly execute bytecodes of a class that is being unloaded, things would blow up one way or another. To me it seems like the user should just pass in the class as intended and then all is good.

@coleenp
Copy link
Contributor Author

coleenp commented Jun 24, 2025

Thank you for reviewing from the GC and class unloading perspective, Axel and Erik.

@coleenp
Copy link
Contributor Author

coleenp commented Jun 24, 2025

Thank you Serguei and Dan for reviewing.

@dholmes-ora
Copy link
Member

the cls parameter is never actually used. So while it is supposed to refer to the class you have the static method jMethodID for, there is no requirement that it actually does, and could even be null.

Not passing in the cls parameter, would be a clear user error though, right? And one that would have crashed before, because if you racingly execute bytecodes of a class that is being unloaded, things would blow up one way or another. To me it seems like the user should just pass in the class as intended and then all is good.

The point is to try and make things more robust when the user does the unexpected. As Coleen stated we are trying to handle cases where JNI code looks up a jMethodID in one place, stashes it away and then uses it elsewhere with no guarantee the class is being kept alive. I agree you would think they would have, and pass in, the original jclass reference, but the fact we don't actually use that value is not hard to determine and JNI code can take advantage of that - treating the jMethodId as an effective raw pointer to a method.

@dholmes-ora
Copy link
Member

The jmethodID table is so that jmethodID isn't a stale pointer itself and doesn't require us to hold a stale pointer, but whether it can return a stale Method* (now and before this change) is something we should figure out how it should work.

That's fine. I (per my response to Thomas) thought the new approach also closed the door on unsafe usage of the jMethodID, but that is not the case. It probably does close the door quite a bit in relation to current approach though.

Copy link
Member

@dcubed-ojdk dcubed-ojdk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made another pass through this PR. Just a few more nits.

@dcubed-ojdk
Copy link
Member

Another way to look at the safety of the new mechanism is to
consider the use of jmethodIDs from the AsyncGetCallTrace API:

src/hotspot/share/prims/forte.cpp:

108 2025.06.25 12:09:14 $ cat /tmp/fred
// Forte Analyzer AsyncGetCallTrace() entry point. Currently supported
// on Linux X86, Solaris SPARC and Solaris X86.
//
// Async-safe version of GetCallTrace being called from a signal handler
// when a LWP gets interrupted by SIGPROF but the stack traces are filled
// with different content (see below).
//
// This function must only be called when JVM/TI
// CLASS_LOAD events have been enabled since agent startup. The enabled
// event will cause the jmethodIDs to be allocated at class load time.
// The jmethodIDs cannot be allocated in a signal handler because locks
// cannot be grabbed in a signal handler safely.
//
// void (*AsyncGetCallTrace)(ASGCT_CallTrace *trace, jint depth, void* ucontext)
//
// Called by the profiler to obtain the current method call stack trace for
// a given thread. The thread is identified by the env_id field in the
// ASGCT_CallTrace structure. The profiler agent should allocate a ASGCT_CallTrace
// structure with enough memory for the requested stack depth. The VM fills in
// the frames buffer and the num_frames field.
//
// Arguments:
//
//   trace    - trace data structure to be filled by the VM.
//   depth    - depth of the call stack trace.
//   ucontext - ucontext_t of the LWP
//
// ASGCT_CallTrace:
//   typedef struct {
//       JNIEnv *env_id;
//       jint num_frames;
//       ASGCT_CallFrame *frames;
//   } ASGCT_CallTrace;
//
// Fields:
//   env_id     - ID of thread which executed this trace.
//   num_frames - number of frames in the trace.
//                (< 0 indicates the frame is not walkable).
//   frames     - the ASGCT_CallFrames that make up this trace. Callee followed by callers.
//
//  ASGCT_CallFrame:
//    typedef struct {
//        jint lineno;
//        jmethodID method_id;
//    } ASGCT_CallFrame;
//
//  Fields:
//    1) For Java frame (interpreted and compiled),
//       lineno    - bci of the method being executed or -1 if bci is not available
//       method_id - jmethodID of the method being executed
//    2) For native method
//       lineno    - (-3)
//       method_id - jmethodID of the method being executed

The AsyncGetCallTrace API returns jmethodIDs without explicitly
associated jclass references. The AsyncGetCallTrace API relies on
JVM/TI CLASS_LOAD events to have been enabled since agent startup for
jmethodID creation. Implicit in that requirement is the agent keeping a
jclass reference for each CLASS_LOAD event so that the class cannot be
unloaded while the data returned by AsyncGetCallTrace is processed.

@dcubed-ojdk
Copy link
Member

Ping @jbachorik for discussions about the safety of jmethodIDs...

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Jun 26, 2025
Copy link
Member

@dcubed-ojdk dcubed-ojdk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making the wording tweaks.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 26, 2025
@coleenp
Copy link
Contributor Author

coleenp commented Jun 26, 2025

// This function must only be called when JVM/TI
// CLASS_LOAD events have been enabled since agent startup. The enabled
// event will cause the jmethodIDs to be allocated at class load time.
// The jmethodIDs cannot be allocated in a signal handler because locks
// cannot be grabbed in a signal handler safely.

I do not see any code that creates jmethodIDs for all the methods in a ClassLoad JVMTI event. ACGCT does call

trace->frames[count].method_id = method->find_jmethod_id_or_null();

so it won't create jmethodIDs from a signal handler. Keeping them live for the frames depends on when the native caller of ASGCT uses the trace that is returned. If the frames are still on the call stack, the methods cannot be unloaded because they're still on the call stack. If the frames returned by ASGCT are stored somewhere else, and accessed later, they could be null. I don't see any code that makes this work tbh. Most methods do not have jmethodIDs.

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Jun 26, 2025
Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still fine

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 27, 2025
@coleenp
Copy link
Contributor Author

coleenp commented Jun 27, 2025

Thank you for the reviews, Serguei, David, Dan, Axel and Erik. And all the discussion, and the idea itself.
/integrate

@openjdk
Copy link

openjdk bot commented Jun 27, 2025

Going to push as commit d8f9b18.
Since your change was applied there have been 208 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jun 27, 2025
@openjdk openjdk bot closed this Jun 27, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jun 27, 2025
@openjdk
Copy link

openjdk bot commented Jun 27, 2025

@coleenp Pushed as commit d8f9b18.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

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

Labels

hotspot hotspot-dev@openjdk.org integrated Pull request has been integrated serviceability serviceability-dev@openjdk.org

Development

Successfully merging this pull request may close these issues.

7 participants