Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7067287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:11:01+00:00 2026-05-28T05:11:01+00:00

I have written a Hello, world program with JNI. Java calls c program with

  • 0

I have written a Hello, world program with JNI. Java calls c program with a string, c program prints that string. Here is the java program

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class JNISample {
    public native void leakMem(String str);

    static {
        System.loadLibrary("sample");
    }


    public static void main(String args[]) throws IOException {
        JNISample sample = new JNISample();
        sample.leakMem("Hello world!");
    }
}

Corresponding c program

#include <stdlib.h>
#include <jni.h>
#include "JNISample.h"

JNIEXPORT void JNICALL Java_JNISample_leakMem(JNIEnv *env, jobject jobj, jstring givenStr) {
    const char *javaStr = env->GetStringUTFChars (givenStr, 0);

    printf("Received String: %s\n", javaStr);

    env->ReleaseStringUTFChars (givenStr, javaStr);
}

When I executed this program with valgrind, with following command

valgrind --trace-children=yes --show-reachable=yes --leak-check=full java -Djava.library.path=. JNISample 10 2> log

I have observed definitely lost bytes in the log, here is the valgrind’s memory summery

LEAK SUMMARY:
==5385==    definitely lost: 5,246 bytes in 36 blocks
==5385==    indirectly lost: 5,072 bytes in 23 blocks
==5385==      possibly lost: 154,317 bytes in 131 blocks
==5385==    still reachable: 6,164,933 bytes in 831 blocks
==5385==         suppressed: 0 bytes in 0 blocks

Why there are definitely lost bytes with such a small program without any memory allocations? Is there any problem with JNI usage or JNI leaks memory? I have used JDK6 on Ubuntu 11.10 64 bit machine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:11:01+00:00Added an answer on May 28, 2026 at 5:11 am

    I cannot answer your question specifically. However, my experience with JNI is that it does a lot of things internally that can confuse valgrind. If the point at which the memory is being lost is inside JNI there wouldn’t be a lot you could do about it, anyway. However, it’s still a pain in the proverbial, since the number of different suppression needed to give you a clean run is likely to be enormous (hundreds).

    Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a simple Hello World program. #include <stdio.h> int main() { printf(Hello
I have written my first Hello World Objective C program. How do I add
i have written the following code to copy a string hello world to another
I have a problem running a simple Hello-world program in C++ on my Windows
suppose I have simple C# code to print HELLO WORLD as shown in here
I would like to see a 'Hello world' program written in C/C++ but made
I have written some console Hello world-like app. and have followed c# cywgwin mono
I am trying to run a hello world program written in javascript in a
I have written simple first stage bootloader which displays Hello world using interrupt to
I'm just learning java and following a book. I have a program written via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.