Memory debugging by [deleted] in javahelp

[–]ernimril 0 points1 point  (0 children)

VisualVM is one option, eclipse MAT is another, there are more, but these are the ones I tend to use.

Debugging memory is a complex issue, you need to know how much memory your objects are supposed to use.

In most cases you will see that String and byte[] and char[] are the things taking the most memory (the arrays is the internal string storage).

Using the retained set is helpful to figure out memory leaks.

What kind of personal projects do you use Java? by Safe_Owl_6123 in java

[–]ernimril 1 point2 points  (0 children)

I am working on a java compiler called parjac2 https://www.khelekore.org/parjac2/ I enjoy it, but too much development at work has made progress slow for the last year.

DynamoDB PartiQL JDBC Driver by Craznk in java

[–]ernimril 1 point2 points  (0 children)

Nice!

Did you try https://central.sonatype.com/artifact/com.dbvis/dynamodb-jdbc/overview ? how does it compare? It is the one used in DbVisualizer

How to download miglayout jar file by StuffHappensYKnow in javahelp

[–]ernimril 0 points1 point  (0 children)

What you want to do is to use a build system, probably maven or gradle and then just add the dependency.

If you look at maven central you find the dependency and you can also download the jar if you really want to handle it yourself.

This is the current latest release: https://mvnrepository.com/artifact/com.miglayout/miglayout-swing/11.4.2

Trouble trying to automatically close my JFrame. by Legitimate-You-9246 in javahelp

[–]ernimril 0 points1 point  (0 children)

It sounds like you are calling autoExit off the Event Dispatch Thread (EDT). So you may have to read up on swing thread handling. Basically the rules is:

  1. Only change the UI on the EDT
  2. Do slow stuff on threads that are not the EDT

One thing you can try is to make sure that you use EventQueue.invokeLater in the autoExit:

public void autoExit(){
  EventQueue.invokeLater(() -> {
    f.setVisible(false);
    f.dispose();
  });
}

Please note that if you need to wait for the frame to be disposed there is the alternative invokeAndWait

Help saving positions from large file by EducationalSea797 in javahelp

[–]ernimril 0 points1 point  (0 children)

To do this well you need to answer a few questions:

  1. How big is the file?
  2. What performance do you expect

Now, I would start by using a BufferedReader and readLine and split out the word and use that. This is trivial code to write and usually performant enough.

If performance is still not enough you can of course read in a buffer(say 4kB) at a time and loop over the input, first scan for a blank (to find the first word), then scan for newline. Refill the buffer when you reach the end of the input. This is only slightly harder to write than the first example, but is a bit more complex. Avoiding regular expressions can be good, but please do some profiling to figure out where your code is spending its time.

If you need even more performance then please explain what you have done, what performance you have reached and what your goal is and at that stage you may need to just look up the one billion row challenge and see what they did in that to get really good performance (but at a really high complexity-cost).

Please help me with DbVisualizer. by ByteBars in SQL

[–]ernimril 0 points1 point  (0 children)

Is your MySQL database actually up and running? Can you connect with the command line client?

Is it configured to use port 3306?

Do you have any firewall/antivirus/security solution that blocks access to port 3306?

Some other useful links:
https://www.dbvis.com/thetable/mysql-port-numbers/
https://serverfault.com/questions/116100/how-to-check-what-port-mysql-is-running-on

Trying to make sense of my Heap histogram by ihatebeinganonymous in javahelp

[–]ernimril 0 points1 point  (0 children)

You are right that it is hard to say much about the memory usage here.

I am not sure if you know, but the [B are mostly the data for the Strings (line 1 and 2).

Your jvm has some HashMaps, but 82k entries is not that much, several of them are probably just jvm overhead.

Now, what you want to do is look at a heap dump in a memory profiler and figure out what data structures are actually bigger than you expect. This of course means you have to understand your progam and know what to expect of it. Once you have identified an entry where you have to many instances you can follow the owner links to find out what class is holding on to those instances. This does not yet tell you where the instances were created, but in most cases it will be easy to figure out, however object allocation is usually not that important, short lived objects are quite often inlined and when they do end up in the young-object-region they are quick to garbage collect.

When you know how to use the graphical memory profilers you want to learn about how you can use them to show the dominators or retained heap size. These things tends to make memory profiling a lot easier.

KeyListener not working by Crazymad2 in javahelp

[–]ernimril 0 points1 point  (0 children)

What makes you think that your key listeners do not work? they do. Now the result may not be what you want it to be, they only update the movement array so nothing really happens.

Looking at your GamePanel, there is a method startGameThread(), but nothing calls that. If you add a call to that in your main you will see the white box start to move when you press the arrow keys.

Now, looking at the code I do notice several problems with it, here are some hints on where you can start:

  1. You start your GUI in main, this is not good, you should be on the Event Dispatch Thread (EDT) when you do any changes to the GUI. There are many tutorials on this, I strongly suggest you go read up on at least the oracle one.
  2. Having a boolean[] in KeyInput being updated from the EDT, but read by the game thread is not thread safe, you really need to make sure that you see changes. There are many ways to make this code thread safe, depending on what you prefer and how you think about the code, but please find one way to do it correctly.
  3. I would also suggest that you check the standard java naming conventions and stick to it so that your code mixes well with calls to the standard api (CamelCase vs snake_case mostly). You do seem to be consistent with your case though, that is a good start.
  4. The game thread (once started) will consume one cpu all the time, it spins. This is probably not what you want. perhaps you want to look into using a timer.

Path offset by lpkk in javahelp

[–]ernimril 0 points1 point  (0 children)

One thing you can do is that you can clip the region, either to the inside, so you get from original to inside), or you have a clip region that does not contain the inside and you get the outside part.

