This discussion is archived
1 2 Previous Next 17 Replies Latest reply: Sep 7, 2011 1:31 PM by jschellSomeoneStoleMyAlias RSS

JNI_CreateJavaVM on linux destroys stack???

691149 Newbie
Currently Being Moderated
This is happening on linux 2.6.18-238.5.1.el5 with a 64 bit app. My process stack size is 10MB. However, after a (successful) call to JNI_CreateJavaVM I only seem to have 1-2 MB left on the stack. If I got past it - I get memory fault as if I'm overflowing the stack.
A few notes:
1. If I DON'T create a JVM then I get have access to the whole my my 10MB stack.
2. The same test program with the same makefile runs fine on Solaris even with a call to JVM

#include <jni.h>
#include <stdio.h>
#include <stdlib.h>

void CreateVM(JavaVM ** jvm) {

JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options[1];
options[0].optionString = (char*)"-Xcheck:jni";

vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 0;
vm_args.options = options;
vm_args.ignoreUnrecognized = 0;

int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);
if(ret < 0) {
printf("\nUnable to Launch JVM\n");
exit(1);
}

if ( env->ExceptionCheck() == JNI_TRUE ) {
printf("exception\n");
exit(1);
}
}


void f() {
printf("inside...\n");
//eat up a few megs of stack
char stackTest[0x2FFFFF];
printf("...returning");
}

int main(int argc, char* argv[]) {
JavaVM * jvm;
CreateVM(&jvm);

f();

printf("exiting...\n");
return 0;
}


