0

I don't how to ask this question but I want to create an object in a specific address (memory) and with specific size. For example, class A {int, string, class B, List<array>} shouldn't be more than 30 MB otherwise throws an error NoMoreMemoryException

I know there is no pointer in Java (even if C++ hackers insist) but how to point to a specific address?

  • Do you always know what's in the list? Otherwise it's hard to know how much memory an instance of this class will take. Maybe look at http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object – sjr Jan 07 '11 at 06:21
  • @sjr let's say int array. the algorithm is important to me –  Jan 07 '11 at 06:22
  • if you can use an int array then the link i posted below should allow you to do what you need, provided the memory you want to write to is controlled by the JVM. – Nick Jan 07 '11 at 06:25
  • @nick thanks Nick. I'll appreciate this –  Jan 07 '11 at 06:26

6 Answers6

1

an important aspect of the JVM is that memory is abstracted. You really can't point to a specific address in memory.

You can get the size of an object in memory. Look here for details

Community
  • 1
  • 1
Ryan Fernandes
  • 8,238
  • 7
  • 36
  • 53
1

Write a JNI code and call thru java code.Please look at the source code of the Object class written in c,specifically clone method, which does the bitwise copy in the memory to create clone object. from there you can logic and use it in ur own class to know the size of the each member field attributes.

Dead Programmer
  • 12,427
  • 23
  • 80
  • 112
1

This should get you half-way: http://robaustin.wikidot.com/how-to-write-to-direct-memory-locations-in-java

Like the article says though, the JVM needs to control the memory to get access.

Another more powerful option would be to use JNI and do what you need to do in C or C++.

Nick
  • 8,181
  • 4
  • 38
  • 63
1

There's no way you can do that in pure Java. If, for some reason you have a requirement for doing exactly that, you should use JNI/C.

As a note, the toString() method (depending in the implementation) sometimes can give you the initial address where an object is created, but it can be moved by the JVM in any time.

Marcelo
  • 2,232
  • 3
  • 22
  • 31
1

I assume you are getting an OutOfMemoryError, perhaps because your -mx or -Xmx setting it too low.

Its not very clear what your data structure is or why you believe it shouldn't be 30 MB, however 30 MB is a fairly trivial amount of memory in a computer these days. A server with 8 GB costs about £850. Are you programming for a mobile device?

The best way to see how much a structure is using and why is to use a memory profiler. VisualVM is a profiler which comes with Java. YourKit is a commercial profiler which I find better.

It is quite likely you are not using the most memory efficient structure, but then again it quite likely it shouldn't make much difference.

Perhaps if you could clarify what you are doing when could suggest a better solution.

In Sun/Oracle's JVM you can point to a specific location in memory and allocate/free memory like in C, but it is highly unlikely to be a good idea. ;)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • just curiosity :) I'm amateur thinkerer –  Jan 07 '11 at 07:55
  • @hilal, Switching from C to Java its hard to let go of the idea that you don't need to know what your pointers are doing and what the size of a structure is. ;) – Peter Lawrey Jan 07 '11 at 08:21
0

I know this post is really old but I thought I comment it anyway :-)

You can actually access the Memory as long as it is managed by the JVM you could use the sun.misc.Unsafe which brings some special methods which allow the user accessing the memory directly.

Be advised, this is a really dangers Class that´s why it is not only not documented, the constructors are also private, which means you have to use Reflection.

You can do some nice things with it. enjoy....

pbertsch
  • 320
  • 2
  • 14