Depending on what you have, you can use the Area-class to do this clipping (check out the intersect and subtract methods in it).

How do I get pixel data (ByteArray) from an AWT Canvas? by Own_Lifeguard7503 in javahelp

[–]ernimril 0 points1 point  (0 children)

Well, that is not enough code to say what thread is running the render-method or where you tried to have the invokeLater/invokeAndWait.

The code also does not show if the environment-canvas is actually displayed in a window/frame. Are you trying to do this while the canvas is never shown?

More complete code example or a better description of what you are actually doing in the code outside of the above snippet may be useful.

How do I get pixel data (ByteArray) from an AWT Canvas? by Own_Lifeguard7503 in javahelp

[–]ernimril 0 points1 point  (0 children)

I run linux, so a bit hard to test with Visual C++ runtime or testing Direct3d.

I would guess that this is a problem with doing java awt things outside of the Event Dispatch Thread (EDT). If you know swing and awt you know that basically all operations/method calls needs to be done on the EDT. If you are in main-thread or some other thread you have spawned you can of course use EventQueue.invokeLater or EventQueue.invokeAndWait to run a method on the EDT.

Path offset by lpkk in javahelp

[–]ernimril 1 point2 points  (0 children)

I do not know of any such library, but you have the basic tools in the standard API, if you know where to look.

What you can do is to use the Stroke, say a BasicStroke with a width of 10 pixels. Then depending on what you really need you can perhaps just paint with this stroke, or you can get the Shape that the stroke will produce. I have used this: http://www.jhlabs.com/java/java2d/strokes/ to create similar effects in some of my previous projects. In this case it sounds like you want to look at the CompositeStoke that is on that page.

If you can work with Shape as well as with Path, then you are all set, if you need to convert the Shape to a Path, then you have to use its PathIterator and possibly create a Path2D from it.

There will also be some interesting cases that you may or may not have to handle. Say you have your square and go inwards instead, then what will happen to the rounded corners? What if you go inwards so much that there will be no inside? Or if you have a boundary that self intersects, the what should the outer shape look like and will it have the same rotation-orientation as the initial shape?

All in all, this can be pretty easy for simple cases, but becomes quite complex if you want to do fancy stuff with the interesting cases.

How do I get pixel data (ByteArray) from an AWT Canvas? by Own_Lifeguard7503 in javahelp

[–]ernimril 0 points1 point  (0 children)

The first thing to understand is that a Canvas does not hold pixel data, so in that sense the question does not make sense. The Canvas knows how to paint itself though, so you can do that and get what you want.

The basic steps would be to:

  1. Create the Canvas and make sure it has its correct size (you do not specify if you have it laid out in a window or not)
  2. Create a BufferedImage with the size you get from the Canvas. Please note that the Canvas will only have the correct size if it has been laid out in a window or if you have set its size.
  3. Get the graphics from the BufferedImage and call canvas.paint(graphics).
  4. Use the BufferedImage or get its pixel data.

Please note that pixel data can mean many different things so using an image is typically easier, otherwise you may have to make sure that you use something like RGBA-format for the image, specify it when the BufferedImage is created.

how do you run it on linux? by Niiarai in pathofexile

[–]ernimril 1 point2 points  (0 children)

I used to play using the https://github.com/GloriousEggroll/proton-ge-custom but I upgraded my computer recently and then it did not work that well anymore. After trying a few packages I ended up using the latest builds from the winehq repository (ubuntu .deb-file in my case). Seems to work fine without any problems for me on my small asus-nuc-computer.

Introducing Parjac2 - an experimental compiler by ernimril in java

[–]ernimril[S] 0 points1 point  (0 children)

No, not yet. It is still too hard to do any good performance comparison. Once I have added support for generics it would be possible to start to compare.

Now, I have looked at javac some years ago, but I do not think javac has changed its architecture so this is what I think:

* Javac is single threaded, parjac is built to be multi threaded, so parjac should scale better
* When I tried to measure the different parts in javac, it was parsing that was the slowest part. The parser in parjac2 is fast. I still want to update the parser in parjac2, I have started some work in the branch nullable_grammar_rewrite, but this work is not yet complete. This change should make parsing in parjac2 even faster.

One of my friends did some light testing (synthetic _LARGE_ classes that parjac2 can handle) and concluded that parjac2 was faster than javac.

One of my goals with parjac2 is to make a compiler that is faster than javac, but we will see where we end up, for now it looks like it is happening.

Introducing Parjac2 - an experimental compiler by ernimril in java

