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 7081739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:57:31+00:00 2026-05-28T06:57:31+00:00

We have the concept of pointers in C++. Now if we allocate some memory

  • 0

We have the concept of pointers in C++. Now if we allocate some memory in C++ and pass it on to Java as an object reference(using JNI) then who should be and who will be freeing it.

Will it be

1.)The Garbage collector does it automatically in Java?

2.)We need to explicitly do a delete on the pointer in the wrapped JNI class finalize method?

3.)Or we should just forget finalize(as finalizers cannot be trusted) and it is responsibility of Java to call a C++ code which deletes the object

4.)Or is there some way to deallocate the memory directly in Java itself (not sure how Java intreprets a C++ pointer inorder to delete it)?

What is the best practice for doing this and vice versa(when we pass objects from Java to C++)?

  • 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-28T06:57:32+00:00Added an answer on May 28, 2026 at 6:57 am

    We have the concept of pointers in C++. Now if we allocate some memory in C++ and pass it on to Java as an object reference(using JNI) then who should be and who will be freeing it.

    The best strategy is usually to have the allocator also be the one to free the data.

    1.)The Garbage collector does it automatically in Java?

    The problem with this is you don’t know when, if ever it will run.

    2.)We need to explicitly do a delete on the pointer in the wrapped JNI class finalize method?

    Better to have a release() method in Java rather than imply that C++ has to delete it. You may want C++ to recycle the memory.

    3.)Or we should just forget finalize(as finalizers cannot be trusted) and it is responsibility of Java to call a C++ code which deletes the object

    If you mean, allocate the memory in Java and pass it to C++ to populate. This is my preference.

    I would use can use ByteBuffer.allocateDirect() and you can call ((DirectBuffer) buffer).cleaner().clean(); to clean it up deterministically.

    This can make recycling the memory simpler, possibly the same buffer can be used for the life of the application.

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

Sidebar

Related Questions

Java does not have the concept of pointers . So how does java implement
Does Java and/or Spring have the concept of properties? I have bunch of domain
I have written some sample program and DLL to learn the concept of DLL
I have a small confusion in the dynamic memory allocation concept. If we declare
I have a Java Applet that I'm making some edits to and am running
I have read there is no pointer concept in Java, however I have also
As python does not have concept of constants, would it be possible to raise
I have the concept of NodeType s and Node s. A NodeType is a
Many websites have the concept of sending messages from user to user. When you
Sybase db tables do not have a concept of self updating row numbers. However

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.