Build command:
g++ -m64 CTest.cpp -I/import/bitbucket/JDK/jdk1.6.0_26/include -I/import/bitbucket/JDK/jdk1.6.0_26/include/linux -L/import/bitbucket/JDK/jdk1.6.0_26/jre/lib/amd64 -L/import/bitbucket/JDK/jdk1.6.0_26/jre/lib/amd64/server -ljava -ljvm
  • 1. Re: JNI_CreateJavaVM on linux destroys stack???
    691149 Newbie
    Currently Being Moderated
    Anybody? It should be very simple to reproduce, if you got linux and g++. Just build the test program using the included build and run.
  • 2. Re: JNI_CreateJavaVM on linux destroys stack???
    EJP Guru
    Currently Being Moderated
    What makes you think anybody is going to do that? Are you paying?

    What you get here is information. Not fault reproduction and analysis and diagnosis and repair.

    I think you have this forum confused with a support contract.
  • 3. Re: JNI_CreateJavaVM on linux destroys stack???
    691149 Newbie
    Currently Being Moderated
    Hey, thanks for taking your time to respond. And you did this for free!
  • 4. Re: JNI_CreateJavaVM on linux destroys stack???
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    Although 64 bit platforms are becoming more popular they are not that universal.

    And you are asking for someone to test with JNI, which most people don't use.

    Additionally you don't just have "linux" but rather a specific vendor/version of linux. Which as far as I can see you have not specified.

    Then there is just whether anyone wants to take the time to try this specific test and who sees this thread. And there are just not that many people that come to this site (lot more before when it was Sun.)

    So a very small pool of those that are even possible.
  • 5. Re: JNI_CreateJavaVM on linux destroys stack???
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    Might note as well that there are only two possibilities.

    1. Something is wrong with the VM so you will need to find another way to solve your problem because waiting for Oracle to fix it isn't going to work.

    2. Something is wrong with your environment and that causes the problem. If you can't figure out the environment problem then you will need to find another way to solve your problem.

    If someone else runs the test it only validates which one it is.
  • 6. Re: JNI_CreateJavaVM on linux destroys stack???
    691149 Newbie
    Currently Being Moderated
    I came across a few articles that seem to confirm that indeed, the JVM (on Linux) restricts the main thread stack size to 2 MB:

    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4689767
         
    http://www.coderanch.com/t/272944/java/java/JNI-stack-size

    If that's the case then we'll have nothing but to go through the application and trim the stack usage. This is not going to be easy since we have been running the app on Solaris and the 8MB stack for years and only just ported to Linux.

    Btw. I'm not really asking anybody to wrestle with my piece of code and try to reproduce the problem for me if they don't feel like doing it. Rather, looking at this from my own perspective, such small, concise, easy testcases like this one make for fun puzzles and for lack of a better phrase, intellectual feats. That's all I meant to point out.

    I'm running Linux Red Hat Enterprise Linux Server release 5.6 (Tikanga).
  • 7. Re: JNI_CreateJavaVM on linux destroys stack???
    EJP Guru
    Currently Being Moderated
    we'll have nothing but to go through the application and trim the stack usage
    Untrue. Just change your main() method to start another thread, that does what the main() method used to do.
  • 8. Re: JNI_CreateJavaVM on linux destroys stack???
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    user10623291 wrote:
    Btw. I'm not really asking anybody to wrestle with my piece of code and try to reproduce the problem for me if they don't feel like doing it. Rather, looking at this from my own perspective, such small, concise, easy testcases like this one make for fun puzzles and for lack of a better phrase, intellectual feats.
    I might have tried it myself if I had a 64 bit linux install.
    If that's the case then we'll have nothing but to go through the application and trim the stack usage.
    Myself that would seem like a good idea regardless. I would wonder at why it was done like that in the first place.
  • 9. Re: JNI_CreateJavaVM on linux destroys stack???
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    EJP wrote:
    we'll have nothing but to go through the application and trim the stack usage
    Untrue. Just change your main() method to start another thread, that does what the main() method used to do.
    As I read the first link posted by the user it seems to specifically say that there is no way on linux to get around that.
  • 10. Re: JNI_CreateJavaVM on linux destroys stack???
    EJP Guru
    Currently Being Moderated
    Fair enough but that was as of 2002. Does RH Linux still have this problem? My reading of this suggests that it doesn't.
  • 11. Re: JNI_CreateJavaVM on linux destroys stack???
    691149 Newbie
    Currently Being Moderated
    Thanks for the new thread idea, a simple test shows that it might work. A new thread created after creating the JVM seems to have as much stack as indicated by ulimit -s.
    Yes, indeed, those articles are dated but that's all I've found so far. I would not be surprised if it wasn't fixed. I have to concede that there should rarely be the need for such a huge stack size so the problem may never have been considered a high priority. Still it may be easier to create a new thread than going through zillions of lines of code.
  • 12. Re: JNI_CreateJavaVM on linux destroys stack???
    691149 Newbie
    Currently Being Moderated
    Also, the threading library on linux was fixed and probably a long time ago but this does not mean the jvm limitation has been removed:


    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4689767

    "The pthreads bug causes a SEGV if any
    part of the stack > 2MB is touched; it may be fixed in RH7.3, but the vm
    has to support older versions."
  • 13. Re: JNI_CreateJavaVM on linux destroys stack???
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    user10623291 wrote:
    Thanks for the new thread idea, a simple test shows that it might work. A new thread created after creating the JVM seems to have as much stack as indicated by ulimit -s.
    If it is an OS limit then you can ditch the JNI test entirely.

    You should be able to test in C/C++ by creating a number of threads, allocating space, writing to both ends of the block and running for a bit.
    Try it without threads as well, since the idea is to get an error to show up.
  • 14. Re: JNI_CreateJavaVM on linux destroys stack???
    691149 Newbie
    Currently Being Moderated
    Huh? Ditch JNI test? The problem is all about JNI. And the threading solution is not so simple. The moment I attach to the JVM (which I may have created from a different thread), the moment my stack gets restricted).
    Here is the scenario:
    1. From the processing thread, create a new thread and perform JVM/JNI initialization in the new thread.
    2. Wait for the JNI initialization thread to complete.
    3. Eat into the stack to verify that I can use more than 2 MB of stack.
    4. Call AttachCurrentThread() to allow to make JNI calls from the processing thread.
    5. Eat into the stack as in (3) to see if my stck is OK.
    6. Memory fault.


    Btw. Other than the stack limitation my program works correctly, I can make JNI calls from the processing thread OK, I just only have 2 MB of stack at my disposal.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points