How to prevent fragmentation in Java? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an interview and I was ask how to prevent Memory Fragmentation in Java. Can Anyone summarize how Java deal with it ? I've read some documents but it very complicated.

The short answer is: you use a generational garbage collector.
Except that most modern Java implementations1 use a generational collector by default. In other words, you (the programmer) don't need to deal with the problem because it is already dealt with.
The only situation where you might run into fragmentation is if you have mis-tuned the low-pause collector and it has fallen back to doing a "stop the world" full collection. At that point, you might be using a non-compacting collector, and fragmentation might ensue. But the solution there is to adjust the tuning (or the application, or the heap size) to avoid getting into that situation.
If you want more information about garbage collectors work, there are whole textbooks on the subject. And the various Java collectors are covered in depth in various articles and published research papers.
I've read some documents but it very complicated.
Yes, it is. It is hard work becoming an expert.
1 - Some really old Java implementations used primitive mark-sweep collectors. But they are long gone.

Related

Heap allocation source code in in jdk(G1GC) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I want to understand how java allocates objects in heap. I want to know which parts of the JDK code are triggered when the "new" keyword is used.
How can I define a function that a user can call from the java code and that would implement functionality in JDK source code?
I am aware of the fact that jdk14 uses G1GC as a default garbage collector and G1GC code is present in jdk14/src/hotspot/share/GC/G1 folder but I am unable to follow G1Allocator allocates memory to the user threads(if it does).
Any known implementation will use TLAB (thread local allocation buffer) by default when allocating memory. Without it - the allocation would be much more slower. Though I have not dived into the code too much about this subject, you can start from here, for example.
There is a very good comment in the source code about what happens when a new is requested here. Basically if TLAB can be used (an Object is not bigger than that for example), it will be; otherwise raw malloc for every new will be done.
Regarding G1 here are the mains points of what it does. A general explanation is again in the comments, with a phrase :
All non-TLAB allocation requests should go to mem_allocate()
What mem_allocate does can be started from here.

Is creating a lot of instances the reason my program is slowing down? (JAVA GRAPHICS) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm new to working with java graphics and recently i've noticed that the project i'm working on has been slowing down (lagging, dropping frame rate). I think that the reason why is that instead of making an instance of an object and then drawing it repeatedly i've been making new instances each frame and drawing those. I want to make sure that that's the reason before I start reworking all my code.
thank you
That is hard to tell without seeign the code but you should definetly create or update instances only when needed and draw them repeatedly.
I recommend that you profile your code. Use the profiling stats to decide whether your theory is correct or not.
We would still be guessing about the cause of your "lagging", even if we saw the source code. You should be spending time tuning or rewriting your code based purely on someone's guesses.
FWIW, the overheads of object creation are not as large as some people make out. But GC impacts on realtime behavior because even the best (low-pause) Java GCs have phases where they stop all application threads to perform essential tasks.
And this leads us to the fact that you may be able reduce "lag" by simply tuning the JVM's GC settings. (Assuming that you haven't already tried that .....) I would try GC tuning before doing a massive code rewrite.

Does JVM memory overhead scale linearly, or is it constant? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
In my experience, a C program that uses around 10 megabytes of resident memory may use around 40 to 50 MB when translated into Java, and around 100 in Clojure or Scala. My question is whether this JVM memory overhead scales linearly; if the C version used 1 gigabyte, would the Java version require 4 GB? Or is the JVM memory overhead more a constant factor, such that the 1 GB C program might only use 1.5 GB in Java?
I'm aware that I could benchmark this, but I think hearing people's experience regarding JVM memory use in production would be more informative than an artificial benchmark, which could be skewed to favour either result depending on how it was designed.
The overhead is about 10MB + 4xC-memory.
The 10MB is the JVM without anything. Java 7 64bit version uses about this much.
The 4x memory is obviously a "guesstimate" because it depends on which data types you use. If you use 100% references in java they take up about 4 times as much memory. The same difference there is between int and Integer.
If there are a lot of malloc/new in your C code there will be that in Java too, and Java's GC might not run when you want it to, so there's also an overhead of "dead references not yet cleaned up" that depends greatly on things out of your control (GC timing).

Differences between C++11, C# and Java memory models [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I know that C++11 memory model was inspired from Java memory model, but there has to be something that differentiates both these memory models.
Java uses synchronize and mutexes
C++11 uses atomics and mutexes
C# uses volatile
But what are the fundamental differences between these three in terms of multithreading in memory and in terms of read/write accesses for threads? Which memory model is better out of those three models? Can anyone please shed a light on this topic(only the differences) in detailed manner or provide a link that i can refer to? And how efficiently can these implemented on various real time systems?
Thanks in advance!
While this does not quantify the differences between the C++11 memory models, it does go into great detail about the C++11 model, which is the most recently codified, and therefore likely the most modern:
http://herbsutter.com/2013/02/11/atomic-weapons-the-c-memory-model-and-modern-hardware/
Once you understand C++11's model as a starting point, that will give you better tools for asking about other languages.

Is there any ways in C,C++ to control the memory ram,registers according to our need? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Are there any ways in C and C++ to control the memory ram and registers according to our need? Eg. movement of data in ram from one location to other, changing values in registers, etc?
Is it possible in Java???
For memory management you should consider using a Memory Pool. Link.
Though you shouldn't be reinventing the wheel. Use a library instead that provides a clean templated interface to memory pools. Avoid malloc and memcpy as much as possible.
If you wan't to play with the registers you can include assembly code. Link.
I am not sure to understand your question, which is operating system, processor, and compiler specific.
With recent GCC you could do some of it (for instance, reserve registers to avoid them being used). And you could also customize the compiler (e.g. with MELT) to suite more needs. But such a customization means at least weeks of efforts.
You could also make a new backend in GCC (but this means months of work)
And recent standard C++11 library has notably std::allocator and a lot of memory management related stuff.

Categories

Resources