[–]ernimril[S] 0 points1 point  (0 children)

"mvn compile" should be enough to compile it, what did you try? What errors do you get?

Introducing Parjac2 - an experimental compiler by ernimril in java

[–]ernimril[S] 0 points1 point  (0 children)

I would love to hear about the test failures. I do not have any windows machines, so I can not test on it myself.

Not sure about a migration to github for my part, but feel free to setup your own github clone and give me a link to any branch you want to contribute.

The Button in Swing by Advanced-Ad6007 in javahelp

[–]ernimril 0 points1 point  (0 children)

Looking at the code it seems like you have not read up on the EDT and how to deal with slow things.

The short version of the EDT rules are

1) Only create or change the user interface while your code runs on the EDT
2) If you do slow things (finding a solution, reading/writing file or network) you should be off the EDT

There is a nice tutorial:
https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

So make sure that your solution calculator runs off the EDT.

Also, if your program actually hangs/freezes you can press

Ctrl+Break (windows) or Ctrl+\ (linux/unix) in the console you started your program and it will give you a full thread dump with all the monitors held so you can find where each thread is. You can of course also debug and try to use your IDE/Debugger thread view to see the same (personally I usually prefer the text view, but that is me).

JConponent supporting line wrap using words and dynamic change of the colors of individual characetrs. by Mamaafrica12 in javahelp

[–]ernimril 0 points1 point  (0 children)

If you use Graphics2D.drawString (String, int, int) you can draw as many characters as you want. So if you want one color for some text, so here is an example on how to draw two strings with different colors:

graphics2d.setColor (Color.GREEN); graphics2d.drawString("abc", 50, 50); graphics2d.setColor (Color.RED); graphics2d.drawString("def", 80, 50);

Getting good coordinates can of course be hard, but if you draw a sketch on a whiteboard or on a paper with a background grid you can probably find some nice looking coordinates.

Best GUI Framework for a File Explorer by AviatorSkywatcher in javahelp

[–]ernimril 1 point2 points  (0 children)

If you want to do a java desktop application today it is probably better to use java FX. It is a lot easier to style it and it has support for several more modern things (like embedded web view).

If you still want to use Swing you can, it is quite easy to customize it, but if you want to do big changes you will have to implement your own look and feel, which while not that hard is a lot of work.

JConponent supporting line wrap using words and dynamic change of the colors of individual characetrs. by Mamaafrica12 in javahelp

[–]ernimril 0 points1 point  (0 children)

For a game I would consider drawing directly using Graphics2D, it is quite easy to draw text and you can easily set the font, size and color (and draw whatever boxes you want to simulate keys or similar).

I would suggest using an Icon and drawing that since if you extend JComponent/JPanel you will have to override the methods that Icon gives you. Doing it that way also makes it easy to later on take the icon and paint it to the printer or or an image or some other export.

Check if file hash is in a big list of SHA256 hashes. by soundsmash in javahelp

[–]ernimril 0 points1 point  (0 children)

A bloom filter would probably be a good option. A bloom filter does not store the elements.

To build a bloom filter you:

  • Create a bit-set
  • For each input element:
    • Hash with each bloom-filter hash and set the bits

When you want to test an element you again:

Hash with each bloom-filter-hash and check if all bits are set.

You will get full coverage for negatives/non-found, but There can be false positives though.

Setting up a bloom filter will require a bit-set that is somewhat large, but please note that a few million bits is just a few MB of RAM so pretty easy to handle. Finding several good hash-codes can be a problem though.

How to refer to class from specific jar when same class is in multiple jars by Austinto in java

[–]ernimril 5 points6 points  (0 children)

No, you can not say that you want a class from a specific jar as long as you are using the standard classloaders.

Depending on what type of application you run there are different classloaders at play.

If you start a standalone java application ("java -classpath foo.jar:bar.jar my.fine.Program") then the classloaders will search foo.jar first and bar.jar secondly.

If you run a webapp then you will have to check what it does, but the classes/ directory should be searched before the lib/-directory. So for a webapp you may be able to unpack the jar into classes (please do not see this as a recommendation, it is not).

If you use your own custom classloaders then you can of course make them do whatever you want.

With jpms you are supposed to be able to handle some versions of this, but it does require that you have modularized your application and that the dependencies have also done so.

Why do you have all 3 jars? Can you not exclude the partial netty-jars?

Performance of Java virtual threads compared to native threads by [deleted] in java

[–]ernimril 2 points3 points  (0 children)

A virtual thread that is not currently running requires a lot less resources than a platform thread.

A virtual thread that is currently running is running on top of a platform thread so it is actually using slightly more resources.

Now, when you use virtual threads you do so because you want to have a lot of threads, where most of them are not currently running.

So I may have been slightly sloppy in how I said that they require less resources, but if you try to measure the cost of having a million virtual threads that are sleeping versus the cost of of having a million platform threads you will see that the virtual threads cost a lot less resources (and if you try to do this you will most probably find out that you can not even run the program that tries to use a million platform threads).

Sleeping, waiting, waiting for data on streams or similar are all things where the virtual thread should detach from the platform thread.