CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2009
    Location
    Netherlands
    Posts
    91

    Java SizeOf /Instrumentation class

    Hello. I'm looking for a way in java to create a buffer with a fixed memory size. For example I want to create a buffer which can hold 10MB of objects in memory, then when the buffer is full, it will deny any new objects being added to it (and write them to the disk instead). So far I haven't found anything in the Java API that suits my needs, so I decided to write a class myself.

    However I quickly realised there seems to be no standard way of measuring the memory usage of an object in java (like c++ sizeof). I found the java.lang.instrument.Instrumentation class and it seems to be able to do what I want. However it doesn't seem very accurate, as it will round everything up to 8 bytes, so if I have an int and a long in a class, it will just show 8 bytes.

    My current OS is fedora 14 and I'm using the sun JDK implementation (not the OpenJDK one that comes with fedora). I don't know if this matters, but I know that implementation of JVM differs for different systems.

    Is there any way to do what I want, with the accuracy I want?

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Java SizeOf /Instrumentation class

    Java isn't really suited to such direct memory management - in fact it's deliberately designed to avoid it. I suspect you'd be better off using a native language. If you have to have a Java front-end, use the JNI to interface with native code.

    If the only tool you have is a hammer, you tend to see every problem as a nail...
    A. Maslow
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Apr 2009
    Location
    Netherlands
    Posts
    91

    Re: Java SizeOf /Instrumentation class

    Quote Originally Posted by dlorde View Post
    Java isn't really suited to such direct memory management - in fact it's deliberately designed to avoid it. I suspect you'd be better off using a native language. If you have to have a Java front-end, use the JNI to interface with native code.

    If the only tool you have is a hammer, you tend to see every problem as a nail...
    A. Maslow
    If only we had such liberties in a business environment, then I would be using C++. I'm afraid use of Java is a requirement. I was kinda afraid of this response. I might have to just estimate the mem usage or throw the whole idea out the window and just make sure that the system that runs my tool will have enough memory assigned to the JVM :P.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Java SizeOf /Instrumentation class

    I guess you could make an empirical assessment of the amount of memory occupied by a certain number of objects, by reading in a large number of objects multiple times and tracking memory usage via a profiler. However, whether you could trust the results to be reliable on other hardware, OS's, or JVMs, I can't say. It may be a case of 'suck it and see'.

    It is easier to measure something than to understand what you have measured...
    Anon.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured